Question about sockets


Hi, I have a question about sockets, I am going to try to explain everything to you so that you can solve this doubt I have, I hope not to disappoint you with my question. Well, the problem I have is that when I'm using a client I can put as many sockets as I want just as always:


And I can even use different sockets with the same ports. Which I find curious. But when I close these sockets in this way from the client


To use them again I can not have to create a second socket again to do something else or repeat the same thing I did before.

So my question is there is some way to close the sockets so that you can reuse them and even loop to repeat certain things. Since it is very annoying to create a socket every time I want to do something and also I am very limited

Try close but do not close.

asked by Perl 11.08.2016 в 18:01

1 answer


apparently you have enough doubts about sockets. First of all I would recommend you to study more deeply in terms of socket and threads.

In general there are two main and most used socket families that are TCP and UDP . TCP sockets are connection oriented (by the way the most used), UDP sockets work in disconnected mode.

When you use TCP socket you will have noticed that there are blocking methods, that is, they stop the execution of the program while waiting for their purpose, for example a server socket the accept method stops the execution of the program until a connection request arrives, then it creates a socket client, assigns it a port and from there it waits for another connection again blocking the execution of the program. A socket client is blocked when you use the recv method until the other socket on the other end sends data. It is very useful to use threads when you work with socket, normally the server socket should run on a thread and your main program receive the sockets for use, each client socket should also run on a particular thread when you use the recv method.

Maybe for the application that you are developing you need UDP sockets.

answered by 11.08.2016 в 18:48