简体   繁体   English

UDP + Java的多线程应用程序

[英]Multithreaded application with UDP + java

I am having problems with making multithreaded Datagram Sockets for learning purposes in java. 我在制作用于Java学习目的​​的多线程数据报套接字时遇到问题。

I have a Server class which spawn two threads Send and Recieve each of which currently have the same IP "localhost" for testing purposes and different ports for communication. 我有一个Server类,该类产生两个线程Send和Recieve,每个线程当前具有用于测试目的的相同IP“ localhost”和用于通信的不同端口。

The other end I have a Client class that recieves the connection data from Send port and then gives a request for another packet through the Recieve port of the Server. 另一端我有一个Client类,该类从Send端口接收连接数据,然后通过Server的Recieve端口发出另一个数据包的请求。

The problem I have is that when i run both of them the server sends the data and the client recieves it but the opposite direction.... 我的问题是,当我同时运行它们时,服务器将发送数据,而客户端会接收数据,但方向相反。

So does anybody know what could be the problem....How can I run intercommunication processes between the client and the server on the same machine to see how it runs.... 所以没有人知道这可能是什么问题。。。如何在同一台计算机上的客户端和服务器之间运行相互通信过程,以查看其运行方式。

Here's a part of the code 这是代码的一部分

Spawning two threads :- 产生两个线程:

    SendServer SendThread = new SendServer();
SendThread.run();
RecieveServer RecieveThread = new RecieveServer();
RecieveThread.run();

In the send thread I declare a DatagramPacket with localhost and a portno and similarly I do it for the recieve thread with some other port number 在发送线程中,我声明了一个带有localhost和portno的DatagramPacket,类似地,我对带有其他端口号的接收线程进行了声明

In the client side I write 2 sockets with the 2 ports and use one for sending and other for recieving... 在客户端,我用2个端口编写了2个套接字,并使用一个用于发送,另一个用于接收...

What is the error in this program... Does the issue of threads running one at a time leads to not recieving at the server side ?? 该程序有什么错误?一次运行一个线程的问题是否导致无法在服务器端接收?

Please help.. 请帮忙..

If the server and client are going to be running on the same machine, they need to listen on different port numbers. 如果服务器和客户端将在同一台计算机上运行,​​则它们需要侦听不同的端口号。 It sounds like right now you have the receiver for both client and server using one port number and the sender for both client and server using another. 听起来现在您的客户端和服务器的接收方使用一个端口号,而客户端和服务器的发送方使用另一个端口号。 That pairing is wrong. 这种配对是错误的。 You want the the server's sender and receiver to use one port number and the client's sender and receiver to use a different port number. 您希望服务器的发送方和接收方使用一个端口号,而客户端的发送方和接收方使用一个不同的端口号。

Since this is multithreading, you might be having an issue where all responses coming from the server are destined for the same port. 由于这是多线程的,因此您可能会遇到一个问题,即来自服务器的所有响应都将发往同一端口。 This would mean that you would have to have a serialized socket reader that is able to read in the packet and distinguish which thread it was supposed to go to. 这意味着,你就必须有一个序列化的套接字阅读器,能够在分组阅读并区分哪些线程它应该去。 Otherwise, having to sockets listening on the same port would allow for one socket to read a packet off the line that was meant for the other thread's socket. 否则,必须在同一端口上侦听套接字,这将允许一个套接字从用于另一线程的套接字的行中读取数据包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM