Socket 关闭问题

VirusFu 2011-04-20 01:27:10
郁闷几天了,两个文件,Service1.java和Client1.java文件.运行后当客户端文件控制台输入内容后,
Client1 文件的控制台中总是异常:java.net.SocketException: Socket is closed
我也没有关闭socket,也没有发现socket什么时候关闭的。

public class Service1 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
BufferedReader br = null;
PrintWriter os = null;
try {
serverSocket = new ServerSocket(8006);
while(true){
Socket service =serverSocket.accept();
br = new BufferedReader(new InputStreamReader(service.getInputStream()));
os = new PrintWriter(service.getOutputStream());
String s = br.readLine();
System.out.println("服务器接收" + s);
os.print("发送" + s + "成功");
os.flush();
os.close();
//service.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}



public class Client1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = null;
Socket client = null;
BufferedReader br = null;
PrintWriter os = null;
try {
while(!(name = scanner.next()).equals("exit")){
client = new Socket("127.0.0.1",8007);
os = new PrintWriter(client.getOutputStream());
os.print(name);
os.flush();
os.close();
InputStream in = client.getInputStream(); //这个位置总是出现问题
br = new BufferedReader(new InputStreamReader(in));
System.out.println("客户端收到:" + br.readLine());
// client.close();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

...全文
453 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
qybao 2011-04-22
  • 打赏
  • 举报
回复
LS两个不要乱说,socket用完后关闭是应该的。
while里的Socket service =serverSocket.accept();每次都会得到一个新的socket,所以不存在多次关闭。
LZ的问题在于客户端关闭了socket的输出流后,要使用了socket的输入流。
而javadoc里已经很明确的说明了,当关闭socket的输入/输出流时,socket也被关闭
所以客户端关闭socket输出流后,socket就被关闭,此时如果再使用socket的输入流,就会抛出socket已关闭的异常。
TKD03072010 2011-04-22
  • 打赏
  • 举报
回复
在try-catch后面加个finally 关闭一下服务器就行了 close()放在while关闭怎么行 后面还要进行通讯呢!
凉岑玉 2011-04-22
  • 打赏
  • 举报
回复

while(true){
Socket service =serverSocket.accept();
br = new BufferedReader(new InputStreamReader(service.getInputStream()));
os = new PrintWriter(service.getOutputStream());
String s = br.readLine();
System.out.println("服务器接收" + s);
os.print("发送" + s + "成功");
os.flush();
os.close();
//service.close();
}


你关闭了N次了··
qybao 2011-04-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 virusfu 的回复:]
引用 6 楼 qybao 的回复:
把os.close放到System.out.println("客户端收到:" + br.……

问题解决了,但是有不解:
为什么要把os.close放到System.out.println("客户端收到:" + br.…… 后面
1.os.close()关闭的输出流与br没有关系
2.这个也比不是关闭socket啊 为什么提示 Socket is c……
[/Quote]
LZ好好看看java的文档说明就清楚了
socket的getOutputStream的文档说明给你复制过来看看
问题就在红色的那行。

getOutputStream
public OutputStream getOutputStream()
throws IOException
Returns an output stream for this socket.
If this socket has an associated channel then the resulting output stream delegates all of its operations to the channel. If the channel is in non-blocking mode then the output stream's write operations will throw an IllegalBlockingModeException.

Closing the returned OutputStream will close the associated socket.

Returns:
an output stream for writing bytes to this socket.
Throws:
IOException - if an I/O error occurs when creating the output stream or if the socket is not connected.

VirusFu 2011-04-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 qybao 的回复:]
把os.close放到System.out.println("客户端收到:" + br.……
[/Quote]
问题解决了,但是有不解:
为什么要把os.close放到System.out.println("客户端收到:" + br.…… 后面
1.os.close()关闭的输出流与br没有关系
2.这个也比不是关闭socket啊 为什么提示 Socket is closed ?
一时懿亮 2011-04-20
  • 打赏
  • 举报
回复
端口也写错了!8006?8007?
qybao 2011-04-20
  • 打赏
  • 举报
回复
LZ是通过readLine来读取的,所以必须保证传输数据有换行符,否则会发生堵塞
server端
os.print("发送" + s + "成功");改成os.println("发送" + s + "成功");

client端
os.print(name);改成os.println(name);
把os.close放到System.out.println("客户端收到:" + br.readLine()); 后


amos1989 2011-04-20
  • 打赏
  • 举报
回复
不能重复close

你用一个循环来close连接。肯定报错职。
bf234511171 2011-04-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 liya32432588 的回复:]
弱弱的问下,Service与Client之间的通信怎么解决呀,俺是java新手,最近看到一个多媒体软件系统设置的题目,打算试试手,请教lz,有代码的请发至289672012@qq.com,谢谢!
[/Quote]

其实你要是新手的话就知道现象就好。。。
ntz 2011-04-20
  • 打赏
  • 举报
回复
弱弱的问下,Service与Client之间的通信怎么解决呀,俺是java新手,最近看到一个多媒体软件系统设置的题目,打算试试手,请教lz,有代码的请发至289672012@qq.com,谢谢!
whut_lcy 2011-04-20
  • 打赏
  • 举报
回复
不能重复close
彡颵爺 2011-04-20
  • 打赏
  • 举报
回复
你没有发现你的.close()方法是在while循环里面的吗,在第一个循环时os.close();就得到执行所以就已经关闭了
meng_qing_shan 2011-04-20
  • 打赏
  • 举报
回复
android的程序怎么和webservice通信啊?
macower 2011-04-20
  • 打赏
  • 举报
回复
服务端 你close 干嘛?

close了客户端还要用 怎么去通信

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧