英文:
How to have UDP Server and Client simultaneously listening for incoming and outgoing packets
问题
我正在尝试在Java中实现一个简单的UDP连接,我希望客户端和服务器都能同时监听传入的消息并准备好同时发送消息。
目前,程序是这样的,服务器开始监听传入的数据包,以便客户端发送第一条消息。
如果可能的话,我真的想知道如何简单地实现这一点。
以下是客户端的代码:
class EchoClient
{
public static void main( String args[] ) throws Exception
{
System.out.println("\nWelcome to UDP Client");
System.out.println("---------------------------------------------------");
Scanner sc = new Scanner (System.in);
DatagramSocket socket = new DatagramSocket();
socket.setSoTimeout(120000);
while (true)
{
//发送
System.out.print("\nEnter message: ");
String msg = sc.nextLine();
byte[] buffer = msg.getBytes();
DatagramPacket packetS = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(args[0]), 1500);
socket.send( packetS );
//接收
DatagramPacket packetR = new DatagramPacket(new byte[512], 512);
socket.receive( packetR );
System.out.println( "Alice at: "+new Date()+" "+packetR.getAddress()+":"+packetR.getPort()+"\nSays: "+new String(packetR.getData(), 0, packetR.getLength()) );
}
}
}
服务器的代码如下:
class EchoServer
{
public static void main( String args[] ) throws Exception
{
System.out.println("\nWelcome to UDP Server");
System.out.println("---------------------------------------------------");
Scanner sc = new Scanner (System.in);
DatagramSocket socket = new DatagramSocket(1500);
//消息循环
while ( true )
{
//接收
DatagramPacket packetR = new DatagramPacket(new byte[512], 512);
socket.receive( packetR );
System.out.println("Bob at: "+new Date()+" "+packetR.getAddress()+":"+packetR.getPort()+"\nSays: "+new String(packetR.getData(), 0, packetR.getLength()) );
//发送
System.out.print("\nEnter message: ");
String msg = sc.nextLine();
byte[] buffer = msg.getBytes();
DatagramPacket packetS = new DatagramPacket(buffer, buffer.length, packetR.getAddress(), packetR.getPort());
socket.send( packetS );
}
}
}
英文:
I'm trying to implement a simple UDP connection in Java and I want both Client and Server to be both listening for incoming messages and be ready to send messages at the same time.
At the moment, the program is such that the server starts out listening for incoming packets so the client send the first message.
I'd really like to know of a simple way to implement this if possible.
Here's the code for the Client:
class EchoClient
{
public static void main( String args[] ) throws Exception
{
System.out.println("\nWelcome to UDP Client");
System.out.println("---------------------------------------------------");
Scanner sc = new Scanner (System.in);
DatagramSocket socket = new DatagramSocket();
socket.setSoTimeout(120000);
while (true)
{
//Send
System.out.print("\nEnter message: ");
String msg = sc.nextLine();
byte[] buffer = msg.getBytes();
DatagramPacket packetS = new DatagramPacket(buffer,buffer.length,InetAddress.getByName(args[0]),1500);
socket.send( packetS );
//Receive
DatagramPacket packetR = new DatagramPacket(new byte[512],512);
socket.receive( packetR );
System.out.println( "Alice at: "+new Date()+" "+packetR.getAddress()+":"+packetR.getPort()+"\nSays: "+new String(packetR.getData(),0,packetR.getLength()) );
}
}
}
And the code for the Server:
class EchoServer
{
public static void main( String args[] ) throws Exception
{
System.out.println("\nWelcome to UDP Server");
System.out.println("---------------------------------------------------");
Scanner sc = new Scanner (System.in);
DatagramSocket socket = new DatagramSocket(1500);
//Message loop
while ( true )
{
//Receiving
DatagramPacket packetR = new DatagramPacket(new byte[512],512);
socket.receive( packetR );
System.out.println("Bob at: "+new Date()+" "+packetR.getAddress()+":"+packetR.getPort()+"\nSays: "+new String(packetR.getData(),0,packetR.getLength()) );
//Send
System.out.print("\nEnter message: ");
String msg = sc.nextLine();
byte[] buffer = msg.getBytes();
DatagramPacket packetS = new DatagramPacket(buffer,buffer.length,packetU.getAddress(),packetU.getPort());
socket.send( packetS );
}
}
}
答案1
得分: 0
你可以这样做的一个简单方法是在主线程中启动多个线程。
class EchoServerClient
{
public static void main( String args[] ) throws Exception
{
new Thread(() -> {
EchoServer.main(args) //你也可以将 main 重命名为其他名称
}).start();
new Thread(() -> {
EchoClient.main(args) //你也可以将 main 重命名为其他名称
}).start();
}
}
英文:
Hello an eay way for doing that is to have mutltiple thread launched in the main.
class EchoServerClient
{
public static void main( String args[] ) throws Exception
{
new Thread(() -> {
EchoServer.main(args) //You can also rename main by another name
}).start();
new Thread(() -> {
EchoClient.main(args) //You can also rename main by another name
}).start();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论