Socket分包传输

Socket分包传输

不分包

 public static String getSocketString(String json,String ip,int port){
	    	String info = null;
	    	StringBuffer returnInfo = new StringBuffer();
	    	PrintWriter pw=null;
	    	OutputStream os =null;
	    	BufferedReader br =null;
	    	InputStream is=null;
	    	Socket socket=null;
	    	try {
				// 和服务器创建连接
	    		 
				socket = new Socket(ip,port);
				
				String s=lengjs(json.length());				
				
				String str=s+json;		
				//StringBuffer sb=new StringBuffer(str);
				// 要发送给服务器的信息
				log.debug("发送socket数据包"+str);
			
				os = socket.getOutputStream();
				
				pw = new PrintWriter(os);
				
 
				pw.write(str);
				
				Thread.sleep(1000);
				pw.flush();
				
				socket.shutdownOutput();
				
				// 从服务器接收的信息
				 is = socket.getInputStream();
				 br = new BufferedReader(new InputStreamReader(is));
				
				while((info = br.readLine())!=null){
					returnInfo.append(info);
					log.debug("客户端返回成功:"+info);				
				}				
				
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				try {
					if(br!=null)
					br.close();
					if(is!=null)
					is.close();
					if(os!=null)
					os.close();
					if(pw!=null)
					pw.close();
					if(socket!=null)
					socket.close();	
				} catch (IOException e) {					
					e.printStackTrace();
				}
				
			}
			return returnInfo.toString();		
	    }

分包

 public static String getSocketString1(String json,String ip,int port){
	    	String info = null;
	    	StringBuffer returnInfo = new StringBuffer();
	    	PrintWriter pw=null;
	    	OutputStream os =null;
	    	BufferedReader br =null;
	    	InputStream is=null;
	    	Socket socket=null;
	    	try {
				// 和服务器创建连接
	    		 // 与服务端建立连接
	            socket = new Socket(ip,port);
	            socket.setSoTimeout(10* 1000); 
	            byte[] byteDatas = json.getBytes("utf-8");
	           
				os = socket.getOutputStream();
				
				
	          //分包发送, 每包1024长度
	            int index = 0;
	            while (index < len)
	            {
	                byte[] buf;
	                if ((len -index) / 1024 > 0) {
	                    buf = new byte[1024];
	                    System.arraycopy(byteDatas, index, buf, 0, 1024);
	                }
	                else
	                {
	                    buf = new byte[len - index];
	                    System.arraycopy(byteDatas, index, buf, 0, len - index);
	                }
	                os.write(buf);
	                index = index + 1024;
	            }
				
				// 从服务器接收的信息
				is = socket.getInputStream();
				 br = new BufferedReader(new InputStreamReader(is));
				
				while((info = br.readLine())!=null){
					returnInfo.append(info);
					log.debug("客户端返回成功:"+info);				
				}			
				
			} catch (Exception e) {
				
				e.printStackTrace();
			}finally {
				try {
					if(br!=null)
					br.close();
					if(is!=null)
					is.close();
					if(os!=null)
					os.close();
					if(pw!=null)
					pw.close();
					if(socket!=null)
					socket.close();	
				} catch (IOException e) {					
					e.printStackTrace();
				}
				
			}
			return returnInfo.toString();		
	    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值