Socket通信,客户端和服务的互相收发------客户端接收代码块执行不了 socket通信,client server互相收发 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void main(String[] args) throws Exception { int i=0; while(i<10){ Socket s = new Socket("127.0.0.1",9052); String str="客户端发送"+i; DataOutputStream out = new DataOutputStream(s.getOutputStream()); out.write(str.getBytes()); System.out.println(str); out.flush(); //到这里就执行不下去了 DataInputStream in = new DataInputStream(s.getInputStream()); byte[] result =new byte[100000]; int len; int thislength = 0; int totallength= 0; do { thislength = in.read(result, totallength, 2000); if(thislength>0) { totallength +=thislength; } } while (in.available()>0); byte[] response = new byte[totallength]; System.arraycopy(result, 0, response, 0, totallength); String responseStr = new String(response); System.out.println("服务端接收:" + responseStr); out.close(); in.close(); i++; }}如果把 DataInputStream in = new DataInputStream(s.getInputStream()); byte[] result =new byte[100000]; int len; int thislength = 0; int totallength= 0; do { thislength = in.read(result, totallength, 2000); if(thislength>0) { totallength +=thislength; } } while (in.available()>0); byte[] response = new byte[totallength]; System.arraycopy(result, 0, response, 0, totallength); String responseStr = new String(response); System.out.println("服务端接收:" + responseStr); out.close(); in.close(); 这一段注释掉就能执行了 但是不能接收啊 求大家帮忙解决…… System.out.println("服务端接收:" + responseStr);服务端能执行到这句么,感觉很悬呢 thislength = in.read(result, totallength, 2000);你调试下,应该阻塞在这,这种交互你需要一个简单的协议,另外data装饰类没太大意义 我记得thinking in java 里说过 available ()得谨慎使用...... 还有 do while ... 不是不推荐用吗 DataInputStream是专门格式化读取的你怎么还用 read() 了...... 客户端10次循环,10个socket共用一个端口怎么可能 好久没用socket了想多了.上面完全错了 JAVA编程思想的练习题,不知道是翻译问题,还是我理解能力差 action="User.do?act=query" 为社么要加上act=query。 C/S 模式用户登录,同名验证如何解决 java 类型转换问题 String 和 StringBuffer的区别的一个问题 java调用已有的DLL时类型转换问题 关于IO输出,需要高用flush吗? 急 Java的价值 类的高级加载问题,请帮帮我丫。在线等待指点呢 为什么输入二月都是29天呢 如何使用定时任务执行一个jar包 一个JAVA的小问题
int i=0; while(i<10){
Socket s = new Socket("127.0.0.1",9052);
String str="客户端发送"+i;
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.write(str.getBytes());
System.out.println(str);
out.flush();
//到这里就执行不下去了
DataInputStream in = new DataInputStream(s.getInputStream());
byte[] result =new byte[100000];
int len;
int thislength = 0;
int totallength= 0;
do {
thislength = in.read(result, totallength, 2000);
if(thislength>0)
{
totallength +=thislength;
}
} while (in.available()>0);
byte[] response = new byte[totallength];
System.arraycopy(result, 0, response, 0, totallength);
String responseStr = new String(response);
System.out.println("服务端接收:" + responseStr);
out.close();
in.close();
i++;
}}
如果把
DataInputStream in = new DataInputStream(s.getInputStream());
byte[] result =new byte[100000];
int len;
int thislength = 0;
int totallength= 0;
do {
thislength = in.read(result, totallength, 2000);
if(thislength>0)
{
totallength +=thislength;
}
} while (in.available()>0);
byte[] response = new byte[totallength];
System.arraycopy(result, 0, response, 0, totallength);
String responseStr = new String(response);
System.out.println("服务端接收:" + responseStr);
out.close();
in.close();
这一段注释掉就能执行了 但是不能接收啊
求大家帮忙解决……
System.out.println("服务端接收:" + responseStr);服务端能执行到这句么,感觉很悬呢
你调试下,应该阻塞在这,这种交互你需要一个简单的协议,另外data装饰类没太大意义
还有 do while ... 不是不推荐用吗
DataInputStream是专门格式化读取的你怎么还用 read() 了......