socket接收报错,求指导

rocrp 2013-10-27 08:10:01
/**
* 报文头固定5位为报文长度,按长度接收
* @param socket
* @return 接收到的报文
*/
public static byte[] accept(Socket socket){
byte [] head = new byte[5];
byte [] body=null;
try {
BufferedInputStream bufIn = new BufferedInputStream(socket.getInputStream());
bufIn.read(head);
System.out.println("head "+new String(head));
int len1 = Integer.parseInt(new String(head));
body = new byte[len1];
bufIn.read(body);
} catch (IOException e) {
e.printStackTrace();
}
return body;
}

客户端接收调用:
 byte[] res = SocketUtil.accept(socket);//读取服务器端返回bytes
String str=new String(res);//转化为String
str=ByteOrStringHelper.ByteToString(res,"GBK");//根据编码格式转化
System.out.println("读取服务器返回数据:"+str);

一接收就报错java.lang.NumberFormatException: For input string: " h"
搞不懂哪错了.接收报文不能2次接收数据吗?不能根据报文长度接收具体长度报文吗?还是其他地方错误.求指导!
...全文
139 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuweisyyc 2013-10-27
  • 打赏
  • 举报
回复
引用 11 楼 rocrp 的回复:
客户端:
/**
	 * 发送报文
	 * 
	 */
	private Socket socket;
	
	private int msg1_len=100;
	private int msg2_len=380;
	private String msg1=McisCall.formatString("gd239", msg1_len,0);//
	private String msg2=McisCall.formatString("ccc56", msg2_len,0);
	
	MSDate ms=new MSDate("txn121");
	
	 public void send() {
		 try{
			 socket=new Socket();
			 socket.connect(new InetSocketAddress("127.0.0.1",8088)); 	

			 socket.setTcpNoDelay(true);
			 System.out.println("客户端正在连接..."); 
			 byte[] bytes =ms.getDate(msg1+msg2, msg1_len+msg2_len);
			 String s=new String(bytes);
			 
			 System.out.println(s+" len "+s.length());
			 SocketUtil.send(socket, bytes);
			 System.out.println("发送数据成功");
			
			 byte[] res = SocketUtil.accept(socket);//读取服务器端返回bytes
			 System.out.println("刚读取的:"+res);
			 String str=new String(res);
			 System.out.println("Str之后:"+str);
			 str=ByteOrStringHelper.ByteToString(res,"GBK");
			 System.out.println("读取服务器返回数据:"+str); 
			 socket.close();
打印00585 txn121 12345677654321 gd239 ccc56 len 585 应该是对的.报文和设计一样. 服务器端就是上面的代码:打印结果为: txn121 12345677654321 gd239 ccc56 接收到的也是对的.我服务端是直接把接收到的报文返回给客户端的.好像就这里出问题
服务器发的不对啊,前面少了body长度,仔细debug下把
rocrp 2013-10-27
  • 打赏
  • 举报
回复
客户端:
/**
	 * 发送报文
	 * 
	 */
	private Socket socket;
	
	private int msg1_len=100;
	private int msg2_len=380;
	private String msg1=McisCall.formatString("gd239", msg1_len,0);//
	private String msg2=McisCall.formatString("ccc56", msg2_len,0);
	
	MSDate ms=new MSDate("txn121");
	
	 public void send() {
		 try{
			 socket=new Socket();
			 socket.connect(new InetSocketAddress("127.0.0.1",8088)); 	

			 socket.setTcpNoDelay(true);
			 System.out.println("客户端正在连接..."); 
			 byte[] bytes =ms.getDate(msg1+msg2, msg1_len+msg2_len);
			 String s=new String(bytes);
			 
			 System.out.println(s+" len "+s.length());
			 SocketUtil.send(socket, bytes);
			 System.out.println("发送数据成功");
			
			 byte[] res = SocketUtil.accept(socket);//读取服务器端返回bytes
			 System.out.println("刚读取的:"+res);
			 String str=new String(res);
			 System.out.println("Str之后:"+str);
			 str=ByteOrStringHelper.ByteToString(res,"GBK");
			 System.out.println("读取服务器返回数据:"+str); 
			 socket.close();
打印00585 txn121 12345677654321 gd239 ccc56 len 585 应该是对的.报文和设计一样. 服务器端就是上面的代码:打印结果为: txn121 12345677654321 gd239 ccc56 接收到的也是对的.我服务端是直接把接收到的报文返回给客户端的.好像就这里出问题
zhuweisyyc 2013-10-27
  • 打赏
  • 举报
回复
引用 9 楼 rocrp 的回复:
while(true){
				System.out.println("等待客户端连接");
				Socket socket=ss.accept();
				byte[] rec=SocketUtil.accept(socket);
				System.out.println("接受客户端请求成功");				
				System.out.println("服务器接受到客户端的连接请求:" + new String(rec));// 用于接收客户端 发来的数据的输入流  
				
				System.out.println("响应前Length"+rec.length);
				SocketUtil.send(socket, rec);//服务器响应给客户端  
				System.out.println("响应后");
				/*
				 * 启动一个线程去接待它
				 */
				Thread clientThread = new Thread(new Handler(socket));
				clientThread.start();
这是服务端线程循环的,一直响应
这样还是看不出来啊,能debug下send方法吗,看下最终write的是什么
rocrp 2013-10-27
  • 打赏
  • 举报
回复
while(true){
				System.out.println("等待客户端连接");
				Socket socket=ss.accept();
				byte[] rec=SocketUtil.accept(socket);
				System.out.println("接受客户端请求成功");				
				System.out.println("服务器接受到客户端的连接请求:" + new String(rec));// 用于接收客户端 发来的数据的输入流  
				
				System.out.println("响应前Length"+rec.length);
				SocketUtil.send(socket, rec);//服务器响应给客户端  
				System.out.println("响应后");
				/*
				 * 启动一个线程去接待它
				 */
				Thread clientThread = new Thread(new Handler(socket));
				clientThread.start();
这是服务端线程循环的,一直响应
zhuweisyyc 2013-10-27
  • 打赏
  • 举报
回复
引用 7 楼 rocrp 的回复:
[quote=引用 5 楼 zhuweisyyc 的回复:] [quote=引用 4 楼 rocrp 的回复:] [quote=引用 3 楼 zhuweisyyc 的回复:] [quote=引用 2 楼 rocrp 的回复:] [quote=引用 1 楼 zhuweisyyc 的回复:] 客户端度到的是字符串,字符串转换出错了,不能讲 “ h”转换为Int。 int len1 = Integer.parseInt(new String(head));
不是这个原因,我设计报文头5位是长度.再根据这个长度来接收整体报文返回字符串数组.现在是他不按照我的代码来返回.是不是socket不能2次接收body内容?[/quote] BufferedInputStream 这个已经封装过了socket的输入流,读出来的直接就是字符串了,你不能再转Int了。[/quote] 我是看这博文http://blog.csdn.net/liuc0317/article/details/6587481这么写的 那接收要怎么接收?在不确定报文长度的情况下.[/quote] 这个的意思是服务端跟客户端约定的特殊报文吧,头部先传5个字节代表我body的总长度,然后通过这个长度再度body的内容,你的服务端发的是什么内容,没有按照这个格式发,所以接受出错了, 你的前5个字节 读出来是 “ h”,这个是不对的,所以问题出在发送的地方。[/quote] 发送报文应该是对的.头5位数字为报文总长度.后面为具体要发送的报文主体, 00121 hxn121 12345677654321 类是这样的.应该是符合要求的吧.但他就是会读到后面的h[/quote] 能把你服务端的代码贴下吗,这样看不出来,但可以肯定问题就在这里。
rocrp 2013-10-27
  • 打赏
  • 举报
回复
引用 5 楼 zhuweisyyc 的回复:
[quote=引用 4 楼 rocrp 的回复:] [quote=引用 3 楼 zhuweisyyc 的回复:] [quote=引用 2 楼 rocrp 的回复:] [quote=引用 1 楼 zhuweisyyc 的回复:] 客户端度到的是字符串,字符串转换出错了,不能讲 “ h”转换为Int。 int len1 = Integer.parseInt(new String(head));
不是这个原因,我设计报文头5位是长度.再根据这个长度来接收整体报文返回字符串数组.现在是他不按照我的代码来返回.是不是socket不能2次接收body内容?[/quote] BufferedInputStream 这个已经封装过了socket的输入流,读出来的直接就是字符串了,你不能再转Int了。[/quote] 我是看这博文http://blog.csdn.net/liuc0317/article/details/6587481这么写的 那接收要怎么接收?在不确定报文长度的情况下.[/quote] 这个的意思是服务端跟客户端约定的特殊报文吧,头部先传5个字节代表我body的总长度,然后通过这个长度再度body的内容,你的服务端发的是什么内容,没有按照这个格式发,所以接受出错了, 你的前5个字节 读出来是 “ h”,这个是不对的,所以问题出在发送的地方。[/quote] 发送报文应该是对的.头5位数字为报文总长度.后面为具体要发送的报文主体, 00121 hxn121 12345677654321 类是这样的.应该是符合要求的吧.但他就是会读到后面的h
rocrp 2013-10-27
  • 打赏
  • 举报
回复
我要发送的数据是:00121 hxn121 12345677654321 头5位是长度, 中间空5位再是一个字符串.这应该没错吧.接收就是121个字节数组.他好像直接找了后面字符串了.
zhuweisyyc 2013-10-27
  • 打赏
  • 举报
回复
引用 4 楼 rocrp 的回复:
[quote=引用 3 楼 zhuweisyyc 的回复:] [quote=引用 2 楼 rocrp 的回复:] [quote=引用 1 楼 zhuweisyyc 的回复:] 客户端度到的是字符串,字符串转换出错了,不能讲 “ h”转换为Int。 int len1 = Integer.parseInt(new String(head));
不是这个原因,我设计报文头5位是长度.再根据这个长度来接收整体报文返回字符串数组.现在是他不按照我的代码来返回.是不是socket不能2次接收body内容?[/quote] BufferedInputStream 这个已经封装过了socket的输入流,读出来的直接就是字符串了,你不能再转Int了。[/quote] 我是看这博文http://blog.csdn.net/liuc0317/article/details/6587481这么写的 那接收要怎么接收?在不确定报文长度的情况下.[/quote] 这个的意思是服务端跟客户端约定的特殊报文吧,头部先传5个字节代表我body的总长度,然后通过这个长度再度body的内容,你的服务端发的是什么内容,没有按照这个格式发,所以接受出错了, 你的前5个字节 读出来是 “ h”,这个是不对的,所以问题出在发送的地方。
rocrp 2013-10-27
  • 打赏
  • 举报
回复
引用 3 楼 zhuweisyyc 的回复:
[quote=引用 2 楼 rocrp 的回复:] [quote=引用 1 楼 zhuweisyyc 的回复:] 客户端度到的是字符串,字符串转换出错了,不能讲 “ h”转换为Int。 int len1 = Integer.parseInt(new String(head));
不是这个原因,我设计报文头5位是长度.再根据这个长度来接收整体报文返回字符串数组.现在是他不按照我的代码来返回.是不是socket不能2次接收body内容?[/quote] BufferedInputStream 这个已经封装过了socket的输入流,读出来的直接就是字符串了,你不能再转Int了。[/quote] 我是看这博文http://blog.csdn.net/liuc0317/article/details/6587481这么写的 那接收要怎么接收?在不确定报文长度的情况下.
zhuweisyyc 2013-10-27
  • 打赏
  • 举报
回复
引用 2 楼 rocrp 的回复:
[quote=引用 1 楼 zhuweisyyc 的回复:] 客户端度到的是字符串,字符串转换出错了,不能讲 “ h”转换为Int。 int len1 = Integer.parseInt(new String(head));
不是这个原因,我设计报文头5位是长度.再根据这个长度来接收整体报文返回字符串数组.现在是他不按照我的代码来返回.是不是socket不能2次接收body内容?[/quote] BufferedInputStream 这个已经封装过了socket的输入流,读出来的直接就是字符串了,你不能再转Int了。
rocrp 2013-10-27
  • 打赏
  • 举报
回复
引用 1 楼 zhuweisyyc 的回复:
客户端度到的是字符串,字符串转换出错了,不能讲 “ h”转换为Int。 int len1 = Integer.parseInt(new String(head));
不是这个原因,我设计报文头5位是长度.再根据这个长度来接收整体报文返回字符串数组.现在是他不按照我的代码来返回.是不是socket不能2次接收body内容?
zhuweisyyc 2013-10-27
  • 打赏
  • 举报
回复
客户端度到的是字符串,字符串转换出错了,不能讲 “ h”转换为Int。 int len1 = Integer.parseInt(new String(head));

62,614

社区成员

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

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