Java网络编程---回显聊天

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {

	private int port=8080;
	private ServerSocket esSocket;

	private int count=1;
	
	public EchoServer() throws IOException{
		esSocket=new ServerSocket(port);
		System.out.println("EchoServer 服务器启动...");
		//System.out.println(esSocket.getLocalSocketAddress()+" : "+esSocket.getLocalPort());
	}

	
	public void service(){
		while(true){
			Socket socket=null;
			System.out.println("Server start");
			
			try{
				socket=esSocket.accept();
				//System.out.println("New connect : "+count);
				Thread workthread=new Thread( new Handler(socket));
				workthread.start();
				
			}catch(IOException e){
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			new EchoServer().service();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

}


class Handler implements Runnable{
	private Socket socket;
	String msg=null;
	public Handler(Socket socket){
		
		this.socket=socket;
	}
	
	private PrintWriter getWriter(Socket socket) throws IOException{
		OutputStream socketOut =socket.getOutputStream();
		return new PrintWriter(socketOut,true);
	}
	
	private BufferedReader getReader(Socket socket) throws IOException{
		InputStream socketIn=socket.getInputStream();
		return new BufferedReader(new InputStreamReader(socketIn));
	}
	
	public void run(){
		try {
			System.out.println("New connect : "+socket.getInetAddress()+" : "+socket.getPort());
			//System.out.println("Local "+socket.getLocalSocketAddress()+" : "+socket.getLocalPort());
			BufferedReader br=getReader(socket);
			PrintWriter pw=getWriter(socket);
			
			while( (msg=br.readLine()) !=null){
				System.out.println("receive : "+msg);
				pw.println("echo :"+msg);
				
				if( msg.equals("bye")==true ){
					System.out.println("======== exit ========");
					//flag=true;
					socket.close();
					break;
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				if(socket !=null){
					socket.close();
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			
		}	
	}
	
}



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;


public class EchoClient {


	private String host="localhost";
	private String remoteHost="109.130.246.96";
	private String remoteBaidu="http:\\www.baidu.com";
	private int port=8080;
	private Socket ecSocket;
	private Boolean flag=false;
	
	public EchoClient() throws IOException{
		System.out.println("==== client start ");
		System.out.println("==== client try to connect server ");
		ecSocket=new Socket(host,port);
		
	}
	
	private PrintWriter getWriter(Socket socket)throws IOException{
		OutputStream socketOut=socket.getOutputStream();
		return new PrintWriter(socketOut,true);
	}
	
	private BufferedReader getReader(Socket socket)throws IOException{
		InputStream socketIn =socket.getInputStream();
		return new BufferedReader(new InputStreamReader(socketIn));
	}
	
	public void talk() throws IOException{
		String msg=null;
		
		while(true){
			flag=false;
			try{
				BufferedReader br=getReader(ecSocket);
				PrintWriter pw=getWriter(ecSocket);
				BufferedReader localReader=new BufferedReader(new InputStreamReader(System.in));
				
				while( ( msg=localReader.readLine())!=null){
			
					System.out.println("send :"+msg);
					pw.println(msg);
					System.out.println(br.readLine());
					if(msg.equals("bye")){
						flag=true;
						System.out.println("======== exit ========");
						break;
					}
				}
			}catch(IOException e ){
				e.printStackTrace();
			}finally {
				try{
					//if(ecSocket==null)
						ecSocket.close();
				}catch(IOException e){
					e.printStackTrace();
				}
			}
			if(flag)
				break;
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			new EchoClient().talk();
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}


}


你可能感兴趣的:(Java)