Hi everyone,
I'm trying to get my client to send a packet with some text and my server to receive the packet and display the text.
Currently I'm trying this locally so what I'm doing now is the following.
1) I send some text using sendto()
2) I try to receive the data using recvfrom()
The problem is that the recvfrom data doesn't seem to receive a packet. The two methods that I'm using are coded like this:
Used in step 1:
bool Networking::SendPacket(const void * message)
{
// Set up address to send the packet to
unsigned int a = 127;
unsigned int b = 0;
unsigned int c = 0;
unsigned int d = 1;
unsigned short port = 2890;
unsigned int destination_address = ( a << 24 ) | ( b << 16 ) | ( c << 8 ) | d;
unsigned short destination_port = port;
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl( destination_address );
address.sin_port = htons(destination_port);
// Try to send the packet
int sent_bytes = sendto(handle, (const char*)message, sizeof(message), 0, (sockaddr*)&address, sizeof(sockaddr_in));
// Check if the packet was sent successfully
if (sent_bytes != sizeof(message))
{
printf("failed to send packet: return value = %d\n", sent_bytes);
return false;
}
else
{
cout << "this packet was successfully sent: " << (const char*)message << "\n";
return true;
}
return true;
}
Used in step 2:
void Networking::ReceivePacket()
{
unsigned char packet_data[256];
#if PLATFORM == PLATFORM_WINDOWS
typedef int socklen_t;
#endif
sockaddr_in from;
socklen_t fromLength = sizeof( from );
int received_bytes = recvfrom(handle, (char*)packet_data, sizeof(packet_data), 0, (sockaddr*)&from, &fromLength );
if (received_bytes <= 0)
return;
cout << "RECEIVED DATA: " << (char*)packet_data << "\n";
}
Does anyone see what I'm doing wrong?
Thanks in advance