Cyclone
Cyclone

Reputation: 18295

How to make a winsock listen on a particular port?

This must be really obvious, but how do I make a winsock listen to a specific port (recieve data?) I cannot find a method that lets me do so!

Sorry that this is probably so obvious.

I am trying to make a chat application, which is obviously running in a server/client setup.

Upvotes: 1

Views: 3115

Answers (2)

Cat Plus Plus
Cat Plus Plus

Reputation: 129764

You bind the socket, and then listen and accept connections. Described in more detail in Getting started with WinSock. There's also a good guide to the BSD sockets, which you should read too.

For .NET, you probably want System.Net.Sockets.Socket (and it's Bind/Listen/Accept/Send/Receive methods) instead of native WinSock, but the concepts are the same, so I'll leave all links.

Upvotes: 1

Charles
Charles

Reputation: 2661

When using TCP sockets you bind, listen and accept and eventually recv. You can find some tutorials over here or check out the official MSDN documentation.

The winsock API is based on UNIX' BSD sockets; so they are very very similar. You might want to look into that. A book that I've recently is UNIX Network Programming by Richard Stevens and he explains pretty much everything there is to know about sockets.

You might also consider running your sockets in a separate thread to improve readability and performance.

Edit: As for listening on a specific port; this is the sin.port parameter of your SOCKADDR_IN struct.

Upvotes: 1

Related Questions