Java 局域网聊天
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();
}
}
}
}
支持楼主{:1_893:} 没啥用 辛苦了 Java萌新前来学习了,前几天在某学习网站上看了socket但是自己没跑通。。。 课设的时候看到过类似的题目,觉得挺有意思,弄个好看的GUI就更好了 挺有意思的,好玩 萌新打个卡,不错,哈哈哈哈 你这个应该是哪个教程上的案例吧,还是基于控制台模式的聊天 额,看了题目,基本上就确定你用socket 不错哦不错哦
页:
[1]
2