Is it wise to use 3 ports for a server application?

I have a client server application [TCP Sockets, .NET 4.0] ..
application about:

execute commands received from the client receives files send screenshot image
the application must perform 3 tasks at the same time
After I did this and it worked .. I realized that such an application should use one port for all tasks. For example, Radmin and netsupport .. etc.
but I used 3 ports .. one to keep receiving commands anytime the client sends. and one for receiving files .. and one if the client asked for a screenshot

is it good to use 3 ports for a network application? ..

I tried to create 3 sockets on the client side to connect to the server on the same port (ex: 9090)
but when I tried to send the file with the client, the server received bytes in the Job function, which should only accept commands ... so it looks like I can not use one port for these tasks, so it is possible to use one port for all three tasks that they can work simultaneously?

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 } } 

Question added:
let's say the server has one port.
the client will connect to the server for commands .. let it call cmdClient, which has port 11589, then Job Thread, such as the up code, was launched for this client. then the client connects to another socket on the server .. dataClient, which its port is 1800, then when I use dataClient to send the file .. the server receives bytes in the Method task .. !! Why is this happening?

+4
source share
2 answers

Yes, it is wise to use multiple ports when transferring files. Using the same port will require a fairly advanced protocol and still save the application response (since you still have to send commands while transferring files).

But I do not recommend you use three fixed ports. Use one port for all commands and an arbitrary number of ports for transferring files.

The file transfer will look like this:

  • (CmdPort) Client → Server Hello, I want to transfer the XXX file with the size YYYY
  • (CmdPort) Server -> Roger Client , connect to port 8217 and transfer the file
  • (8217) Client → Server . Connects, transfers the entire file, disconnects
  • (8217) Server Verifies that the transferred size matches the one specified in step # 1

This allows you to transfer multiple files at once. Let the server create a new auditory socket using port 0. It tells the OS to select a free port. Then use Socket.LocalEndpoint to find out the port before sending it back in step 2.

This approach also allows you to use Socket.SendFile , which is probably the most efficient and fastest way to send files using .NET.

(FTP uses the same approach as bittorrent. You may have used the file manager when downloading files from the Internet. They use a more extreme approach and share file downloads across multiple sockets to bypass the web server bandwidth.)

update in response to comment:

my application also sends folders, there can be 2000 files in a folder, a large file can be less than a kilobyte, it would be a huge mistake to connect for each file .. or maybe I should connect once for each task (folder / file)

You did not provide this information in the original question, which made me assume that you only transfer one file at a time.

Batch translations will work the same way, just change so that step # 1 sends a list of file name + size and then sends all the files to each other in step # 3.

+2
source

It is not recommended to use 3 ports, unless each operation was its own application, i.e. a file server, image server, etc. You have to create one application listening on 1 port, and then use Threading to accept several connections to the server (each of which has its own StreamReader instance). Then you can write a parser method to enter users. For example, if a user sends a “GET FILE” command, the server expects a file; if a user sends a “PICTURE” command, the server would expect an image.

An example of multithreaded servers can be found here:

http://csharp.net-informations.com/communications/csharp-multi-threaded-server-socket.htm

0
source

Source: https://habr.com/ru/post/1402611/


All Articles