Socket编程(一)简单Client与Server的单向通信和双向通信

从Console中读取内容

		BufferedReader inputReader;
		// Alt + Shift + z    try...catch等快捷键
		// 从Console读取内容
		try {
			inputReader = new BufferedReader(new InputStreamReader(System.in));
			String inputContent;
			while (!(inputContent = inputReader.readLine()).equals("bye")) {
				System.out.println(inputContent);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}


单向通信

Client -> Server


SocketClient

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

public class SocketClient {

	public static void main(String[] args) {
		SocketClient client = new SocketClient();
		client.start();
	}
	
	public void start() {
		BufferedReader inputReader = null;
		BufferedWriter writer = null;
		Socket socket = null;
		// Alt + Shift + z    try...catch等快捷键
		// 从Console读取内容
		try {
			socket = new Socket("127.0.0.1", 9898);
			// 从socket进行写入
			writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			inputReader = new BufferedReader(new InputStreamReader(System.in));
			String inputContent;
			while (!(inputContent = inputReader.readLine()).equals("bye")) {
				writer.write(inputContent + "\n");
				writer.flush();
//				System.out.println(inputContent);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
				inputReader.close();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

SocketServer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

	public static void main(String[] args) {
		SocketServer socketServer = new SocketServer();
		socketServer.startServer();
	}
	
	public void startServer() {
		ServerSocket serverSocket = null;
		BufferedReader reader = null;
		Socket socket = null;
		try {
			// 端口号只要不冲突就行
			serverSocket = new ServerSocket(9898);
			System.out.println("server started..");
			// 进入阻塞状态,等待客户端接入
			socket = serverSocket.accept();
			System.out.println("client " + socket.hashCode() + " connected");
			// 从socket中读取数据
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String receiveMsg;
			while ((receiveMsg = reader.readLine()) != null) { // 以"\n"结束
				System.out.println(receiveMsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				socket.close();
				serverSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}



Client Server双向通信

即时接受消息

也就是所谓的长连接

记得要先启动Server


SocketClient

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

public class SocketClient {

	public static void main(String[] args) {
		SocketClient client = new SocketClient();
		client.start();
	}
	
	public void start() {
		BufferedReader inputReader = null;
		// 从socket读取
		BufferedReader reader = null;
		BufferedWriter writer = null;
		Socket socket = null;
		// Alt + Shift + z    try...catch等快捷键
		// 从Console读取内容
		try {
			socket = new Socket("127.0.0.1", 9898);
			// 用于读取服务器返回的数据
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			// 从socket进行写入
			writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			inputReader = new BufferedReader(new InputStreamReader(System.in));
			String inputContent;
			while (!(inputContent = inputReader.readLine()).equals("bye")) {
				writer.write(inputContent + "\n");
				writer.flush();
//				System.out.println(inputContent);
				// 服务器返回的数据
				String response = reader.readLine();
				System.out.println(response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				writer.close();
				inputReader.close();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

SocketServer

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

public class SocketServer {

	public static void main(String[] args) {
		SocketServer socketServer = new SocketServer();
		socketServer.startServer();
	}
	
	public void startServer() {
		ServerSocket serverSocket = null;
		BufferedReader reader = null;
		BufferedWriter writer = null;
		Socket socket = null;
		try {
			// 端口号只要不冲突就行
			serverSocket = new ServerSocket(9898);
			System.out.println("server started..");
			// 进入阻塞状态,等待客户端接入
			socket = serverSocket.accept();
			System.out.println("client " + socket.hashCode() + " connected");
			// 从socket中读取数据
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			String receiveMsg;
			while ((receiveMsg = reader.readLine()) != null) { // 以"\n"结束
				System.out.println(receiveMsg);
				writer.write("server reply:" + receiveMsg + "\n");
				writer.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				writer.close();
				socket.close();
				serverSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}





你可能感兴趣的:(Socket)