Sending multiple strings over same TCP connection in Java? -
i have been trying hours , can't seem done. want send 2 strings tcp client server, , return them both capitalized. 2 strings username , password: client code is:
public void actionperformed(actionevent e) { final string s1 =textpane.gettext(); //retrieve username final string s2=passwordfield.gettext();//retrieve password //************************************************************************** //after have gotten credentials, have send them server check in database socket clientsocket = null; //create new socket string response=null; //create supposed become server's response string response2=null; try { /** * construct client socket, specify ip address here * localhost loopback device , port number @ * connection needs initialized. */ clientsocket = new socket("localhost", 6789); // these streams same purpose in server side // dataoutputstream outtoserver = new dataoutputstream(clientsocket .getoutputstream()); dataoutputstream outtoserver2 = new dataoutputstream(clientsocket .getoutputstream()); bufferedreader infromserver = new bufferedreader( new inputstreamreader(clientsocket.getinputstream())); // read user's input console. // // send sentence server: note need append // endline character // outtoserver.writebytes(s1 + '\n'); //send username outtoserver.writebytes(s2 + '\n'); //send password // listen on socket , read reply of server // response2=infromserver.readline(); response = infromserver.readline(); } catch (unknownhostexception ex) {// catch exceptions // ex.printstacktrace(); } catch (ioexception ex) { ex.printstacktrace(); } system.out.println("from server: " + response); //print response of server system.out.println("from server: " + response2); try { // close socket when done // clientsocket.close(); } catch (ioexception ex) { ex.printstacktrace(); } } });
bulk of server code is:
while(true) { // connection socket // socket connectionsocket; try { // accept connection on connection socket welcome socket server socket // connectionsocket = welcomesocket.accept(); // input stream connection socket , save in buffer // bufferedreader infromclient = new bufferedreader(new inputstreamreader(connectionsocket.getinputstream())); // allocate output stream send data client // dataoutputstream outtoclient = new dataoutputstream(connectionsocket.getoutputstream()); // read client sentence receive buffer // clientsentence = infromclient.readline(); clientsentence2=infromclient.readline(); system.out.println("from user: " + clientsentence); // capitalize client's sentence, here server actions on client's data // capitalizedsentence = clientsentence.touppercase() + '\n'; capitalizedsentence2=clientsentence2.touppercase()+'\n'; if(clientsentence.equalsignorecase("quit")){// close socket if server sends quit message // connectionsocket.close(); system.out.println("connection socket quitting"); // note here server not exit or end close connection client // } else outtoclient.writebytes(capitalizedsentence);// send capitalized sentence client // } catch (ioexception ex) { ex.printstacktrace(); } }
what doing wrong? can't 1 buffered reader accept 2 separate strings? how separate them later?
you haven't written capitalizedsentence2 client that's why client waiting @ second readline()
mentioned below:
response2 = infromserver.readline(); response = infromserver.readline();
put below line in server side code in else part
outtoclient.writebytes(capitalizedsentence2);
it might solve problem.
Comments
Post a Comment