快三点了,还搞不定!Socket

dext 2009-12-03 04:42:54
很简单的 客户端 服务器程序,收发都有。就是不正常。 总是停在哪里。和Linux 没关系吧!Windows上没试

public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(4321);
Socket cs = ss.accept();
BufferedReader rcs = new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintWriter wcs = new PrintWriter(cs.getOutputStream());

String s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);

rcs.close();
wcs.close();
cs.close();
ss.close();
}
}


public class Client {
public static void main(String[] args) throws IOException {
Socket ss = new Socket("localhost", 4321);
BufferedReader rss = new BufferedReader(new InputStreamReader(ss.getInputStream()));
PrintWriter wss = new PrintWriter(ss.getOutputStream());

wss.println("Why?");
String s;
s = rss.readLine();
System.out.println(s);
wss.println(s);
s = rss.readLine();
System.out.println("1");
wss.println(s);
s = rss.readLine();
System.out.println("1");
wss.println(s);
s = rss.readLine();
System.out.println("1");
wss.println(s);
s = rss.readLine();
System.out.println(s);

rss.close();
wss.close();
ss.close();

}
}


给点建议~~

...全文
84 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dext 2009-12-04
  • 打赏
  • 举报
回复
这只是一个实验,按道理 有发有收,可以的。结果不正常。
dext 2009-12-04
  • 打赏
  • 举报
回复
问题解决,是字符流 和 字节 流 的问题。 散分
NeverGiveUp2016 2009-12-04
  • 打赏
  • 举报
回复
用死循环,服务器一直等待连接就是了,while(true);
NeverGiveUp2016 2009-12-04
  • 打赏
  • 举报
回复

System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);

建议循环。
crazylaa 2009-12-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 taolei 的回复:]
PrintWriter wcs = new PrintWriter(cs.getOutputStream(),true);
PrintWriter wss = new PrintWriter(ss.getOutputStream(),true);
[/Quote]

up
NeverGiveUp2016 2009-12-04
  • 打赏
  • 举报
回复
mark,楼上已解决!
taolei 2009-12-04
  • 打赏
  • 举报
回复
PrintWriter wcs = new PrintWriter(cs.getOutputStream(),true);
PrintWriter wss = new PrintWriter(ss.getOutputStream(),true);
xiesisi3 2009-12-03
  • 打赏
  • 举报
回复
很简单的 客户端 服务器程序,收发都有。就是不正常。 总是停在哪里。和Linux 没关系吧!Windows上没试

怎么样个不正常法呢?你这样写的程序就只能是服务器说句话,然后客户端回一句,然后服务器再说一句,因为readline()方法是阻塞式的

62,614

社区成员

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

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