2.百度百科搜索很详细
3.附件中的代码仅供参考
TcpSocketServerDemo.java
package yaoshun.InetAddress;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/*
* 客户端:TCP服务器端
*/
public class TcpSocketServerDemo {
private ServerSocket serverSocket;// 服务器连接
private int port; // 服务器监听端口
private List<Socket> sockets;
private int ranNumber;
/**
* 服务器结束的标志
*/
public boolean ISEND;
public TcpSocketServerDemo(int port) {
this.port = port;
serverSocket = null;
sockets = new ArrayList<Socket>();
ranNumber = new Random().nextInt(50);
}
/*
* 启动服务器
*/
public void starServer() {
try {
// 第一步:建立服务器连接(绑定监听端口)
serverSocket = new ServerSocket(port);
System.out.println("姚舜的服务器已经启动...");
Socket socket;// 客户端连接(如果建立连接时,本机网络不容或服务器程序未启动则会抛出异常)
ServerThread serverThread;
Thread t2;
while (!ISEND) {
// 第二步:监听端口(获取客户端连接)
socket = serverSocket.accept();
if (ISEND) {
while (!socket.isClosed()) {
socket.close();
Thread.sleep(100);
}
} else {
sockets.add(socket);
serverThread = new ServerThread(this, socket);
t2 = new Thread(serverThread);
t2.start();
}
}
for (Socket s : sockets) {
s.close();
}
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TcpSocketServerDemo tDemo = new TcpSocketServerDemo(1000);
tDemo.starServer();
}
/**
* 校验
*
* @param i
* @return
*/
public int check(int i) {
if (i < ranNumber) {
return -1;
} else if (i > ranNumber) {
return 1;
}
return 0;
}
}
/**
* 服务端处理线程
*
*/
class ServerThread implements Runnable {
private TcpSocketServerDemo tDemo;
private Socket socket;
private DataInputStream dis;// 输入流(读取数据)
private DataOutputStream dos;// 输出流(发送数据)
public ServerThread(TcpSocketServerDemo tDemo, Socket socket) {
super();
this.tDemo = tDemo;
this.socket = socket;
try {
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
String message = "";
int i;
int index = 0;
int result = 0;
while (!message.equals("Quit") && index < 5) {
try {
message = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
try {
i = Integer.parseInt(message);
result = tDemo.check(i);
if (result == 1) {
dos.writeUTF("数字大了");
} else if (result == -1) {
dos.writeUTF("数字小了");
}
if (result == 0) {
dos.writeUTF("猜中了");
break;
}
} catch (NumberFormatException | IOException e) {
continue;
}
index++;
}
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TcpSocketClientDemo.java
package yaoshun.InetAddress;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
/*
* 演示类:TCP客户端
*/
public class TcpSocketClientDemo {
private Socket socket;// 客户端连接(如果建立连接时,本机网络不容或服务器程序未启动则会抛出异常)
private DataInputStream dis;// 输入流(读取数据)
private DataOutputStream dos;// 输出流(发送数据)
// private String serverIP;// 服务器IP地址
// private int serverPort;// 服务器监听端口
private String sendData;// 发送的数据
public TcpSocketClientDemo(String serverIP, int serverPort) throws UnknownHostException, IOException {
// this.serverIP = serverIP;
// this.serverPort = serverPort;
socket = new Socket(serverIP, serverPort);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
sendData = null;
startClient();
}
public void startClient() {
try {
// 第一步:创建客户端连接
System.out.println("===我的客户端界面===");
// 第二步:客户端向服务器发送数据
int i = 0;
Scanner input = new Scanner(System.in);
while (i < 5) {
System.out.println("请输入你的内容(客户端的):");
sendData = input.nextLine();
if (sendData.equals("quit")) {
break;
}
dos.writeUTF(sendData);
sendData = dis.readUTF();
System.out.println("接收到服务器内容:" + sendData);
if (i == 5) {
System.out.println("超过允许次数,请重新连接服务器.");
}
if (sendData.equals("猜中了")) {
i = 5;
}
i++;
}
input.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 第四步:关闭输入流、输出流和客户端连接
try {
dis.close();
dos.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
new TcpSocketClientDemo("127.0.0.1", 1000);
} catch (IOException e) {
e.printStackTrace();
}
}
}