• java 记录一下socket的server与client


    package com.test.server;
    
    import java.io.IOException;
    import java.net.ServerSocket;
    
    import org.apache.log4j.Logger;
    
    public class Server{
    	
    	private static final Logger logger = Logger.getLogger(Server.class);
    
    	public static ServerSocket server = null;	
    	
    	static{
    		try {
    			server = new ServerSocket(5678);
    		} catch (IOException e) {
    			logger.error("Server set up error: ", e); 
    		}   
    	}
    	
    	public static void stop(){
    		if(server != null){
    			try {
    				server.close();
    			} catch (IOException e) {
    				logger.error("Server close error: ", e); 
    			}
    		}
    	}
    	
    	public static void main(String[] args) {
    		Server.stop();
    	}
    	
    }
    
    package com.test.server;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.net.Socket;
    
    import org.apache.log4j.Logger;
    
    public class ServerThread implements Runnable {
    
    	private static final Logger logger = Logger.getLogger(ServerThread.class);
    
    	private Socket socket;
    
    	public ServerThread(Socket socket) {
    		this.socket = socket;
    	}
    
    	@Override
    	public void run() {
    //		 String line;
    		BufferedReader is = null;
    		Writer writer = null;
    		if (socket == null) {
    			logger.error("Server down!");
    			return;
    		}
    		StringBuilder sb = new StringBuilder();
    		try {
    			is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    			String temp;
    			int index;
    			temp = is.readLine();
    			System.out.println("line is: "+ temp);
    			while (temp != null) {
    				System.out.println(temp);
    				if ((index = temp.indexOf("eof")) != -1) {// 遇到eof时就结束接收
    					sb.append(temp.substring(0, index));
    					break;
    				}
    				sb.append(temp);
    				temp = is.readLine();
    			}
    			// logger.info(sb.toString());
    			System.out.println(sb.toString());
    			writer = new OutputStreamWriter(socket.getOutputStream());
    			writer.write("Hello Client.");
    			writer.write("eof
    ");
    			writer.flush();
    		} catch (IOException e) {
    			logger.error("Read information error: ", e);
    		} finally {
    			try {
    				if (is != null) {
    					is.close();
    				}
    				if (writer != null) {
    					writer.close();
    				}
    				if (socket != null) {
    					socket.close();
    				}
    			} catch (IOException e) {
    				logger.error("Close the IO session error: ", e);
    			}
    
    		}
    	}
    
    }
    
    package com.test.server;
    
    import java.io.IOException;
    import java.net.Socket;
    
    import org.apache.log4j.Logger;
    
    public class SocketServer {
    
    	private static final Logger logger = Logger.getLogger(SocketServer.class);	
    	
    	public void connect() {          
            try {                       
                while (true) {              	
                    Socket socket = Server.server.accept();  
    //                socket.getRemoteSocketAddress();
                    new Thread(new ServerThread(socket)).start();  
                }  
            } catch (IOException e) {  
            	logger.error("Server set up error: ", e);  
            }  
      
        } 	
    	
    	public static void main(String[] args) {
    		SocketServer ss = new SocketServer();
    		ss.connect();		
    	}
    
    }
    
    package com.test.client;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import org.apache.log4j.Logger;
    
    public class SocketClient {
    
    	private static final Logger logger = Logger.getLogger(SocketClient.class);
    
    	public void client() {
    		Socket client = null;
    		BufferedReader is = null;
    		Writer writer = null;
    		try {
    			client = new Socket("172.16.13.41", 5678);
    
    			writer = new OutputStreamWriter(client.getOutputStream());
    			writer.write("Hello Server.");
    			writer.write("eof
    ");
    			writer.flush();
    
    			is = new BufferedReader(new InputStreamReader(client.getInputStream()));
    			StringBuffer sb = new StringBuffer();
    			String temp;
    			int index;
    			while ((temp = is.readLine()) != null) {
    				if ((index = temp.indexOf("eof")) != -1) {
    					sb.append(temp.substring(0, index));
    					break;
    				}
    				sb.append(temp);
    			}
    			// logger.info(sb.toString());
    			System.out.println(sb.toString());
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (is != null) {
    					is.close();
    				}
    				if (writer != null) {
    					writer.close();
    				}
    				if (client != null) {
    					client.close();
    				}
    			} catch (IOException e) {
    				logger.error("Close the IO session error: ", e);
    			}
    		}
    
    	}
    
    	public static void main(String[] args) {
    		SocketClient sc = new SocketClient();
    		sc.client();
    	}
    
    }
    
  • 相关阅读:
    Threading in C# Learining
    win7 设置IIS
    Aforge视频采集
    C# 删除文件夹下的所有文件
    C# Timer实现实时监听
    Java中堆内存和栈内存详解
    彻底掌握 AQS
    四种线程同步/互斥方式小结
    CyclicBarrier可重用的循环栅栏
    答疑解惑之ExecutorService——shutdown方法和awaitTermination方法使用
  • 原文地址:https://www.cnblogs.com/zhangfei/p/4482022.html
Copyright © 2020-2023  润新知