Java TCP sending first message, then infinite wait -


my issue based on code below:

  • run tcpserver.java
  • then run tcpclient.java

i expect have client print out

server said(1): hey dude 1

server said(2): hey dude 2

... stays on hey dude 1. doing not producing results want?

tcpserver.java

import java.io.*; import java.net.*;  class tcpserver {     public static void main (string args[]) throws exception{         new tcpserver();     }     tcpserver() throws exception{         //create welcoming socket @ port 6789         serversocket welcomesocket = new serversocket(6789);          while (true) {             //block on welcoming socket contact client             socket connectionsocket = welcomesocket.accept();             // create thread client             connection c = new connection(connectionsocket);         }     }     class connection extends thread{         socket connectionsocket;         connection(socket _connectionsocket){             connectionsocket = _connectionsocket;             this.start();         }         public void run(){             try{                 //create input stream attached socket                 bufferedreader infromclient = new bufferedreader(new inputstreamreader (connectionsocket.getinputstream()));                 //create output stream attached socket                 printwriter outtoclient = new printwriter(new outputstreamwriter(connectionsocket.getoutputstream()));                 //read in line socket                 string clientsentence = infromclient.readline();                 system.out.println("client sent: "+clientsentence);                 //process                 string capitalizedsentence = clientsentence.touppercase() + '\n';                 //write out line socket                 outtoclient.print(capitalizedsentence);                 outtoclient.flush();             }catch(exception e){}         }     } } 

tcpclient.java

import java.io.*; import java.net.*;  class tcpclient {     //string name="";     string host = "localhost";     int port = 6789;     socket socket = null;     public static void main(string args[]) throws exception{         tcpclient client = new tcpclient();         client.sendtoserver("hey dude 1");         system.out.println("server said(1): "+client.recievefromserver());         client.sendtoserver("hey dude 2");         system.out.println("server said(2): "+client.recievefromserver());         client.close();     }      tcpclient(string _host, int _port) throws exception{         host = _host;         port = _port;         socket = new socket(host, port);     }     tcpclient() throws exception{         socket = new socket(host, port);     }     void sendtoserver(string msg) throws exception{         //create output stream attached socket         printwriter outtoserver = new printwriter(new outputstreamwriter(socket.getoutputstream()));         //send msg server         outtoserver.print(msg + '\n');         outtoserver.flush();     }     string recievefromserver() throws exception{         //create input stream attached socket         bufferedreader infromserver = new bufferedreader(new inputstreamreader (socket.getinputstream()));         //read line server         string res = infromserver.readline(); // if connection closes on server end, throws java.net.socketexception          return res;     }     void close() throws ioexception{         socket.close();     } } 

your server thread ends process first message. need put server code loop this:

string clientsentence; while ((clientsentence = infromclient.readline()) != null) {     system.out.println("client sent: "+clientsentence);     //process     string capitalizedsentence = clientsentence.touppercase() + '\n';     //write out line socket     outtoclient.print(capitalizedsentence);     outtoclient.flush(); } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -