UDP winsock2.h. New to C++ networking

Started by
11 comments, last by hplus0603 17 years, 9 months ago
I've done networking in other languages for fun before, however even with my experience in C++, I don't know how to do networking. I want to use the UDP protocol (since that is what I usually use in other languages). I have a few questions. What's winsock2.h? I've heard of winsock.h, is winsock2.h the newer version? Do you guys know of any quick tutorials for setting up a UDP connection and sending and getting packets. I've already made a buffer class that can store my packets I just need a basic set up for sending and getting data using a winsock connection with UDP. Any input you guys can give me will help a lot, (I'm making a "multiplayer" game....)
Advertisement
Yes, winsock2.h is for version 2 of WinSock -- available since Windows 95 or so. It's the version you want to be using.

Here are the functions you'll need, in order. Check them out on MSDN to get code samples and descriptions:

Server:
- WSAStartup() (pass 2,2 for the version)
- socket() to create a IP datagram UDP socket.
- bind() with htons() for the port number
- select() to test whether there's data to read
- recvfrom() to receive data from clients
- sendto() to return data to clients

Client:
- WSAStartup()
- gethostbyname() to find the host, htons() to transform the port number
- socket() to create a IP datagram UDP socket.
- select() to test whether there's data to read
- sendto() to send to the server
- recvfrom() to receive from the server

enum Bool { True, False, FileNotFound };
Quote:Original post by hplus0603
Yes, winsock2.h is for version 2 of WinSock -- available since Windows 95 or so. It's the version you want to be using.

Here are the functions you'll need, in order. Check them out on MSDN to get code samples and descriptions:

Server:
- WSAStartup() (pass 2,2 for the version)
- socket() to create a IP datagram UDP socket.
- bind() with htons() for the port number
- select() to test whether there's data to read
- recvfrom() to receive data from clients
- sendto() to return data to clients

Client:
- WSAStartup()
- gethostbyname() to find the host, htons() to transform the port number
- socket() to create a IP datagram UDP socket.
- select() to test whether there's data to read
- sendto() to send to the server
- recvfrom() to receive from the server



Why should the server bind() and not the client... and if I connect() my UDP socket, can I still put them in my fd_sets for select() ?
Yes, you can send and receive on the same socket. You need to bind() on the server so that the clients can find a known port. Clients do not need to have a known port (and making one will limit debuggability and compatibility of the system).

You do not, in general, connect() UDP sockets; instead, you use sendto().
enum Bool { True, False, FileNotFound };
okay, the select() is confusing me. I thought I understood it, but even with the last parameter set to 0, the function hangs the whole program.
Quote:
fd_set socketSet;
FD_ZERO(&socketSet);
FD_SET(RecvSocket, &socketSet);

while(select(0,&socketSet,0,0,0) != 0){
std::cout<<"Receiving datagrams...\n";
recvfrom(RecvSocket, RecvBuf, BufLen, 0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
std::cerr << RecvBuf << "\n";
}

std::cerr<<"DONE WITH LOOP\n";
First: the first argument to select() should be the highest socket number you pass in, plus one. However, WinSock ignores this argument, so you can get away with 0 on Windows.

Second: If you pass 0 for timeout in select(), it will block (it says as much in the documentation). You need to pass a pointer to a timeval, which in turn contains the timevalue value 0; this will make select() just poll the sockets and return.
enum Bool { True, False, FileNotFound };
evolving this thread, since im also new to udppackets using winsock, how do i send a broadcast message from client to server?
what i want to do is simply send a message from client to get the sockaddr from the server (via sendto() and recvfrom()), but i can't seem to get the message from the client to the server.
any pointers..?
if you want the server to send the sockaddr info how will you send the information in the first place ......

you need port + address pairs to communicate in UDP . The packet should know from sockaddr structure where to find the server .
Look in the Forum FAQ for "how does the client find the server" -- there's one answer for the greater internet (use DNS) and another answer for LAN play (use broadcasting).
enum Bool { True, False, FileNotFound };
Quote:Original post by atultuff
if you want the server to send the sockaddr info how will you send the information in the first place ......

you need port + address pairs to communicate in UDP . The packet should know from sockaddr structure where to find the server .


well, what i meant was, i do everything i thought was right, but it still doesnt work.
i init winsock, create the socket, bind and listen for broadcastmessages (via recvfrom), but when the client sends a broadcastmessage the server says nothing. but the sendto function in the client returns succesfully, just that recvfrom in the server doesnt get anything.
what i know of, the sendto and recvfrom contains a sockaddr structure about the remote computer. so the server has to reply to the broadcastmessage, and the client knows the servers ipadress. i could be wrong though..
guess i couldve defined "any pointers..?" better.. my fault..

Quote:hplus0603
Look in the Forum FAQ for "how does the client find the server" -- there's one answer for the greater internet (use DNS) and another answer for LAN play (use broadcasting).


thanks, but what i was looking for was a simple tutorial for broadcasting.. i know both to use tcp/ip and udp, but its just the broadcasting i cant get to work.
thanks to both for your fast answers!

This topic is closed to new replies.

Advertisement