Their isn't one tutorial that can tell you everything you will need to know, I learnt about winsock years ago in visual basic. You can find an example of a c++ client / server by searching for c++ tcp client, tcp server. Getting a connection isn't the hard part the hard part is structuring your server and client properly that's why I made this post here to get other peoples opinions on using a global stream and a local stream.
Anyway the first thing you need to do is start up winsock using the WSAStartup function, you have to pass the version of winsock and the address of a WSADATA structure.
Then you need to setup the socket it will look a bit like this
1 2 3
|
/* Socket descriptor */
int sock;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
so for the server you have to make a sockaddr_in structure then bind it to your socket descriptor which is in the code above its simply an int which contains a value unique to your socket.
Your sockaddr_in structure contains things like the port and ip
So once you are binded you then need to listen you can do this like this
listen(sock, 5);
Then you use the accept function and as long as you haven't set the non blocking flag it won't execute any code after it until someone is connected. But that's just for the thread the accept function was called in of course.
Once someone connects the accept function will return a socket descriptor. You can then send data to it using the send function and receive data using the recv function.
And its a similar thing for the client except you don't need to bind it or listen and you use the connect function.
This is just for TCP though, UDP is a bit of a different setup and its connectionless so you send data using the sendto function.