我正在尝试构建一个可以回显来自 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();
}
}
感谢您的帮助。