分为客户端与服务器端。客户端向服务器端发送用户信息,请求登录;服务器端采用多线程处理多用户登录请求,并简单匹配用户登录信息是否正确,返回相应的状态。
package NET;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TCP_Server {
public static void main(String[] args) throws IOException {
System.out.println("-----SERVER-----");
// 1. 指定端口,使用ServerSocket创建服务器套接字
ServerSocket server = new ServerSocket(8888);
while (true) {
// 2. 阻塞式等待TCP连接
Socket client = server.accept();
System.out.println("建立TCP连接");
// 3. 创建新线程处理业务
new Thread(new Channel(client)).start();
}
}
}
class Channel implements Runnable {
private Socket client;
private DataInputStream dis;
private DataOutputStream dos;
Channel(Socket client) throws IOException {
this.client = client;
this.dis = new DataInputStream(this.client.getInputStream());
this.dos = new DataOutputStream(this.client.getOutputStream());
}
private String receive() throws IOException {
return dis.readUTF();
}
private void send(String msg) throws IOException {
dos.writeUTF(msg);
dos.flush();
}
private void release() throws IOException {
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
if (client != null) {
client.close();
}
}
@Override
public void run() {
String username = null;
String password = null;
try {
String[] dataArray = receive().split("&");
for (String data: dataArray) {
String[] userInfo = data.split("=");
switch (userInfo[0]) {
case "username":
username = userInfo[1];
System.out.println("用户:" + username);
break;
case "password":
password = userInfo[1];
System.out.println("密码:" + password);
break;
}
}
if (username.equals("username") && password.equals("password")) {
send("登录成功");
} else {
send("登录失败");
}
release();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package NET;
import java.io.*;
import java.net.Socket;
public class TCP_Client {
public static void main(String[] args) throws IOException {
System.out.println("-----CLIENT-----");
Socket server = new Socket("localhost", 8888);
Session session = new Session(server);
session.getInfo();
session.send();
session.receive();
session.release();
}
}
class Session {
private String msg;
private Socket server;
private DataInputStream dis;
private DataOutputStream dos;
Session(Socket server) throws IOException {
this.msg = null;
this.server = server;
this.dis = new DataInputStream(server.getInputStream());
this.dos = new DataOutputStream(server.getOutputStream());
}
void getInfo() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名:");
String username = reader.readLine();
System.out.println("请输入密码:");
String password = reader.readLine();
msg = String.format("username=%s&password=%s", username, password);
}
void send() throws IOException {
dos.writeUTF(msg);
dos.flush();
}
void receive() throws IOException {
String res = dis.readUTF();
System.out.println(res);
}
void release() throws IOException {
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
if (server != null) {
server.close();
}
}
}