I have a server-client application [TCP sockets, .NET 4.0]..
the application about :
the application should do the 3 tasks in the same time
After i done that and it worked .. i realized that these kind of application should use one port for all tasks .. like Radmin and netsupport .. etc
but i used 3 ports .. one to keep receiving commands any time the client sends. and one for receiving files .. and one if the client asked for a screenshot
so is it fine to use 3 ports for a network application? ..
I’ve tried to create 3 sockets on the client side to connect to server on the same port (ex:9090)
but when i tried to send a file from the client .. the server received the bytes in Job function that’s should receives the commands only… so it looks like i can’t use one port for those tasks
so is it possible use one port for all of the three tasks that they may work at the same time?
private void ClientAccept()
{
while (true)
{
Socket client = server.Accept();
Console.WriteLine(client.RemoteEndPoint + " has connected");
NetworkStream stream = new NetworkStream(client);
StreamReader reader = new StreamReader(stream);
string line = reader.ReadLine();
if (line == "1") //it means its a command socket
{
thJob = new Thread(Job);
thJob.Start(cmdClient);
}
else if(line=="2") //its a data socket
{
FileTransfer ft = new FileTransfer(client);
}
}
}
private void Job(object o)
{
Socket s = (Socket)o;
StreamReader sr = new StreamReader(new NetworkStream(s));
string cmd = null;
while ((cmd = sr.ReadLine()) != null)
{
//bla bla
}
}
Added question:
lets say the server has one port.
the client connect to server for commands .. let’s call it cmdClient which its port is 11589
then a Job Thread started for this client like the code up.
then the client connect with another socket to the server .. dataClient which its port is 1800
then when i use the dataClient to send a file .. the server receives the bytes at the job Method .. !! why does that happene?
Yes, it is wise to use multiple ports when doing file transfers. It would require a quite advanced protocol to use the same port and still keep the application response (as you still have to be able to send commands during file transfers).
But I do not recommend you to use three fixed ports. Use one port for all commands and an arbitrary number of ports for the file transfers.
A file transfer would look like this:
That allows you to transfer several files at the same time. Let the server create a new listening socket using port 0. It tells the OS to select a free port. Then use
Socket.LocalEndpointto find out the port before sending it back in step #2.The specified approach also lets you take advantage of
Socket.SendFilewhich is probably the most effective and fastest way to send files using .NET.(FTP uses the same approach, as do bittorrent. You might have used a file manager when downloading files from the web. They takes a more extreme approach and splits up the file download over multiple sockets to get around web server bandwidth throttling.)
update in response to a comment:
You did not specify that information in the original question, which made me assume that you only transfer one file at a time.
Batch transfers would work in the same way, just change so that step #1 sends a list of filename+size and then send all files after each other in step #3.