2017-07-02 11 views
0

こんにちはすべて私はStringintを取得し、クライアントから送信したいプロジェクトがあります。 。私は何も間違ったものを得ません。私を助けてください。ソケットJavaのデータ(String、Int)を取得して送信する際にエラーが発生する

クライアント:

//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

: iは受注

メインを制御すぎメインクラスを持っています。あなた

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ビットintとして書き込む必要があります。受信側では、それに続く文字列の長さを含む正確に4バイトが必要です。次に、文字列を構成する正確な正しいバイト数を読み取ることができます。もちろん、Javaでは、文字は2バイト長であることを忘れないでください。

+0

あなたの答えに感謝します。私はメインクラスも持っているので、どのような種類のデータを今すぐ送信して取得するのかを制御します。 それは十分だと思いますか? – alish

関連する問題