当我运行服务器时,我有客户端服务器应用程序我得到了从服务器类打印的内容但是当我运行Client类时,即使建立连接也没有任何反应 但似乎Client类没有收到来自服务器的任何消息
这是我的服务器代码
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();
}
}
和客户端代码
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();
}
}
答案 0 :(得分:1)
您的read_instream方法将阻塞,直到流关闭。 br.readline()在“到达流的末尾”之前不会返回null(参见javadoc)。您的服务器类发送消息,将其刷新,但永远不会结束流。
编辑:发生的事情仍然是阻塞问题。您的服务器仅在从客户端收到消息后才关闭到客户端的输出流。但是,服务器上的read_instream()方法等待客户端关闭流。客户端永远不会关闭它,因为它仍然在等待服务器首先关闭。因此,一种选择是在sendmsg()方法中关闭输出流。但是,这不允许发送任何其他消息。您也可以将read_instream()方法更改为在每行之后返回,而不是让它等待流的结束。
阅读Oracle docs中的客户端/服务器教程。他们使用第二个选项在服务器和客户端之间进行长时间对话:不要等待read_instream()方法中的流结束。在本教程结束时,他们将简要介绍支持多个客户端。