I'm trying to make a simple client and server with UDP but am running into issues. Here's what I'm doing. I get the server setup fine and loop a recvfrom() function. Then I run my client which calls a sendto() function however my server never recieves anything if anyone could help it would be much appreciated. Here's some of my code On my server this function is called at initialisation
void SetupLoginConnection()
{
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
{
AddString(TEXT("Error at WSAStartup()"));
}
UDPSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( UDPSocket == INVALID_SOCKET )
{
AddString(TEXT("Error at socket():"));
closesocket(UDPSocket);
WSACleanup();
return;
}
sockaddr_in service;
hp = gethostbyname("localhost");
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(32459);
if ( bind( UDPSocket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR )
{
AddString(TEXT("bind() failed."));
closesocket(UDPSocket);
WSACleanup();
return;
}
AddString(TEXT("Waiting for request on port:"));
}
Then this function is called in the main server loop
void SendRcv()
{
char inbuffer[256];
int test;
int len = sizeof(SOCKADDR);
memset(inbuffer, '\0', 256);
if(test = recvfrom(UDPSocket, inbuffer, 256, 0, NULL, &len) >= 0)
{
AddString(TEXT("Packets recieved!"));
MessageBox(NULL, NULL, TEXT("Incoming Data"), MB_OK);
}
if(test == SOCKET_ERROR)
{
MessageBox(NULL, NULL, TEXT("FUCK!!"), MB_OK);
}
}
Now here's my client main function
int _tmain(int argc, char* argv[])
{
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
{
printf("error");
}
UDPSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( UDPSocket == INVALID_SOCKET )
{
printf("socket error");
WSACleanup();
return 1;
}
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(32459);
char buffer[256];
int len = sizeof(SOCKADDR);
int test;
strcpy(buffer,"HELLO!!!");
test = sendto(UDPSocket, buffer, sizeof(buffer), 0, (SOCKADDR *)&service, sizeof(SOCKADDR));
if(test == SOCKET_ERROR)
{
printf("no data sent");
}
if((test == SOCKET_ERROR) || (test < 0))
{
printf("FUCK!!");
}
else
{
printf("%s sent",buffer);
}
closesocket(UDPSocket);
WSACleanup();
return 0;
}
Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)