views:

295

answers:

2

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.

+1  A: 

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.

PiotrLegnica
I forgot to mention, this is in vb.net by the way.
Cyclone
Then it shouldn't be tagged as WinSock question. I've added .NET sockets to answer.
PiotrLegnica
Okay, I guess I am just confused lol. Thanks for the help!
Cyclone
+1  A: 

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.

Chaoz