I am working on an application consisting of sockets. It needs to transfer a String value from the client(android device) to the server(my PC running Ubuntu). On the server side code, I need to store the value being transferred via socket in a String variable.
Here is the code I am using to send the String value to the server.
//instantiating the socket object for the client
Socket client = new Socket("192.168.0.103", port);
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
String msg = "My String Value";
//Message to be send to server
out.writeUTF(msg);
//Closing the client's socket. No connections after this.
client.close();
And here is the code I am using to get the String value at the server.
//Server object. Stores the reference to the socket object returned by the accept()
//The returned socket object contains established connection to the client.
Socket server = serverSocket.accept();
DataInputStream in = new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
//This prints "My String Message"
//Something like this i want to do.
String msg = in.readUTF();
//closing the server socket. No connection now.
server.close();
Right now what is happening is the program at server side is getting stuck at line :
String msg = in.readUTF();
What is the correct way of storing the String value from DataInputStream into a String variable?