使用Socket实现NIO通信


Socket为套接字。


1.accept方法会阻塞线程,直到有一个客户端连接到服务端

2.基于TCP/IP、NIO

3.网络传输以字节为单位,所以在使用时,需要转换成字符。使用Scanner和PrintWrite更方便,可直接接收字节或是转换为字节。

3.使用线程。对接收和发送使用两个线程,可以实现同时发送多条数据,否则只能你发一句,对方再发一句之后才可以再继续发。

4.使用swing设计简单界面  

   https://git.oschina.net/zzhao114/Socket.git


服务器端代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class MyServer implements Runnable {

	ServerSocket server;
	Socket client;
	BufferedReader in;
	PrintWriter out;
	BufferedReader br;
	Thread connThread;

	public void serverStart() {
		
		try {
			server = new ServerSocket(5678);
			System.out.println("Server Start!");
			while(true){
				client = server.accept();
				System.out.println(new Date());
				in = new BufferedReader(new InputStreamReader(client.getInputStream()));
				out = new PrintWriter(client.getOutputStream());
				new Thread(this).start();
				out.println("yes");
				out.flush();
				if (in.readLine().equals("yes"))
					System.out.println("对方已上线!");
				String info;
				while ((info = in.readLine()) != null) {
					System.out.print("He/She:");
					System.out.println(info);
					if (info.equals("bye")) {
						break;
					}
				}
				client.close();
				System.out.println("Server Shuntdown!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void run() {
		br = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			String info;
			try {
				while ((info =br.readLine()) != null) {
					out.println(info);
					out.flush();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}

	public static void main(String[] args) {
		new MyServer().serverStart();
	}
}

Client端代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client implements Runnable {

	Socket server;
	BufferedReader in;
	PrintWriter out;
	BufferedReader br;
	Thread connThread;

	public void clientStart(){
		try {
			server = new Socket(InetAddress.getLocalHost(), 5678);
			System.out.println("Client Start!");
			while(true){
				in = new BufferedReader(new InputStreamReader(server.getInputStream()));
				out = new PrintWriter(server.getOutputStream());
				new Thread(this).start();
				out.println("yes");
				out.flush();
				if (in.readLine().equals("yes"))
					System.out.println("对方已上线!");
				String info;
				while ((info = in.readLine()) != null) {
					System.out.print("He/She:");
					System.out.println(info);
					if (info.equals("bye")) {
						break;
					}
				}
				System.out.println("断开连接!");
				server.close();
			}	
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public void run() {
		br = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			String info;
			try {
				while ((info =br.readLine()) != null) {
					out.println(info);
					out.flush();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}

	public static void main(String[] args) {
		new Client().clientStart();
	}
}


你可能感兴趣的:(线程,socket,通信,swing,nio)