Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
As title. I am developing a server-side program to listen for incoming socket connections via Port X, and send data via Port Y. I may receive a string from machine A via port X, and must send processed result to machine B (or back to machine A in some case) via port Y.
I felt sad because I found it was hard to do in a windows service program. I can listen successfully but sometimes failed to send data out, even the port Y at the target machine is opening. How could I solve it?
Posted
Comments
Nathan Minier 9-Dec-15 7:21am    
Why would you want to do this? Most default firewall configurations will evaluate that as malicious traffic and will block it.
Momoko Asahina 708H 9-Dec-15 19:42pm    
Because my partner's iOS programs will send data to me via port X, and receive my response via port Y. That is the way my server-side program communicate with it.

1 solution

Yes, of course, but you should better use separate threads for that. Your listening thread needs to use blocking calls to listed, and a thread already having a connection and writing to a network stream or reading from it is blocked by write or read operations. So each such activity should be done in a separate thread.

It also depends on your application-layer protocol (always try to define such protocol explicitly). Typically you have a protocol which requires you to read and write in certain regular order, then such read and write operations, being sequential, will be done in a single thread; but not in the same thread where you listen. The port is irrelevant here. If you add some activity on one port, your threads should be separate from those using another port. At the same time, another (somewhat typical) mistake would be using a separate thread per client. On the server side, all clients should better be served with one thread, but listening thread should be separate, of course.

Please see my past answers:
an amateur question in socket programming[^],
Multple clients from same port Number[^],
How Do I Get To Know If A A Tcp Connection Is Over[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900