Socket编程服务端和客户端互相通信,实现简单的聊天

客户端两个线程分别记录读和写,服务端两个线程分别记录读和写;

客户端代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 * 读取服务器的信息
 * 
 * @author Mr.Gao
 * 
 */
class ClientRead extends Thread {
	private Socket client;

	public ClientRead(Socket client) {
		this.client = client;
	}

	@Override
	public void run() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
			String line = "";
			while (true) {
				line = br.readLine();
				if (!"".equals(line) && line != null) {
					System.out.println("服务器说:" + line);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

/**
 * 读键盘,写到服务器
 * @author Administrator
 */
class ClientWrite extends Thread {
	private Socket client;

	public ClientWrite(Socket client) {
		this.client = client;
	}

	@Override
	public void run() {
		BufferedReader input = new BufferedReader(new InputStreamReader(
				System.in));
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
					client.getOutputStream()));
			String str = "";
			while (true) {
				str = input.readLine();
				bw.write(str);
				bw.newLine();
				bw.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

public class ClientChat {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Socket client = new Socket("127.0.0.1", 9988);
		System.out.println("客户端已经建立。。。");
		// 1.启动线程用于写给服务器
		new ClientRead(client).start();
		// 2.启动线程用于读取服务器的数据
		new ClientWrite(client).start();

	}

}

服务端代码:

package org.mobiletrain.demo05;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 读客户端的数据:socket中输入流,读,打印输出
 * 
 * @author Mr.Gao
 * 
 */
class ServerRead extends Thread {
	private Socket s;

	public ServerRead(Socket s) {
		this.s = s;
	}

	@Override
	public void run() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
			String line = "";
			while (true) {
				line = br.readLine();
				if (!"".equals(line) && line != null) {
					System.out.println("客户端说:" + line);
				}
			}
		} catch (Exception e) {

		}
	}
}

/**
 * 向客户端写数据:socket中获取输出流,读键盘,写给客户端
 * 
 * @author Mr.Gao
 * 
 */
class ServerWrite extends Thread {
	private Socket socket;

	public ServerWrite(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		// 1.获取流
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			String str = "";
			// 2.循环向客户端写数据
			while (true) {
				str = input.readLine();
				bw.write(str);
				bw.newLine();
				bw.flush();
			}
		} catch (Exception e) {

		}
	}
}

public class ServersChat {
	public static void main(String[] args) throws Exception {
		ServerSocket server = new ServerSocket(9988);
		System.out.println("服务器已经建立,,等待客户端连接。。");
		Socket socket = server.accept();
		System.out.println("已有客户端连接。。。");
		// 读,或者写
		// 启动线程,用于读客户端的数据
		new ServerRead(socket).start();
		// 启动线程,用于向客户端写数据
		new ServerWrite(socket).start();
	}
}
效果如下:

Socket编程服务端和客户端互相通信,实现简单的聊天_第1张图片

Socket编程服务端和客户端互相通信,实现简单的聊天_第2张图片

你可能感兴趣的:(javaSe)