Socket编程 基本的聊天实现。

public class Server
{

	//用来存储所有连接上来的客户
	private List<ServerThread> clients;
	
	public static void main(String[] args)
	{
		Server s = new Server();
		s.startServer(9988);
	}

	public void startServer(int port)
	{
		ServerSocket ss = null;
		Socket socket = null;
		try
		{
			ss = new ServerSocket(port);
			clients=new ArrayList<ServerThread>();
			while (true)
			{
				System.out.println("waiting...");
				socket = ss.accept();
				ServerThread st = new ServerThread(socket);
				new Thread(st).start();
			}
		}
		catch (IOException e)
		{

			e.printStackTrace();
		}
		finally
		{
			SocketUtils.closeSocketServer(ss);

		}
	}

	private class ServerThread implements Runnable
	{

		private Socket s = null;
		private BufferedReader br;
		private PrintWriter out;
		private String address;
		private String username;

		private boolean flag;

		public ServerThread(Socket s) throws IOException
		{
			this.s = s;
			br = new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8"));
			out = new PrintWriter(s.getOutputStream(),true);
			
			username=br.readLine();
			System.out.println("username="+username);
			
			address = s.getInetAddress().getHostAddress() + ":" + s.getPort();
		//	System.out.println(name + "连接上来了");
			clients.add(this);
			send(username+"上线了");
		}

		private void stop()
		{
			System.out.println(username+"已经离开了");
			clients.remove(this);
			flag = false;
		}

		
		/**
		 * 接收用户的信息
		 */
		private void receive()
		{
			String str = null;
			try
			{
				while ((str = br.readLine()) != null)
				{
					if (str.equalsIgnoreCase("quit"))
					{
						// 关闭
						stop();
						out.println("disconnect");
						break;
					}
					
					String sendMsg=username+":"+str;
					
				//	out.println(username+":"+ str);
					
					//给所有的用户发送信息
					send(sendMsg);
					
					System.out.println("out");
				}
			}
			catch (IOException e)
			{
				stop();
				System.out.println("用户非正常死亡->"+username);
			}finally{
				SocketUtils.closeSocket(s);
			}
		}
		
		
		/**
		 * 给用户发信息
		 * @param msg
		 */
		private void send(String msg){
			for(ServerThread st:clients){
				st.out.println(msg);
			}
		}
		

		public void run()
		{
			// if(!flag)break;
			receive();

		}

	}

}




public class Client
{
	
	private Socket s;
	private BufferedReader br;
	private PrintWriter out;
	
	private boolean flag=true;
	
	public static void main(String[] args)
	{	
		Client c=new Client();
	 //	c.sendMessage();
		c.startup();
	}
	
	
	public void startup(){
		BufferedReader sbr=null;
		
		try
		{
			s=new Socket("127.0.0.1",9988);
			//s=new Socket("115.28.240.213",9988);
			out=new PrintWriter(s.getOutputStream(),true);
			out.println("myname");
			br=new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8"));
			
			sbr=new BufferedReader(new InputStreamReader(System.in));
			new Thread(new ClientThread()).start();
			String str=null;
			
			while((flag)&&(str=sbr.readLine())!=null){
				if(!flag)break;
				String msg=new String(str.getBytes("gbk"),"utf-8");
				out.println(msg);
			}
			
		}
		catch (UnknownHostException e)
		{
			
			e.printStackTrace();
		}
		catch (IOException e)
		{
			
			e.printStackTrace();
		}
	}
	
	

	 
	private void closeSocket(Socket s){
		if(s!=null){
			try
			{
				s.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	private void receive(){
		try
		{
			String rs=br.readLine();
			if(rs.equalsIgnoreCase("disconnect")){
				flag=false;
				System.out.println("按回车退出");
				return ;
			}
			System.out.println(rs);
		}
		catch (IOException e)
		{
			
			e.printStackTrace();
		}
	}
	
	
	private class ClientThread implements Runnable{
		
		
		
		@Override
		public void run()
		{
			while(true){
				if(!flag)break;
				receive();
			}
		}
		
	}
	 
}

猜你喜欢

转载自alleni123.iteye.com/blog/2154830
今日推荐