java簡單聊天程式碼

不設限發表於2011-12-10
客戶端:

package com.dada.handlefile;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author dada
 * 客戶端
 */
public class Client {
	public static void main(String[] args) throws Exception, IOException {
		Socket sClient = new Socket("localhost", 9999);
		InputStream is = sClient.getInputStream();
		DataInputStream dis = new DataInputStream(is);
		OutputStream os = sClient.getOutputStream();
		DataOutputStream dos = new DataOutputStream(os);

		new ClientReader(dis).start();
		new ClientWriter(dos).start();
	}
}

/**
 * @author dada
 * 客戶端讀取類
 */
class ClientReader extends Thread {
	DataInputStream dis;

	public ClientReader(DataInputStream dis) {
		super();
		this.dis = dis;
	}

	@Override
	public void run() {
		String info;
		while (true) {
			try {
				info = dis.readUTF();
				System.out.println("對方說: " + info);
				if (info.equals("bye")) {
					System.out.println("對方下線");
					System.exit(0);
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

/**
 * @author dada
 * 客戶端寫出類
 */
class ClientWriter extends Thread {
	DataOutputStream dos;

	public ClientWriter(DataOutputStream dos) {
		this.dos = dos;
	}

	@Override
	public void run() {
		String info;
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		while (true) {
			try {
				info = br.readLine();
				dos.writeUTF(info);
				if (info.equals("bye")) {
					System.out.println("自己下線");
					System.exit(0);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

伺服器端:

package com.dada.handlefile;

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

/**
 * @author dada
 *	伺服器端
 */
public class Server {
	public static void main(String[] args) throws Exception {
		//例項化一個伺服器端的socket例項
		ServerSocket ss = new ServerSocket(9999);
		Socket sServer = ss.accept();
		//獲取輸入流
		InputStream is = sServer.getInputStream();
		//封裝DataInputStream是因為它可以以UTF的格式讀取資料
		DataInputStream dis = new DataInputStream(is);
		//獲取輸出流
		OutputStream os = sServer.getOutputStream();
		//封裝DataInputStream是因為它可以以UTF的格式讀取資料
		DataOutputStream dos = new DataOutputStream(os);

		new ServerReader(dis).start();
		new ServerWriter(dos).start();

	}
}

/**
 * @author Administrator
 *	這個類主要是用來提供從伺服器端的流讀取資料的
 */
class ServerReader extends Thread {
	DataInputStream dis;

	public ServerReader(DataInputStream dis) {
		this.dis = dis;
	}

	@Override
	public void run() {
		String info;
		while (true) {
			try {
				info = dis.readUTF();
				System.out.println("對方說: " + info);
				if (info.equals("bye")) {
					System.out.println("對方下線: ");
					System.exit(0);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}
}


/**
 * @author Administrator
 *	這個方法主要是提供從伺服器端向外寫資料服務的
 */
class ServerWriter extends Thread {
	DataOutputStream dos;

	public ServerWriter(DataOutputStream dos) {
		super();
		this.dos = dos;
	}

	@Override
	public void run() {
		String info;
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		while (true) {
			try {
				info = br.readLine();
				dos.writeUTF(info);
				if (info.equals("bye")) {
					System.out.println("自動退出!");
					System.exit(0);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
}


 

相關文章