2017-07-02 25 views
0

嗨每一個我有一個項目,得到Stringintint和客戶端,我想將它們發送到我的server.but我有問題得到這個。我什麼也沒有得到,也沒有錯請幫幫我。錯誤獲取和發送數據(字符串,詮釋)在套接字java

客戶:

//build a socket. 
public void connectclient() throws IOException 
{ 
    socket = new Socket("localhost", 9097); 
    System.out.println("connect to server on port 9097"); 
} 
public void startstreams() throws IOException , ClassNotFoundException  
{ 
in = socket.getInputStream(); 
out = socket.getOutputStream(); 
dos = new DataOutputStream(out); 
dis = new DataInputStream(in); 
writer = new OutputStreamWriter(out); 
reader = new InputStreamReader(in); 
breader = new BufferedReader(reader); 
bwriter = new BufferedWriter (writer); 
w = new PrintWriter(out, true); 
} 
public void writeSocketMyJson(String n) throws IOException 
{ 
    w = new PrintWriter(out, true); 
    w.println(n); 
    w.flush(); 
    w.close(); 
} 

massage1 = "username"; 
//send username that get from login form to server. 
public void sendusername() throws IOException 
{ 
    writeSocketMyJson(massage1); 
} 

massage2 ="admin"; 
//send password that get from login form to server. 
public void sendpassword() throws IOException 
{ 
    writeSocketMyJson(massage2); 
} 

//send access level to server. 
public void sendaccess(int l) throws IOException 
{ 
    dos.writeInt(l); 
    dos.flush(); 
} 
sendaccess(21); 

服務器:

//build server. 
public void connectserver() throws IOException 
{ 
    listener = new ServerSocket(9097); 
    System.out.println("Server is running on port 9097 ..."); 
} 
//wait for new connection. 
public void waitforclient() throws IOException 
{ 
    socket = listener.accept(); 
System.out.println("A new client connected to the server"); 
} 
public void startstreams() throws IOException 
{ 
in = socket.getInputStream(); 
out = socket.getOutputStream(); 
dos = new DataOutputStream(out); 
dis = new DataInputStream(in);  
writer = new OutputStreamWriter(out); 
reader = new InputStreamReader(in); 
bwriter = new BufferedWriter (writer); 
breader = new BufferedReader (reader); 
} 
public String readSocket() throws IOException 
{ 
     breader = new BufferedReader(reader); 
     while (true) 
     { 
      massage = new String(); 
      massage = breader.readLine(); 
      if (massage.equals(null) == false) 
      { 
       break; 
      } 
     } 
     return(massage); 
} 

//get username that client send it. 
public String getusername() throws IOException, ClassNotFoundException 
{ 
    try 
    { 
     username = new String(); 
     username = readSocket(); 
     System.out.println("the username is : " + username); 
    } 
    catch(IOException IOE) 
    { 
      IOE.printStackTrace();//if there is an error, print it out 
    } 
    return(username); 
} 

//get password that client send it. 
public String getpassword() throws IOException, ClassNotFoundException 
{ 
    try 
    { 
     password = new String(); 
     password = readSocket(); 
     System.out.println("the password is : " + password); 
    } 
    catch(IOException IOE) 
    { 
      IOE.printStackTrace();//if there is an error, print it out 
    } 
    return(password); 
} 

//get commend from client. 
//admin or user send which commend 21-add new information 22-show information  
public int getaccess() throws IOException 
{ 
    System.out.println("server get access : " + dis.readInt()); 
    return(dis.readInt()); 
} 

但是當我打電話getpassword()我什麼也得不到。 當我打電話getaccess()我什麼也沒得到。 爲什麼?請幫幫我。 我有主類也控制訂單

主:

//build Server & Client 
    Server server = new Server(); 
    Client client = new Client(); 

    //Start Server & Client 
    server.connectserver(); 
    client.connectclient(); 

    //Server wait for new connection 
    server.waitforclient(); 

    //start the Streams 
    server.startstreams(); 
    client.startstreams(); 

    client.sendusername(); 
    String msg1 =server.readSocket(); 

    client.sendpassword(); 
    String msg2 =server.readSocket(); 

    client.sendaccess(); 
    int n = getaccess(); 

回答

0

你的代碼是完全的無稽之談。你不喜歡的東西

breader = new BufferedReader (reader); 

public String readSocket() throws IOException 
{ 
    breader = new BufferedReader(reader); 

和像

w = new PrintWriter(out, true); 

public void writeSocketMyJson(String n) throws IOException 
{ 
    w = new PrintWriter(out, true); 

和像

password = new String(); 
    password = readSocket(); 

我建議你只是把它全部帶走,並從頭開始,與Java的一些示例代碼插座,其中interwebz上有很多。

要記住的事情是,你不能只是假裝一個套接字是一個普通的流,你不能只在它上面創建一個BufferedReader。在任何特定的時刻,您都需要確切知道發送了多少個字節,因此準確地指定要接收多少個字節。

所以一般來說,在發送端,你不能寫一個字符串,並期望在接收端的字符串末尾被檢測到。在發送端,首先需要將字符串的長度寫爲32位整數,因此在接收端,您知道您預計只有4個字節包含隨後的字符串長度。然後,您可以讀取組成字符串的確切正確字節數。當然,不要忘記,在java中,一個字符長度爲2個字節。

+0

謝謝你的回答。我也有主類,控制他們的訂單,我現在發送哪種數據並獲得它。 你認爲這是否足夠? – alish