Click here to Skip to main content
14,211,427 members
Rate this:
Please Sign up or sign in to 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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   

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



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.1 | Last Updated 9 Dec 2015
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100