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

}

 

posted on 2015-05-06 14:52  张飞_  阅读(7928)  评论(1编辑  收藏  举报

导航