问题描述
publicstaticvoidmain(String[]args)throwsException{ServerSocketss=newServerSocket(9052);System.out.println("开始等待某个连接");do{Sockets=ss.accept();System.out.println("获取到一个连接");DataInputStreamin=newDataInputStream(s.getInputStream());byte[]result=newbyte[100000];intthislength=0;inttotallength=0;do{thislength=in.read(result,totallength,2000);if(thislength>0){totallength+=thislength;}}while(thislength>0);byte[]response=newbyte[totallength];System.arraycopy(result,0,response,0,totallength);StringresponseStr=newString(response);System.out.println("服务端接收:"+responseStr);Stringstr="服务端发送"+responseStr;DataOutputStreamout=newDataOutputStream(s.getOutputStream());out.write(str.getBytes());System.out.println(str);out.flush();}while(true);//ss.close();}
这是我测试用的服务端的代码,能收发
解决方案
解决方案二:
publicstaticvoidmain(String[]args)throwsException{inti=0;while(i<10){Sockets=newSocket("127.0.0.1",9052);Stringstr="客户端发送"+i;DataOutputStreamout=newDataOutputStream(s.getOutputStream());out.write(str.getBytes());System.out.println(str);out.flush();//到这里就执行不下去了DataInputStreamin=newDataInputStream(s.getInputStream());byte[]result=newbyte[100000];intlen;intthislength=0;inttotallength=0;do{thislength=in.read(result,totallength,2000);if(thislength>0){totallength+=thislength;}}while(in.available()>0);byte[]response=newbyte[totallength];System.arraycopy(result,0,response,0,totallength);StringresponseStr=newString(response);System.out.println("服务端接收:"+responseStr);out.close();in.close();i++;}}
如果把DataInputStreamin=newDataInputStream(s.getInputStream());byte[]result=newbyte[100000];intlen;intthislength=0;inttotallength=0;do{thislength=in.read(result,totallength,2000);if(thislength>0){totallength+=thislength;}}while(in.available()>0);byte[]response=newbyte[totallength];System.arraycopy(result,0,response,0,totallength);StringresponseStr=newString(response);System.out.println("服务端接收:"+responseStr);out.close();in.close();
这一段注释掉就能执行了但是不能接收啊求大家帮忙解决……
解决方案三:
引用1楼barrons的回复:
publicstaticvoidmain(String[]args)throwsException{inti=0;while(i<10){Sockets=newSocket("127.0.0.1",9052);Stringstr="客户端发送"+i;DataOutputStreamout=newDataOutputStream(s.getOutputStream());out.write(str.getBytes());System.out.println(str);out.flush();//到这里就执行不下去了DataInputStreamin=newDataInputStream(s.getInputStream());byte[]result=newbyte[100000];intlen;intthislength=0;inttotallength=0;do{thislength=in.read(result,totallength,2000);if(thislength>0){totallength+=thislength;}}while(in.available()>0);byte[]response=newbyte[totallength];System.arraycopy(result,0,response,0,totallength);StringresponseStr=newString(response);System.out.println("服务端接收:"+responseStr);out.close();in.close();i++;}}如果把DataInputStreamin=newDataInputStream(s.getInputStream());byte[]result=newbyte[100000];intlen;intthislength=0;inttotallength=0;do{thislength=in.read(result,totallength,2000);if(thislength>0){totallength+=thislength;}}while(in.available()>0);byte[]response=newbyte[totallength];System.arraycopy(result,0,response,0,totallength);StringresponseStr=newString(response);System.out.println("服务端接收:"+responseStr);out.close();in.close();
这一段注释掉就能执行了但是不能接收啊求大家帮忙解决……
这里的是我的测试用的客户端代码
解决方案四:
引用2楼barrons的回复:
Quote: 引用1楼barrons的回复:
publicstaticvoidmain(String[]args)throwsException{inti=0;while(i<10){Sockets=newSocket("127.0.0.1",9052);Stringstr="客户端发送"+i;DataOutputStreamout=newDataOutputStream(s.getOutputStream());out.write(str.getBytes());System.out.println(str);out.flush();//到这里就执行不下去了DataInputStreamin=newDataInputStream(s.getInputStream());byte[]result=newbyte[100000];intlen;intthislength=0;inttotallength=0;do{thislength=in.read(result,totallength,2000);if(thislength>0){totallength+=thislength;}}while(in.available()>0);byte[]response=newbyte[totallength];System.arraycopy(result,0,response,0,totallength);StringresponseStr=newString(response);System.out.println("服务端接收:"+responseStr);out.close();in.close();i++;}}如果把DataInputStreamin=newDataInputStream(s.getInputStream());byte[]result=newbyte[100000];intlen;intthislength=0;inttotallength=0;do{thislength=in.read(result,totallength,2000);if(thislength>0){totallength+=thislength;}}while(in.available()>0);byte[]response=newbyte[totallength];System.arraycopy(result,0,response,0,totallength);StringresponseStr=newString(response);System.out.println("服务端接收:"+responseStr);out.close();in.close();
这一段注释掉就能执行了但是不能接收啊求大家帮忙解决……
这里的是我的测试用的客户端代码
项目要求是要求字节流互相收发
解决方案五:
System.out.println("服务端接收:"+responseStr);
服务端能执行到这句么,感觉很悬呢
解决方案六:
thislength=in.read(result,totallength,2000);
你调试下,应该阻塞在这,这种交互你需要一个简单的协议,另外data装饰类没太大意义
解决方案七:
我记得thinkinginjava里说过available()得谨慎使用......还有dowhile...不是不推荐用吗DataInputStream是专门格式化读取的你怎么还用read()了......
解决方案八:
客户端10次循环,10个socket共用一个端口怎么可能
解决方案九:
引用7楼abcdefghiijklmnopqrs的回复:
客户端10次循环,10个socket共用一个端口怎么可能
好久没用socket了想多了.上面完全错了