java 套接字通信_Java如何创建客户端-服务器套接字通信?

在此示例中,您将看到如何创建客户端-服务器套接字通信。下面的示例包含两个主要类,ServerSocketExample和和ClientSocketExample。服务器应用程序侦听本地主机上的端口7777。当我们从客户端应用程序发送消息时,服务器会接收到该消息并向客户端应用程序发送回复。

在此示例中,使用TCP套接字进行通信,这意味着客户端应用程序和服务器应用程序之间存在固定的连接线。package org.nhooo.example.network;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.lang.ClassNotFoundException;

import java.lang.Runnable;

import java.lang.Thread;

import java.net.ServerSocket;

import java.net.Socket;

public class ServerSocketExample {

private ServerSocket server;

private static final int PORT = 7777;

private ServerSocketExample() {

try {

server = new ServerSocket(PORT);

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

ServerSocketExample example = new ServerSocketExample();

example.handleConnection();

}

private void handleConnection() {

System.out.println("Waiting for client message...");

// 服务器在此处进行循环以接受由服务器发起的所有连接

// 客户应用程序。

while (true) {

try {

Socket socket = server.accept();

new ConnectionHandler(socket);

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

class ConnectionHandler implements Runnable {

private Socket socket;

ConnectionHandler(Socket socket) {

this.socket = socket;

Thread t = new Thread(this);

t.start();

}

public void run() {

try {

// 阅读客户端应用程序发送的消息

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

String message = (String) ois.readObject();

System.out.println("Message Received: " + message);

// 发送响应信息到客户端应用程序

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

oos.writeObject("Hi...");

ois.close();

oos.close();

socket.close();

System.out.println("Waiting for client message...");

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

}

}

}package org.nhooo.example.network;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.lang.ClassNotFoundException;

import java.net.InetAddress;

import java.net.Socket;

public class ClientSocketExample {

public static void main(String[] args) {

try {

// 在服务器应用程序上创建到服务器套接字的连接

InetAddress host = InetAddress.getLocalHost();

Socket socket = new Socket(host.getHostName(), 7777);

// 向客户端应用程序发送消息

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

oos.writeObject("Hello There...");

// 读取并显示服务器应用程序发送的响应消息

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

String message = (String) ois.readObject();

System.out.println("Message: " + message);

ois.close();

oos.close();

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

}

}

}

要测试该应用程序,您需要启动服务器应用程序。每次您运行客户端应用程序时,它将发送一条消息“ Hello There…”,然后服务器以一条消息“ Hi…”答复。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值