Can someone give me an example on how to listen on multiple ports with Winsock? I have been trying for hours with no luck..

Recommended Answers

All 5 Replies

I haven't done it, but I dont think that it should be much different than listening in a single port...Just create SocketNumber1 and bind it to portnumber1, create SocketNumber2 and bind it to portnumber2, and listen on both sockets. Depending on the Socket I/O Model, you will get which request comes to which socket. Give what you have tried so far, and maybe someone will be able to see what you are doing wrong.

Thanks for the reply,
I can do it easily using the method you described (create a socket for each individual port i want to listen on).. is there an easier way? i want to be able to listen on 15+ ports and that means i'm going to have to create an idividual socket for each port i want to listen on, so 15+ sockets.. it would be nice if i could just use one socket for listening on several ports.

>it would be nice if i could just use one socket for listening on several ports.
Too bad it doesn't work that way. You have to have multiple sockets to listen on multiple ports, whether it be something like an array of sockets in the thread, or multiple threads that work with a single socket.

I am not 100% sure about this, but I dont think you can do that. Maybe someone who has more experiance in network programming will disagree, but out of the Socket Models that I have used upto now, when you get a connection request, you can only tell to what socket is that request made. There is no way of differentiating from which port-number.

By the way, why do you need 15 different port numbers? That means you are trying to use 15 different protocols. What are you trying to do?

I was messing around with a client-server p2p application, I guess I will just go with the first method suggested (creating a socket for each port), thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.