好友
阅读权限10
听众
最后登录1970-1-1
|
java实现socket通信
可以进行简单的双人聊天,服务器和客户端之间互发消息
服务器支持多个客户端同时连入,服务器为每个客户端创建独立的线程处理socket请求
心跳包机制保证socket长连接不掉线,每5秒发送一次心跳包
重新连接机制,在掉线发生时,每3秒尝试一次重新连接
Server
import org.json.simple.JSONObject;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
public static void main(String[] args){
try {
System.out.println("Socket服务器开始运行...");
ServerSocket serverSocket = new ServerSocket(9999);
while (true){
Socket socket = serverSocket.accept();
new Thread(new Server_listen(socket)).start();
new Thread(new Server_send(socket)).start();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
class Server_listen implements Runnable{
private Socket socket;
Server_listen(Socket socket){
this.socket = socket;
}
@Override
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 Server_send implements Runnable{
private Socket socket;
Server_send(Socket socket){
this.socket = socket;
}
@Override
public void run() {
try {
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
Scanner scanner = new Scanner(System.in);
while (true){
System.out.print("请输入要发送的内容:");
String string = scanner.nextLine();
JSONObject object = new JSONObject();
object.put("type","chat");
object.put("msg",string);
oos.writeObject(object);
oos.flush();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
Client
import org.json.simple.JSONObject;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
private static Socket socket;
public static boolean connection_state = false;
public static void main(String[] args){
while (!connection_state) {
connect();
try {
Thread.sleep(3000);
}catch (Exception e){
e.printStackTrace();
}
}
}
private static void connect(){
try {
socket = new Socket("127.0.0.1", 9999);
connection_state = true;
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
new Thread(new Client_listen(socket,ois)).start();
new Thread(new Client_send(socket,oos)).start();
new Thread(new Client_heart(socket,oos)).start();
}catch (Exception e){
e.printStackTrace();
connection_state = false;
}
}
public static void reconnect(){
while (!connection_state){
System.out.println("正在尝试重新链接.....");
connect();
try {
Thread.sleep(3000);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
class Client_listen implements Runnable{
private Socket socket;
private ObjectInputStream ois;
Client_listen(Socket socket,ObjectInputStream ois){
this.socket = socket;
this.ois = ois;
}
@Override
public void run() {
try {
while (true){
System.out.println(ois.readObject());
}
}catch (Exception e){
e.printStackTrace();
}
}
}
class Client_send implements Runnable{
private Socket socket;
private ObjectOutputStream oos;
Client_send(Socket socket, ObjectOutputStream oos){
this.socket = socket;
this.oos = oos;
}
@Override
public void run() {
try {
Scanner scanner = new Scanner(System.in);
while (true){
System.out.print("请输入你要发送的信息:");
String string = scanner.nextLine();
JSONObject object = new JSONObject();
object.put("type","chat");
object.put("msg",string);
oos.writeObject(object);
oos.flush();
}
}catch (Exception e){
e.printStackTrace();
try {
socket.close();
Client.connection_state = false;
Client.reconnect();
}catch (Exception ee){
ee.printStackTrace();
}
}
}
}
class Client_heart implements Runnable{
private Socket socket;
private ObjectOutputStream oos;
Client_heart(Socket socket, ObjectOutputStream oos){
this.socket = socket;
this.oos = oos;
}
@Override
public void run() {
try {
System.out.println("心跳包线程已启动...");
while (true){
Thread.sleep(5000);
JSONObject object = new JSONObject();
object.put("type","heart");
object.put("msg","心跳包");
oos.writeObject(object);
oos.flush();
}
}catch (Exception e){
e.printStackTrace();
try {
socket.close();
Client.connection_state = false;
Client.reconnect();
}catch (Exception ee){
ee.printStackTrace();
}
}
}
}
|
免费评分
-
查看全部评分
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|