Socket 测试
package com.gcx;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.util.Scanner;
/**
* @author gcx
* @site 514130142@qq.com
* @company 暂无
* @create 2021-05-22 15:49
*/
public class Server {
public static void main(String[] args) {
try {
// System.setProperty("java.net.preferIPv6Addresses", "true");
System.out.println("Socket服务器已经在运行...");
// IPv4
// ServerSocket serverSocket = new ServerSocket(9999);
// IPv6
InetAddress address6 = InetAddress.getByName("::1");
ServerSocket serverSocket = new ServerSocket(7777, 50, address6);
// 服务器在实时接收客户端的链接
while (true) {
Socket socket = serverSocket.accept();
new Thread(new ServerListener(socket)).start();
new Thread(new ServerSend(socket)).start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void test1() {
try {
System.setProperty("java.net.preferIPv4Stack", "true");
Socket socket = new Socket("::1", 80); // 没报异常
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 监听客户端发来的消息
class ServerListener implements Runnable {
private Socket socket;
public ServerListener(Socket socket) {
this.socket = socket;
}
public void run() {
try {
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
// 由于客户端传入的信息不是固定的,所以这里使用循环
while (true) {
System.out.println(ois.readObject());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 如果客户端断开了连接,那么服务器需要断开连接,避免资源的浪费
try {
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// 服务器发送给客户端的类
class ServerSend implements Runnable {
private Socket socket;
public ServerSend(Socket socket) {
this.socket = socket;
}
public void run() {
try {
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Server,请输入您要发送的数据:");
String string = scanner.nextLine();
JSONObject jsonObject = new JSONObject();
jsonObject.put("type", "chat");
jsonObject.put("msg", string);
jsonObject.put("serverIp", socket.getInetAddress().toString());
oos.writeObject(jsonObject);
oos.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.gcx;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
/**
* @author gcx
* @site 514130142@qq.com
* @company 暂无
* @create 2021-05-22 15:49
*/
public class Client {
private static Socket socket;
public static boolean connection_state = false;
public static void main(String[] args) {
// System.setProperty("java.net.preferIPv6Addresses", "true");
while (!connection_state) {
connect();
}
try {
Thread.sleep(30000);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void connect() {
try {
// IPv4
// socket = new Socket("127.0.0.1", 9999);
// IPv6
InetAddress address4 = InetAddress.getByName("127.0.0.1");
socket = new Socket(address4, 7777);
connection_state = true;
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
new Thread(new ClientListener(socket, ois)).start();
new Thread(new ClientSend(socket, oos)).start();
new Thread(new ClientHeart(socket, oos)).start();
} catch (Exception e) {
e.printStackTrace();
connection_state = false;
}
}
public static void reconnect() {
while (!connection_state) {
System.out.println("Client1正在尝试重新连接...");
connect();
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// 客户端监听服务器传来的消息
class ClientListener implements Runnable {
private Socket socket;
private ObjectInputStream ois;
public ClientListener(Socket socket, ObjectInputStream ois) {
this.socket = socket;
this.ois = ois;
}
public void run() {
try {
while (true) {
System.out.println(ois.readObject());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 客户端发送数据
class ClientSend implements Runnable {
private Socket socket;
private ObjectOutputStream oos;
public ClientSend(Socket socket, ObjectOutputStream oos) {
this.socket = socket;
this.oos = oos;
}
public void run() {
try {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Client1,请输入您要发送的数据:");
String string = scanner.nextLine();
JSONObject jsonObject = new JSONObject();
jsonObject.put("type", "chat");
jsonObject.put("msg", string);
jsonObject.put("client1Ip", socket.getLocalAddress().toString());
oos.writeObject(jsonObject);
oos.flush();
}
} catch (Exception e) {
e.printStackTrace();
try {
socket.close();
Client.connection_state = false;
Client.reconnect();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
// 客户端主动发送心跳包给服务器以保持他们之间的连接
class ClientHeart implements Runnable {
private Socket socket;
private ObjectOutputStream oos;
public ClientHeart(Socket socket, ObjectOutputStream oos) {
this.socket = socket;
this.oos = oos;
}
public void run() {
try {
System.out.println("心跳包线程已经启动...");
while (true) {
Thread.sleep(5000);
JSONObject jsonObject = new JSONObject();
jsonObject.put("type", "heart");
jsonObject.put("msg", "我是心跳包!");
oos.writeObject(jsonObject);
oos.flush();
}
} catch (Exception e) {
e.printStackTrace();
try {
socket.close();
Client.connection_state = false;
Client.reconnect();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}