首页>代码>netty4解决粘包的项目实例>/SocketServer/com/EchoServer.java
package com;
import java.net.*;
import java.io.*;



public class EchoServer {
	 private int port=8991;
	    private ServerSocket serverSocket;
	    
	    public EchoServer() throws IOException{
	        serverSocket=new ServerSocket(port);
	        System.out.println("服务器启动");
	        }
	    public String echo(String msg){
	        return "echo:"+msg;
	    }
	    public PrintWriter getWriter(Socket socket)throws IOException{
	        OutputStream socketOut=socket.getOutputStream();
	        return new PrintWriter(socketOut,true);
	    }
	    public BufferedReader getReader(Socket socket)throws IOException{
	        InputStream socketIn=socket.getInputStream();
	        return new BufferedReader(new InputStreamReader(socketIn));
	    }
	    public void service(){
			   String msg = "";
			   DataInputStream in = null;
			   String str_msg[] = null;
		     try{ 	  
		        /*
		         * 多线程处理. 每n_count条启用一个线程
		         */   
		    	 
		    	
		    	  Socket socket=null;
		    	  boolean flag = true;
		          try{
		        	  while(flag)
		        	  {
			              socket=serverSocket.accept();//等待客户端连接
			              System.out.println("接受新的联络:"
			                                      +socket.getInetAddress()+":"
			                                      +socket.getPort());
			               in  = new DataInputStream(socket.getInputStream());
			               //在默认的vm arguments下,byte数组的最大长度是61858764,将近60M。
			               byte b[] = new byte[40960];
			               while(in.read(b)!=-1)
		                  {	
				              msg = new String (b,"GBK");
				              msg = msg.toString().trim();
			                  System.out.println("接收数据....."+msg);
			                  str_msg = msg.split(";");
			                  for(int i=0;i<str_msg.length;i++)
			                  {
			                	  System.out.println("拆分后的数据....."+str_msg[i]);
			                  }
		                  }
		                 
		              }
		          }catch(IOException e){e.printStackTrace();
		          }finally{
		              try{
		                  if(socket!=null)
		                  {
		                      socket.close();
		                  }
		                  if(in!=null)
		                  {
		                	  in.close();
		                  }
		                  
		                  
		                     
		              }catch(IOException e){e.printStackTrace();
		              }
		          }
		      
		     }
		     catch(Exception ex){        	 
		         ex.printStackTrace();
		         
		     }
		}
	 

	    public static void main(String[] args) throws IOException {
	        // TODO Auto-generated method stub
	        new EchoServer().service();
	    }
}
最近下载更多
jaonsang  LV25 2022年4月8日
13021603815  LV7 2021年4月12日
yangctz  LV25 2021年1月2日
bian21bian  LV2 2020年8月24日
lililililiapi  LV1 2019年7月6日
dagf113225  LV68 2019年1月29日
494821341  LV1 2018年12月12日
1847731288  LV13 2018年8月18日
1943815081  LV13 2018年4月13日
369647247  LV9 2018年4月10日
最近浏览更多
1909741473  LV8 2024年2月19日
sunlzh888888  LV29 2023年9月12日
yinyun1985  LV14 2022年10月31日
jaonsang  LV25 2022年4月7日
ericxu1116  LV24 2021年5月20日
13021603815  LV7 2021年4月12日
aloner_joker 2021年4月3日
暂无贡献等级
yangctz  LV25 2021年1月2日
wxm4252  LV12 2020年12月18日
名字随便的啦啦  LV7 2020年10月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友