都 要关闭的, 例: try { ServerSocket server = new ServerSocket(5776); while (true) { Socket connection = server.accept( ); try { Writer out = new OutputStreamWriter(connection.getOutputStream( )); out.write("You've connected to this server. Bye-bye now.\r\n"); out.flush( ); connection.close( ); } catch (IOException ex) { // This tends to be a transitory error for this one connection; // e.g. the client broke the connection early. Consequently, // you don't want to break the loop or print an error message. // However, you might choose to log this exception in an error log. } finally { // Guarantee that sockets are closed when complete. try { if (connection != null) connection.close( ); } catch (IOException ex) {} }}catch (IOException ex) { System.err.println(ex);}
try { ServerSocket server = new ServerSocket(5776); while (true) { Socket connection = server.accept( ); try { Writer out = new OutputStreamWriter(connection.getOutputStream( )); out.write("You've connected to this server. Bye-bye now.\r\n"); out.flush( ); connection.close( ); } catch (IOException ex) { // This tends to be a transitory error for this one connection; // e.g. the client broke the connection early. Consequently, // you don't want to break the loop or print an error message. // However, you might choose to log this exception in an error log. } finally { // Guarantee that sockets are closed when complete. try { if (connection != null) connection.close( ); } catch (IOException ex) {} }}catch (IOException ex) { System.err.println(ex);}
问题是,比如client端,发送数据完成,并接收到数据,这个时候再关闭socket。那我再次输入的时候不是会出问题的么?这点还是没弄清楚,哪位帮我看看要关闭的地方,具体给我指出来行么。public void run()
{
in = getInputStream(s);
out = getOutputStream(s);
while(true)
{
String message = getMessage();
try
{
System.out.println("send message : " + message);
out.write(message);
out.newLine();
out.flush();
String str = in.readLine();
System.out.println("return : " + str);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
s.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}我把那个close加在这里,Server端还是报错。
java.net.SocketException: socket closed
我现在启动了两个客户端,分别发送了一个消息给服务器端,服务器端接收到了,但是A客户端只接收到A自己发出的消息,B也只收到自己发的。我要做的应该是A发的A,B都接收的到,同理B发的任何一个接连上的客户端应该接收到。
我觉得Server端写的应该没问题,可能是Client端接收的地方写的有点小毛病。private void send(String message)
{
for(Socket s: list)
{
BufferedWriter out = getOutputStream(s);
try
{
out.write(message);
out.newLine();
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
/*try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}*/
}
}
}send方法是向每一个正在接连上的Client发回信息。
第二个问题:看看你服务器端有没有把每个客户端都保存在arraylist容器中
发送时有没有给每个客户端发送数据?ps:下次发代码 用工具栏那个带#的按键 这样发出来有缩进,方面看
public void run()
{
in = getInputStream(s);
out = getOutputStream(s);
while (true)
{
String message = getMessage();
try
{
//这个函数里,先执行getMessage()从控制台输入,然后输入消息M 到out中.这个时候执行到in.readLine()
//客户端阻塞在这等待,而server接到M后转发,in.ReadLine读到消息输出消息M,进入下一个While循环
//在getMessage()处等待控制台输入
//所以客户端是在getMessage()等待,只能接收控制台输入,是没办法接收到server端发的消息的.
System.out.println("send message : " + message);
out.write(message);
out.newLine();
out.flush();
String str = in.readLine();
System.out.println("return : " + str);
} catch (IOException e)
{
e.printStackTrace();
}
}
}
http://mybeautiful.javaeye.com/blog/659317
基本同意这个观点,read, write要用两个不同的线程处理!