Note that there are some explanatory texts on larger screens.

plurals
  1. POClient server don't communicate
    text
    copied!<p>i have client server application when i run the server i get what i print from the server Class but when i run the Client class nothing happen even if the connection established but it seems like the Client class doesn't receive any messages from server</p> <p>here is my server code </p> <pre><code>import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class Server { ServerSocket provider; Socket clientsocket; OutputStream out; InputStream in; InetAddress clientAddress; StringBuilder sb; BufferedReader br; String msg=""; Server(){ } public void Communicate() { try { provider=new ServerSocket(2013); System.out.println("Server Waiting for connections"); clientsocket=provider.accept(); System.out.println("incoming connection from "); clientAddress=clientsocket.getInetAddress(); //System.out.println("Client Name"+clientAddress.getHostName()); System.out.println("Client Address "+clientAddress.getHostAddress()); out=clientsocket.getOutputStream(); in=clientsocket.getInputStream(); sendmsg("connection success"); do { msg=read_instream(in); System.out.println("client say"+msg); if(msg.equals("bye")) { sendmsg(msg); clientsocket.close(); in.close(); out.close(); } } while(!msg.equals("bye")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void sendmsg(String msg) throws IOException { out.write(msg.getBytes()); out.flush(); } private String read_instream(InputStream in) throws IOException { br=new BufferedReader(new InputStreamReader(in)); sb=new StringBuilder(); while (br.readLine()!=null) sb.append(br.readLine()); return sb.toString(); } public static void main(String[] args){ Server provider=new Server(); while(true) provider.Communicate(); } } </code></pre> <p>and the client code</p> <pre><code>import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class Client { Socket client; String msg="give me the last degree"; OutputStream out; InputStream in; StringBuilder sb; BufferedReader br; public void Communicate() { try { client=new Socket("localhost",2013); out=client.getOutputStream(); in=client.getInputStream(); System.out.println("Messege from server:"+read_instream(in)); while (!msg.equals("bye")) { msg=read_instream(in); System.out.println("server say"+msg); sendmsg("hey from client"); msg="bye"; sendmsg(msg); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private String read_instream(InputStream in) throws IOException { br=new BufferedReader(new InputStreamReader(in)); sb=new StringBuilder(); while (br.readLine()!=null) sb.append(br.readLine()); return sb.toString(); } private void sendmsg(String msg) throws IOException { out.write(msg.getBytes()); out.flush(); } public static void main(String[] args) { // TODO Auto-generated method stub Client requester=new Client(); requester.Communicate(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload