Click here to Skip to main content
15,847,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

I am working on a project where i need to transfer multiple files from server to client(s) using TCP connection. Every client needs to be connected to the server using separate thread. The main issue is I already have the client listening on a port and i want that the thread from the server connects to the server on that port itself. This is a WinForm Application where i have a GUI running on a thread. The connection is to be established under that thread.So i dont want that my UI freezes and doesn't respond to the User request's
Can anyone please give me a bit of idea on how to accomplish this?
Or any links would be equally helpful!!
[no name] 24-May-14 7:54am    
Aha, you have clients listening on a port and the thread from the server should connect to the Server. Please think about your question again, rephrase it and use "Improve question". I'm sure when the question is more defined, you will get an answer here.
sovit agarwal 25-May-14 5:46am    
Actually its an opposite process that i am following.. I want my clients to be listening on a port and the server connects to them....Its unlike the general norm where the server is listening on a port and client connects to the server
gggustafson 26-May-14 23:05pm    
May I suggest that your design is wrong.
sovit agarwal 27-May-14 1:15am    
Actually the design isn't wrong but this is the actual ask of my project...

1 solution

In the "client", asynchronously execute a BackgroundWorker thread. That thread should create a TcpListener. Make sure that you use a port in the range 49152-65535 (the unassigned ports). When the listener accepts a connection, it reads the file contents into a variable (byte, string, etc.) that is available to the main thread (GUI). It then raises the ReportProgress event and reenters the listening state. The main thread, in the ProgressChanged event handler, accesses the data and acts upon it.

Using this algorithm (server/client) contains a danger if the transfers occur so rapidly that the main thread is not able to keep up with the incoming data. This is why the traditional client/server model puts the client in control. Then the client signals the server its readiness to receive data and the server responds to the request. In a UI application, this is the preferred method.

Also in the server/client algorithm, I believe that the server must broadcast its messages since there is no known address to which to send the message.
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