Reputation: 3876
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
here is my server 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();
}
}
and the client 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();
}
}
Upvotes: 0
Views: 124
Reputation: 197
Your read_instream method will block until the stream closes. br.readline() will not return null until "the end of the stream is reached" (see javadoc). Your server class sends a message, flushes it, but never ends the stream.
edit: What happens is still a blocking issue. Your server only closes the output stream to the client after itself has received a message back from the client. However the read_instream() method on the Server waits for the client to close the stream. The client never closes it because it's still waiting for the server to close in the first place. So one option is to close the outputstream in the sendmsg() method. This however will not allow for any other message to be send. You may also change the read_instream() method to return after each line, and not have it wait for the end of the stream.
Read the client/server tutorial from the Oracle docs. They use the second option for long running conversation between server and client: don't wait for the end of the stream in the read_instream() method. At the end of the tutorial they'll briefly go into supporting multiple clients.
Upvotes: 1