Java 聊天器 beta1

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;


public class ChatServer {
	boolean started = false;
	ServerSocket serverSocket = null;
	List<Client> clients = new ArrayList<Client>();
	
	public static void main(String[] args) {
		new ChatServer().start();
	}
	
	public void start() {
		try {
			serverSocket = new ServerSocket(7777);
			started = true;
		}
		catch(BindException e2) {
			System.out.println("端口使用中 ... ...");
			System.out.println("请关闭双关程序!");
			System.exit(0);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		try{
			while(started) {
			    Socket socket = serverSocket.accept();
			    Client client = new Client(socket);
				System.out.println("连接上了... ...");
				new Thread(client).start();
				clients.add(client);
			}
		} catch (IOException e) {
			System.out.println("Server 关闭");
		}finally {
	          try {
				serverSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}	
	}

	
	
	class Client implements Runnable {

		private Socket socket;
		private DataInputStream dis = null;
		private DataOutputStream dos = null;
		boolean bConnected = false;
		
		public Client(Socket socket) {
			this.socket = socket;
		
			try {
				dis = new DataInputStream(socket.getInputStream());
				dos = new DataOutputStream(socket.getOutputStream());
				bConnected = true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		public void send(String str) {
			try {
				dos.writeUTF(str);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		
		public void run() {
			try{
          	while(bConnected) {
          		String str = dis.readUTF();
          		System.out.println(str);
          		
          		for(int i = 0;i<clients.size();i++) {
          			Client c = clients.get(i);
          			c.send(str);
          		}
          		
          		
          	}	
			}catch (EOFException e) {
				System.out.println("Server 关闭");
			}catch(IOException e) {
				e.printStackTrace();
			}finally {
				try {
					if(dis != null){
						dis.close();
					}
					if(dos != null){
						dos.close();
					}
					if(socket != null){
						socket.close();
						socket = null;
					}
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		
	}
	
}

 

客户端 :

 

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class ChatClient extends Frame {
	
	Socket socket = null;
	TextField textField = new TextField();
	TextArea textArea = new TextArea();
	DataOutputStream dos = null;
	DataInputStream dis = null;
	boolean bContented = false;
	public void MainFrame() {
		setLocation(200, 200);
		setSize(400, 300);
		add(textArea,BorderLayout.NORTH);
		add(textField,BorderLayout.SOUTH);
		textField.addActionListener(new TextFieldListener());
		pack();
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				disConnect();
				System.exit(0);
			}
		});
		
		setVisible(true);
		connect();
		
		new Thread(new ClientTalk()).start();
	}
	
	private class TextFieldListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			String str = textField.getText();
			//textArea.setText(str);
			textField.setText("");
			try {
				dos.writeUTF(str);
				dos.flush();
				
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
		}
		
	}
	
	
	public void disConnect() {
		try {
			dos.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	public void connect() {
		try {
			socket = new Socket("127.0.0.1",7777);
			dos = new DataOutputStream(socket.getOutputStream());
			dis = new DataInputStream(socket.getInputStream());
			bContented = true;
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	class ClientTalk implements Runnable {
		public void run() {
			try{
			while(bContented) {
				String str = dis.readUTF();
				//System.out.println(str);
				textArea.setText(textArea.getText() + str + '\n');
				
			}
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static void main(String[] args) {
		
		new ChatClient().MainFrame();
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值