Reputation: 624
I am not able to send the data on the second instance. The server just waits infinitely for client data Here is my sample server code snippet:
ServerSocket serv = new ServerSocket(6789);
Socket soc = serv.accept();
System.out.println("waiting for client's input");
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
DataOutputStream out = new DataOutputStream(soc.getOutputStream());
String indata=in.readLine();
System.out.println("The client says: "+indata+"\n Send them some data: ");
String datum="demodata";
out.writeBytes(datum);
System.out.println("Data sent");
Sample Client:
ocket soc = new Socket("localhost",6789);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
DataOutputStream out = new DataOutputStream(soc.getOutputStream());
System.out.println("Connected to: "+soc.getLocalAddress() +"\nEnter data to be sent: ");
String outdata = br.readLine(); //take input
out.writeBytes(outdata); // send
String indata=in.readLine(); //read
System.out.println("Data Sent! Now reading data from server "+indata)
Please tell me my problem! Thanks in advance
Upvotes: 1
Views: 189
Reputation: 4645
Here is some sample code. See if it works for you. You can stop the server code with Ctrl-C and the client will wait for the server to reappear.
Also, you can stop the server and it will wait for the client to reappear. The main method has syntax
package test;
import java.io.*;
import java.net.*;
public class SocketIPC{
public PrintWriter out;
BufferedReader in;
Socket socket = null;
ServerSocket serverSocket = null;
StringBuffer sb;
int port;
public SocketIPC(int port) throws Exception{
this.port = port;
}
public void send(String msg){
sb.append(msg+"\n");
}
public void flush(){
out.write(sb.toString());
sb = new StringBuffer();
out.flush();
}
public String recv() throws Exception{
return in.readLine();
}
public void setIO() throws Exception{
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
sb = new StringBuffer();
}
public void serverLoop() throws Exception{
serverSocket = new ServerSocket(port);
while(true){
socket = serverSocket.accept();
setIO();
int count = 0;
System.out.println("Connected.");
while(true){
count += 1;
for(int j=0; j<100; j++)
send("+");
send(String.format(".%d", count));
flush();
Thread.sleep(1000L);
String msg = recv();
if (msg!=null)
System.out.println(msg);
else
break;// socket connection is broken
}
}
}
public void clientLoop() throws Exception{
while(true){
while(socket==null){
try{
socket = new Socket("localhost", port);
}catch(Exception e){
System.out.println("Waiting for server.");
Thread.sleep(1000L);
}
}
setIO();
System.out.println("Connected.");
while(true){
String msg = recv();
if (msg==null){
socket.close();
socket = null;
break;
}
if (msg.charAt(0)=='.'){
int idx = Integer.parseInt(msg.substring(1));
System.out.println(idx);
send(Integer.toString(idx));
Thread.sleep(2000);
flush();
}
}
}
}
public static void main(String[] args){
if (args.length != 1){
System.out.println("Server invocation: java test.SocketIPC s");
System.out.println("Client invocation: java test.SocketIPC c");
}
int port = 32000;
try{
SocketIPC fip = new SocketIPC(port);
if (args[0].equals("s")){
System.out.println("Server started");
fip.serverLoop();
}else{
System.out.println("Client started");
fip.clientLoop();
}
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
}
Upvotes: 1
Reputation: 3824
Answer is quite simple. You want to exchange lines of text between server and client.
The correct part is on the server side:
String indata=in.readLine();
The wrong part is on the client side:
out.writeBytes(outdata); // send
I haven't tested the code, but it seems you are just sending some data and your server side waits for \n
(newline escape sequence) to appear.
Option 1:
Construct a PrintWriter
and call the corresponding println
method.
Option 2:
Append a newline (\n
) manually.
Afterwards, readLine
on the server side will recognize the line terminated by \n
and will proceed.
Upvotes: 1
Reputation: 80633
Your client is not sending a newline character in the output it is writing. And your server is expecting this newline, implicitly, by calling readLine
on BufferedReader (which expects a terminating newline character in the data it reads via that method). Add the following in both your client and server code, after calling out.writeBytes()
:
out.newLine();
out.flush();
Alternatively, you can use PrintWriter's instead and just utilize its println
method.
Upvotes: 1
Reputation: 115388
I think that you forgot to flush output stream.
Add the line: out.flush()
just after out.writeBytes()
on both server and client sides.
Upvotes: 1