socket 客户端分包传输

socket TCP在网上都能查到,这里就不多说了,
socket tcp协议在传输base64图片过大时会发生丢包的情况,
这里讲一下如何分包

1、不分包的情况

 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();		
	    }
		

2、分包的情况

 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();		
	    }

 

你可能感兴趣的:(java基础)