Socket通信,客户端和服务的互相收发------客户端接收代码块执行不了

barrons 2014-11-21 09:27:46
public static void main(String[] args) throws Exception {



ServerSocket ss = new ServerSocket(9052);
System.out.println("开始等待某个连接");

do{
Socket s = ss.accept();
System.out.println("获取到一个连接");

DataInputStream in = new DataInputStream(s.getInputStream());
byte[] result =new byte[100000];

int thislength = 0;
int totallength= 0;
do {
thislength = in.read(result, totallength, 2000);

if(thislength>0)
{
totallength +=thislength;
}

} while (thislength>0);

byte[] response = new byte[totallength];

System.arraycopy(result, 0, response, 0, totallength);

String responseStr = new String(response);

System.out.println("服务端接收:" + responseStr);

String str="服务端发送"+responseStr;
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.write(str.getBytes());
System.out.println(str);
out.flush();

}while(true);


// ss.close();
}



这是我测试用的服务端的代码 ,能收发
...全文
275 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 7 楼 abcdefghiijklmnopqrs 的回复:
客户端10次循环,10个socket共用一个端口怎么可能
好久没用socket了想多了.上面完全错了
  • 打赏
  • 举报
回复
客户端10次循环,10个socket共用一个端口怎么可能
W-Aires 2014-11-21
  • 打赏
  • 举报
回复
thislength = in.read(result, totallength, 2000);
你调试下,应该阻塞在这,这种交互你需要一个简单的协议,另外data装饰类没太大意义
W-Aires 2014-11-21
  • 打赏
  • 举报
回复

System.out.println("服务端接收:" + responseStr);
服务端能执行到这句么,感觉很悬呢
kainever 2014-11-21
  • 打赏
  • 举报
回复
我记得thinking in java 里说过 available ()得谨慎使用...... 还有 do while ... 不是不推荐用吗 DataInputStream是专门格式化读取的你怎么还用 read() 了......
barrons 2014-11-21
  • 打赏
  • 举报
回复
引用 2 楼 barrons 的回复:
[quote=引用 1 楼 barrons 的回复:]
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();
				
这一段注释掉就能执行了 但是不能接收啊 求大家帮忙解决……
这里的是我的测试用的客户端代码[/quote] 项目要求是要求 字节流 互相收发
barrons 2014-11-21
  • 打赏
  • 举报
回复
引用 1 楼 barrons 的回复:
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();
				
这一段注释掉就能执行了 但是不能接收啊 求大家帮忙解决……
这里的是我的测试用的客户端代码
barrons 2014-11-21
  • 打赏
  • 举报
回复
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();
				
这一段注释掉就能执行了 但是不能接收啊 求大家帮忙解决……

62,614

社区成员

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

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