分析
实现一个服务器端响应多个客户端的连接请求。
没有实现Send、Receice中的资源释放。
代码
服务器端
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class LoginMultiServer {
public static void main(String[] args) throws IOException{
System.out.println("Server");
//1. 指定用于接收客户端的端口,使用ServerSocket创建服务器
ServerSocket server = new ServerSocket(8888);
int num = 1;
//2. 循环等待连接accept(),通过多线程可连接多个客户端
boolean isRunning = true;
while(isRunning){
Socket client = server.accept();
System.out.println("第"+ (num++) +"个客户请求建立连接");
new Thread(new Channel(client)).start(); //建立线程
}
//4. 释放资源
server.close();
}
/**
* 静态内部类
* 封装数据的传输通道
* @author dxt
*
*/
static class Channel implements Runnable{
private Socket client;
//输入流
private DataInputStream dis;
//输出流
private DataOutputStream dos;
public Channel(Socket client){
this.client = client;
try {
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
release();
}
}
/**
* 接收数据
* @return
*/
private String receive(){
String datas = "";
try {
datas = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
return datas;
}
/**
* 发送数据
* @param msg
*/
private void send(String msg){
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 释放资源
*/
private void release(){
try {
if(dos != null){
dos.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
try {
if(dis != null){
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(client != null){
client.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
//接收数据
String uname = "";
String upwd = "";
String[] dataArray = receive().split("&");
//输出数据
for(String info:dataArray){
String[] userInfo = info.split("=");
if(userInfo[0].equals("name")){
System.out.println("name"+"--->"+userInfo[1]);
uname = userInfo[1];
}else if(userInfo[0].equals("pwd")){
System.out.println("pwd"+"--->"+userInfo[1]);
upwd = userInfo[1];
}
}
//返回响应
if(uname.equals("dxt") && upwd.equals("123456")){
send("登录成功,欢迎回来。");
}else{
send("用户名或密码错误。");
}
//释放资源
release();
}
}
}
客户端
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class LoginMultiClient {
public static void main(String[] args) throws UnknownHostException, IOException{
System.out.println("Client");
//1.建立客户端
Socket client = new Socket("localhost", 8888);
//2. 处理数据
new Send(client).send(); //发送数据, 先请求
new Receive(client).receive(); //接收数据,后响应
//3. 释放资源
client.close();
}
/**
* 发送数据
* @author dxt
*
*/
static class Send{
private Socket client;
private DataOutputStream dos;
private BufferedReader br;
private String msg;
public Send(Socket client){
this.client = client;
this.br = new BufferedReader(new InputStreamReader(System.in));
msg = init();
try {
dos = new DataOutputStream(this.client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 接收需要发送的数据
* @return
*/
public String init(){
//接收数据
String msg ="";
try {
System.out.print("请输入姓名:");
String uname = br.readLine();
System.out.print("请输入密码:");
String upwd = br.readLine();
msg = "name="+uname+"&"+"pwd="+upwd;
} catch (IOException e) {
e.printStackTrace();
}
return msg;
}
/**
* 发送数据
*/
public void send(){
try {
dos.writeUTF(this.msg);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static class Receive{
private Socket client;
private DataInputStream dis;
public Receive(Socket client){
this.client = client;
try {
dis = new DataInputStream(this.client.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 接收服务器端返回的相应数据
*/
public void receive(){
try {
String msg = dis.readUTF();
System.out.println(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}