WinSock - Going from TCP to UDP

closed account (2NywAqkS)
I've made a multiplayer game using a TCP connection. Although there isn't any lag when I run the server and client on the same computer, my friends say it lags quite bad on their computer(which is to be expected). Would I benefit from converting from a TCP connection to a UDP connection?

And if so is it a quick matter of changing a few lines of code in the set up function?

Thanks,
Rowan.
It uses less network bandwidth, so it will be faster. And as you're always sending out updates from the server, it doesn't really matter if the odd packet or so doesn't arrive or arrives malformed. You may have firewall issues though.

To go from a TCP server to a UDP server:
1. Call socket() with IP Protocol id. Windows has a constant for which I can't recall right now, but generally you'd call getprotobyname("udp") to get the protocol.

2. You still call bind() so clients can find you.

3. Don't call listen()/accept(), just call recvfrom() in a loop.

To go from a TCP client to a UDP client:
1. Call socket() as you do for the server.

2 Don't call connect(), just start sending stuff with sendto().
Last edited on
closed account (2NywAqkS)
thanks, although I'm still a bit confused. Do I get rid of
1
2
3
4
WSAAsyncSelect(ServerSocket,
					hWnd,
					WM_SOCKET,
					(FD_CLOSE|FD_ACCEPT|FD_READ));
? and if so how do I know when a player has joined and left? And how do I set up player Sockets on the server side?
normally I would use this in FD_ACCEPT;
playerVector.back().socket=accept(wParam,&sockAddrClient,&size);
I forgot that you're using that async stuff. I don't know much about those and I can't find any examples that use UDP.
WSAAsyncSelect can be used for UDP and TCP read/write. All you have to do is handle the window messages and you will be fine

MSDN "The WSAAsyncSelect function is used to request that WS2_32.DLL should send a message to the window hWnd when it detects any network event specified by the lEvent parameter. "
closed account (2NywAqkS)
Ok, I've almost mastered this. Although the server is picking up that something is being sent it's says that there's nothing in szIncoming. To get over the problem of it not detecting when a new client connects I've made the client send an "a" to the server like this;
1
2
3
std::stringstream stream;
	stream << "a";
	sendto(Socket, stream.str().c_str(), sizeof(stream), 0, (SOCKADDR *) & SockAddr, sizeof(SockAddr));

the server goes to FD_READ as hoped and recvives it like this;
1
2
3
4
5
char szIncoming[1024];
		ZeroMemory(szIncoming,sizeof(szIncoming));
		sockaddr clientAddr;
		int szClientAddr;
		int inDataLength = recvfrom(ServerSocket, (char*)szIncoming, sizeof(szIncoming),0, &clientAddr, &szClientAddr);

at this point the server says there's nothing in szIncoming.

the sockets are made like this.
1
2
struct protoent *proto = getprotobyname("udp");
			ServerSocket=socket(AF_INET,SOCK_DGRAM,proto->p_proto);


thanks
closed account (2NywAqkS)
dw, I solved it.
I needed to do this in FD_READ:
1
2
int szClientAddr = sizeof(clientAddr);
		int inDataLength = recvfrom(ServerSocket, (char*)szIncoming, sizeof(szIncoming),0, (sockaddr*)&clientAddr, &szClientAddr);
Topic archived. No new replies allowed.