关键步骤如下:
1、两个端点进行连接
2、打开传递信息的输入/输出流
3、传递数据,接受数据
4、关闭连接。
模拟用户登录的功能,实现:客户发送登录用户信息,服务器端显示登录信息并响应给客户端(登录成功,登录失败)信息。
服务器端实现步骤:
1、建立连接,监听端口。
2、使用accept()方法等待客户端触发通信。
3、打开Socket关联的输入/输出流。
4、向输出流中写入信息。
5、从输入流中读取响应信息。
6、关闭所有的数据流和Socket。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;
public class Service {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(6000);
System.out.println("服务器已经启动!");
socket = serverSocket.accept();
// 从客户端读取数据,需要先转换为字符流
is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = str = br.readLine();
System.out.println("我是服务器,客户端输入的消息为:" + str);
socket.shutdownInput();
// 对客户端输入的账号密码信息进行处理
String[] str2 = str.split("&");
HashMap<String, String> hm = new HashMap<>();
for (String str1 : str2) {
String[] str3 = str1.split(":");
hm.put(str3[0], str3[1]);
}
String reply = "登录失败";
if ("zhangwendi".equals(hm.get("name"))
&& "123456".equals(hm.get("password"))) {
reply = "登录成功!";
}
// 向客户端发送消息
os = socket.getOutputStream();
os.write(reply.getBytes());
socket.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (os != null)
os.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端实现步骤:
1、建立连接,连接指向服务器及端口。
2、打开Socket关联的输入/输出流。
3、向输出流中写入信息。
4、从输入流中读取响应信息。
5、关闭所有的数据流和Socket。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class client {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 6000);
os = socket.getOutputStream();
is = socket.getInputStream();
// 向服务器发送消息,需要先转换为字节流
Scanner can = new Scanner(System.in);
System.out.println("输入账号:");
String name = can.next();
System.out.println("输入密码:");
String password = can.next();
String str = "name:" + name + "&password:" + password;
byte[] b = str.getBytes();
os.write(b);
socket.shutdownOutput();
// 从服务器读取信息,需要先把字节流转换为容易读取的字符流
BufferedReader bis = new BufferedReader(new InputStreamReader(is));
String str2 = bis.readLine();
System.out.println("客户端输出:服务器回应为==>" + str2);
socket.shutdownInput();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (os != null)
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
<<<<=======升级=======>>>>实现传递对象信息
import java.io.Serializable;
public class User implements Serializable {
/**
* 随机序列号
*/
private static final long serialVersionUID = -7185447814341077688L;
private String name;
private String password;
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [name=" + name + ", password=" + password + "]";
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Service {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(6000);
System.out.println("服务器已经启动!");
socket = serverSocket.accept();
// 从客户端读取数据,需要先转换为字符流
is = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
User user = (User) ois.readObject();
// BufferedReader br = new BufferedReader(new
// InputStreamReader(is));
// String str = str = br.readLine();
System.out.println("我是服务器,客户端输入的消息为:" + user);
socket.shutdownInput();
// 对客户端输入的账号密码信息进行处理
// String[] str2 = str.split("&");
// HashMap<String, String> hm = new HashMap<>();
// for (String str1 : str2) {
// String[] str3 = str1.split(":");
// hm.put(str3[0], str3[1]);
// }
String reply = "登录失败";
if ("zhangwendi".equals(user.getName())
&& "123456".equals(user.getPassword())) {
reply = "登录成功!";
}
// 向客户端发送消息
os = socket.getOutputStream();
os.write(reply.getBytes());
socket.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (os != null)
os.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class client {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 6000);
os = socket.getOutputStream();
is = socket.getInputStream();
// 向服务器发送消息,需要先转换为字节流
Scanner can = new Scanner(System.in);
System.out.println("输入账号:");
String name = can.next();
System.out.println("输入密码:");
String password = can.next();
User user = new User(name, password);//
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(user);
// String str = "name:" + name + "&password:" + password;
// byte[] b = str.getBytes();
// os.write(b);
socket.shutdownOutput();
// 从服务器读取信息,需要先把字节流转换为容易读取的字符流
BufferedReader bis = new BufferedReader(new InputStreamReader(is));
String str2 = bis.readLine();
System.out.println("客户端输出:服务器回应为==>" + str2);
socket.shutdownInput();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (os != null)
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
<<<<=======再升级=======>>>>实现处理客户端的多次请求,利用while循环设计服务器(单个任务执行完才执行下个任务)
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Service {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(6000);
System.out.println("服务器已经启动!");
while (true) {
socket = serverSocket.accept();
// 从客户端读取数据,需要先转换为字符流
is = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
User user = (User) ois.readObject();
// BufferedReader br = new BufferedReader(new
// InputStreamReader(is));
// String str = str = br.readLine();
System.out.println("我是服务器,客户端输入的消息为:" + user);
socket.shutdownInput();
// 对客户端输入的账号密码信息进行处理
// String[] str2 = str.split("&");
// HashMap<String, String> hm = new HashMap<>();
// for (String str1 : str2) {
// String[] str3 = str1.split(":");
// hm.put(str3[0], str3[1]);
// }
String reply = "登录失败";
if ("zhangwendi".equals(user.getName())
&& "123456".equals(user.getPassword())) {
reply = "登录成功!";
}
// 向客户端发送消息
os = socket.getOutputStream();
os.write(reply.getBytes());
socket.shutdownOutput();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (os != null)
os.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class client {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 6000);
os = socket.getOutputStream();
is = socket.getInputStream();
// 向服务器发送消息,需要先转换为字节流
// Scanner can = new Scanner(System.in);
// System.out.println("输入账号:");
// String name = can.next();
// System.out.println("输入密码:");
// String password = can.next();
User user = new User("zhangwendi", "123456");//为了方便测试,直接赋值
// User user = new User(name, password);//
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(user);
// String str = "name:" + name + "&password:" + password;
// byte[] b = str.getBytes();
// os.write(b);
socket.shutdownOutput();
// 从服务器读取信息,需要先把字节流转换为容易读取的字符流
BufferedReader bis = new BufferedReader(new InputStreamReader(is));
String str2 = bis.readLine();
System.out.println("客户端输出:服务器回应为==>" + str2);
socket.shutdownInput();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (os != null)
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器已经启动!
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi2, password=123456]
<<<<=======再升级=======>>>>实现多客户端的响应处理,利用多线程设计服务器(可同时处理多个客户端的请求)
设计步骤:
1、创建服务器端线程类,run()方法中实现对一个请求的响应处理。
2、修改服务器端代码,实现一直监听。
3.服务器端每监听到一次请求,创建一个线程对象,启动。
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServiceThread extends Thread {
Socket socket = null;
public ServiceThread(Socket socket) {
super();
this.socket = socket;
}
@Override
public void run() {
InputStream is = null;
OutputStream os = null;
try {
// 从客户端读取数据
is = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
User user = (User) ois.readObject();
try {
Thread.sleep(3000);// 为了测试方便,休眠三秒
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("我是服务器,客户端输入的消息为:" + user);
socket.shutdownInput();
try {
Thread.sleep(3000);// 为了测试方便,再休眠三秒
} catch (InterruptedException e) {
e.printStackTrace();
}
String reply = "登录失败";
if ("zhangwendi".equals(user.getName())
&& "123456".equals(user.getPassword())) {
reply = "登录成功!";
}
// 向客户端发送消息
os = socket.getOutputStream();
os.write(reply.getBytes());
socket.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (os != null)
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Service {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(6000);
System.out.println("服务器已经启动!");
while (true) {
socket = serverSocket.accept();
new ServiceThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务器已经启动!
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]
我是服务器,客户端输入的消息为:User [name=zhangwendi, password=123456]