JAVA网络编程实战应用——双向聊天(线程)

控制台双向聊天

    • Code

Code

以下如果有些流不懂的话,看我之前关于IO流的文章
要先启动服务端(Server),再启动客户端(Client)
必须要保证端口一致
Socket socket = new Socket(“localhost”,9999);
主机我写的是localhost,也就是本机

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
public class Client {
//	static Socket socket = null;
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket socket = new Socket("localhost",9999);
		//读取服务端发送过来的数据
		new ClientReadThread(socket.getInputStream()).start();
		//发送给服务端数据
		InputStream in = System.in;
		new ClientWriteThread(in, socket).start();
		//以下我也写了一个线程,所以注释了
		/*int len;
		byte[] byt = new byte[1024];
		while ((len = in.read(byt)) != -1) {
			//发送数据
			new PrintStream(socket.getOutputStream()).println(new String(byt, 0, len));
		}*/
		
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
//读的线程
public class ClientReadThread extends Thread{
	private InputStream inputStream;
	public ClientReadThread(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	
	@Override
	public void run() {
		//读取数据	服务端发送过来的数据
		while (true) {
		//使用缓冲流进行读取
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
			try {
			//读一行
				String readLine = bufferedReader.readLine();
				System.out.println("客户端收到消息:" + readLine);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
//客户端发送数据的线程
public class ClientWriteThread extends Thread{
	private InputStream inputStream;
	private Socket socket;
	public ClientWriteThread(InputStream inputStream, Socket socket) {
		this.inputStream = inputStream;
		this.socket = socket;
	}
	
	
	@Override
	public void run() {
		OutputStream os = null;
		PrintStream printStream = null;
		try {
			os = socket.getOutputStream();
			printStream = new PrintStream(os);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		int len;
		byte[] byt = new byte[1024];
		try {
			while ((len = inputStream.read(byt)) != -1) {
				//发送数据
				printStream.println(new String(byt, 0, len));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if (printStream != null)
				printStream.close();
				try {
				if (os != null)
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
}

//服务端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	static ServerSocket serverSocket = null;
	static Socket accept = null;
	public static void main(String[] args) throws IOException {
		 serverSocket = new ServerSocket(9999);
		//建立连接
		 accept = serverSocket.accept();
		//读取客户端发送过来的数据
		new ServerReadThread(accept.getInputStream()).start();
			//发送数据
		OutputStream outputStream = accept.getOutputStream();
		PrintStream printStream = new PrintStream(outputStream);
		InputStream in = System.in;
		int len;
		byte[] byt = new byte[1024];
		while ((len = in.read(byt)) != -1) {
			//发送数据
			printStream.println(new String(byt, 0, len));
		}
	}
}

//服务端接收数据的线程
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ServerReadThread extends Thread {
	private InputStream inputStream;
	public ServerReadThread(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	
	@Override
	public void run() {
		while (true) {
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
			try {
				String readLine = bufferedReader.readLine();
				System.out.println("服务器收到消息:" + readLine);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

你可能感兴趣的:(多线程,JAVA,SE,网络编程)