gpt4 book ai didi

一台服务器中的 Java TCP 和 UDP 回显

转载 作者:可可西里 更新时间:2023-11-01 02:44:16 24 4
gpt4 key购买 nike

我正在尝试构建一个可以回显来自 TCP 或 UDP 客户端的输入的服务器。

到目前为止,我能想到的最好的是:

import java.net.*;
import java.io.*;

public class EchoServerMultiProtocol {
public static void main(String[] args) throws IOException {

String clientSentence;
String capitalizedSentence;

while (true) {
/*******************************************************************
* * Handle TCP * *
*******************************************************************/
ServerSocket TCP_Socket = new ServerSocket(6789);

Socket connectionSocket = TCP_Socket.accept();

BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));

DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());

clientSentence = inFromClient.readLine();

capitalizedSentence = clientSentence.toUpperCase() + '\n';

outToClient.writeBytes(capitalizedSentence);

/*******************************************************************
* * Handle UDP * *
*******************************************************************/
DatagramSocket UDP_Socket = new DatagramSocket(9876);

byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];

DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);

UDP_Socket.receive(receivePacket);

clientSentence = new String(receivePacket.getData());

InetAddress IPAddress = receivePacket.getAddress();

int port = receivePacket.getPort();

capitalizedSentence = clientSentence.toUpperCase();

sendData = capitalizedSentence.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, port);

UDP_Socket.send(sendPacket);

}
}
}

发生的情况是,如果我从 TCP 客户端发送消息,程序按预期工作,但从 UDP 客户端发送消息时什么也没有发生。我不是很精通客户端/服务器通信,所以任何帮助将不胜感激。

客户端代码如下,但我怀疑它们是问题的根源。

TCP 客户端

import java.io.*;
import java.net.*;

class EchoClientTCP {

public static void main(String argv[]) throws Exception {
String sentence;
String modifiedSentence;

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
System.in));

Socket clientSocket = new Socket("127.0.0.1", 6789);

DataOutputStream outToServer = new DataOutputStream(
clientSocket.getOutputStream());

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + '\n');

modifiedSentence = inFromServer.readLine();

System.out.println("FROM SERVER: " + modifiedSentence);

clientSocket.close();

}
}

UDP 客户端

import java.io.*;
import java.net.*;

class EchoClientUDP {
public static void main(String args[]) throws Exception {

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
System.in));

DatagramSocket clientSocket = new DatagramSocket();

InetAddress IPAddress = InetAddress.getByName("127.0.0.1");

byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];

String sentence = inFromUser.readLine();

sendData = sentence.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, 9876);

clientSocket.send(sendPacket);

DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);

clientSocket.receive(receivePacket);

String modifiedSentence = new String(receivePacket.getData());

System.out.println("FROM SERVER:" + modifiedSentence);

clientSocket.close();

}
}

感谢您的帮助。

最佳答案

您的程序阻塞等待 TCP 消息,因此它永远不会看到任何 UDP。

您至少需要两个线程,每个线程一个。

关于一台服务器中的 Java TCP 和 UDP 回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14445282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com