今天学习了老师讲的java的第一个小项目,是一个聊天程序雏形。
一个服务器端ChatServer.java,一个是客户端ChatClient.java
监听的是18881接口。
此小程序的截图如下,有兴趣的可以看一下
java聊天室
ChatServer.java的源代码如下:

import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer
{
	boolean started = false;
	ServerSocket ss = null;
	
	List<Client> clients = new ArrayList<Client>();

	public static void main(String[] args){
		new ChatServer().start();
	}
		
	public void start(){
		try	{
			ss = new ServerSocket(18881);
			started = true;
			System.out.println("This is 18881 chatting room chatting history");
		} catch (BindException b) {
			System.out.println("端口使用中");
			System.out.println("请关掉相关程序并重新运行服务器");
			System.exit(0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try	{
			int i = 1;
			while(started) {
				Socket s = ss.accept();
				Client c = new Client(s);
				System.out.println(i + " chatter connected,it's IP:" + s.getInetAddress() + ",it's port:" + s.getPort());
				i++;
				//一个客户端启动一个对应的服务器线程
				new Thread(c).start();
				clients.add(c);
			}
		} catch (IOException e)	{
			System.out.println("a chatter out");
		}finally {
			try {
				ss.close();
			} catch (IOException i){
				i.printStackTrace();
			}
		}
	}
	
	class Client implements Runnable {
		private Socket s = null;
		private DataInputStream dis = null;
		private DataOutputStream dos = null;
		private boolean bConnected = false;
		public Client(Socket s){
			this.s = s;
			try {
				dis = new DataInputStream(s.getInputStream());
				dos = new DataOutputStream(s.getOutputStream());
				bConnected = true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		public void send(String str){
			try {
				dos.writeUTF(str);
			} catch (IOException e) {
				clients.remove(this);
				System.out.println("一个client退出了,我将它去除了");
			}
		}
		
		public void run() {
			Client c = null;
			try{
				while (bConnected) {
					String str=dis.readUTF();
					System.out.println(s.getPort() + ":" + str);
					for(int i=0;i<clients.size();i++){
						c=clients.get(i);
						c.send(s.getPort() + ":" + str);
					}
				}
			} catch (SocketException s) {
				System.out.println("quit");
			} catch (EOFException e){
				System.out.println("a quit");
			}catch (IOException e)	{
				System.out.println("a client quit");
			} finally {
				try {
					if(dis != null) dis.close();
					if(dos != null) dos.close();
					if(s != null) s.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}	
		}
	}
}

ChatClient.java的源代码如下:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class ChatClient extends Frame
{
	TextField tf;
	TextArea ta;
	Socket s = null;
	DataOutputStream dos = null;
	DataInputStream dis=null;
	private boolean bConnected = false;
	public static void main(String[] args){
		new ChatClient().LaunchFrame();
	}

	public void LaunchFrame(){
		setLocation(100,100);
		setSize(400,300);
		tf = new TextField();
		ta = new TextArea();
		add(tf,BorderLayout.SOUTH);
		add(ta,BorderLayout.NORTH);
		pack();
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				disconnect();
				System.exit(0);
			}
		});
		tf.addActionListener(new TFListener());
		setVisible(true);
		connect();
		
		new Thread(new RecvThread()).start();
	}

	private void connect(){
		try
		{
			s = new Socket("127.0.0.1",18881);
			dos = new DataOutputStream(s.getOutputStream());
			dis = new DataInputStream(s.getInputStream());
			ta.setText("-----welcome to 18881 chatting room-----" + '\n');
			
			bConnected = true;
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void disconnect(){
		try{
			dos.close();
			s.close();
		} catch (IOException i) {
			i.printStackTrace();
		}
		
	}


	class TFListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e){
			String str=tf.getText();
			//ta.setText(str);
			tf.setText("");
			try {
				dos.writeUTF(str);
				dos.flush();
				//System.out.println(dis.readUTF());
				//dos.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	private class RecvThread implements Runnable{
		public void run(){
			try {
				while(bConnected){
					String str = dis.readUTF();
					ta.setText(ta.getText() + str + '\n');
				}
			} catch (SocketException s) {
				System.out.println("退");
			} catch (EOFException e){
				System.out.println("退出");
			}catch (IOException e)	{
				System.out.println("退出了");
			}
			
		}
	}
}

随机文章

android4.0下使用yesvpn访问facebook
android4.0下使用yesvpn访问facebook

ubuntu终端“确定”窗口无法点击
ubuntu终端“确定”窗口无法点击

鼠标右键添加“新建.java源文件”
鼠标右键添加“新建.java源文件”

财猫省钱浏览器使用心得
财猫省钱浏览器使用心得

adobe将photoshop搬到了移动设备
adobe将photoshop搬到了移动设备

相关文章

windows最新版chrome出现卡顿?
windows最新版chrome出现卡顿?

javascript让图片来回移动
javascript让图片来回移动

ubuntu终端“确定”窗口无法点击
ubuntu终端“确定”窗口无法点击

联通过年放福利,给大家500M的流量红包
联通过年放福利,给大家500M的流量红包

Java基础第五讲:流程控制(一)
Java基础第五讲:流程控制(一)

IOS版One Today上线
IOS版One Today上线

内容分享:道招
本文链接:j2se java编写简易聊天室程序
道招声明:除特别标注或作者不详外,本站所有文章均为原创,转载请注明。欢迎共同关注互联网!