欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

多线程TCP通信

程序员文章站 2022-06-06 08:43:32
...

 

一.客户端服务端,形成对话模式。

1.创建服务端用作读取客户端的每一行内容,

 

并且服务端把每一行内容传输客户端,形成对话模式。

 

package chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
 * 创建服务端用作读取客户端的每一行内容,
 * 并且服务端把每一行内容传输客户端,形成对话模式。
 * @author soft01
 *
 */
public class Server {
	private ServerSocket server;
	public Server() throws IOException {
		System.out.println("初始化服务端...");
		server = new ServerSocket(8088);
		System.out.println("服务端初始化完毕");
	}
	
	public void start() {
		try {
			System.out.println("等待客户端连接...");
			Socket socket = server.accept();
			System.out.println("已连接一个客户端");
			InputStream is = socket.getInputStream();
			InputStreamReader isr = new InputStreamReader(is, "utf-8");
			BufferedReader br = new BufferedReader(isr);
			
			OutputStream os = socket.getOutputStream();
			OutputStreamWriter osr = new OutputStreamWriter(os, "utf-8");
			PrintWriter pw = new PrintWriter(osr,true);
			Scanner scan = new Scanner(System.in);
			while (true) {
				String message = br.readLine();
				System.out.println("客户端说:"+message);
				String str = scan.nextLine();
				pw.println(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		try {
			Server server = new Server();
			server.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


2.创建客户端把每一行内容传输服务端,
并且读取服务端的每一行内容,形成对话模式。

 

 

 

 

package chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
 * 创建客户端把每一行内容传输服务端,
 * 并且读取服务端的每一行内容,形成对话模式。
 * @author soft01
 *
 */
public class Client {
	Socket socket;
	public Client() throws UnknownHostException, IOException  {
		System.out.println("初始化客户端...");
		socket = new Socket("localhost", 8088);
		System.out.println("客户端初始化完毕");
	}
	public void start() {
		try {
			OutputStream os = socket.getOutputStream();
			OutputStreamWriter osr = new OutputStreamWriter(os, "utf-8");
			PrintWriter pw = new PrintWriter(osr,true);
			Scanner scan = new Scanner(System.in);
			
			InputStream is = socket.getInputStream();
			InputStreamReader isr = new InputStreamReader(is, "utf-8");
			BufferedReader br = new BufferedReader(isr);
			while (true) {
				String str = scan.nextLine();
				pw.println(str);
				String message = br.readLine();
				System.out.println("服务端说:"+message);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		try {
			Client client = new Client();
			client.start();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}


二.服务端用作读取客户端的每一行内容,
可以实现连接多个客户端的操作。


1. 创建服务端用作读取客户端的每一行内容,
可以实现连接多个客户端的操作。

 

 

 

 

package chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 创建服务端用作读取客户端的每一行内容,
 * 可以实现连接多个客户端的操作。
 * @author soft01
 *
 */
public class Server_v2 {
	private ServerSocket server;
	public Server_v2() throws IOException {
		System.out.println("初始化服务端...");
		server = new ServerSocket(8088);
		System.out.println("服务端初始化完毕");
	}
	
	public void start() {
		try {
			while(true) {
				System.out.println("等待客户端连接...");
				Socket socket = server.accept();
				System.out.println("已连接一个客户端");
				ClientHandler handler = new ClientHandler(socket);
				Thread t = new Thread(handler);
				t.start();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		try {
			Server_v2 server = new Server_v2();
			server.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private class ClientHandler implements Runnable{
		private Socket socket;
		public ClientHandler(Socket socket) {
			this.socket=socket;
		}
		public void run() {
			try {
				InputStream is = socket.getInputStream();
				InputStreamReader isr = new InputStreamReader(is, "utf-8");
				BufferedReader br = new BufferedReader(isr);
				String str = null;
				while ((str= br.readLine())!=null) {
					System.out.println("客户端说"+str);
				}
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
}


2.创建客户端把每一行内容传输服务端

 

 

 

 

package chat;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
 * 创建客户端把每一行内容传输服务端
 * @author soft01
 *
 */
public class Client_v2 {
	Socket socket;
	public Client_v2() throws UnknownHostException, IOException  {
		System.out.println("初始化客户端...");
		socket = new Socket("localhost", 8088);
		System.out.println("客户端初始化完毕");
	}
	public void start() {
		try {
			OutputStream os = socket.getOutputStream();
			OutputStreamWriter osr = new OutputStreamWriter(os, "utf-8");
			PrintWriter pw = new PrintWriter(osr,true);
			Scanner scan = new Scanner(System.in);
			
			while (true) {
				String str = scan.nextLine();
				pw.println(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		try {
			Client_v2 client = new Client_v2();
			client.start();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}