UDP VC++ Echo problem in server program
Hi:
I have a UDP server program where all I want to do is broadcast out a
universal message on 192.168.0.255 (if 255 is a universal broadcast ->
255.255.255.0 will not work b\c my router will stop it).
I keep getting back my echo (12345678) instead of the client response
(23456789).
Please help. code follows:(BUFFER is defined as 8)
**********************************************
int main(int argc, char **argv)
{
WSADATA w; /* Used to open windows connection */
unsigned short port_number; /* Port number to use */
int a1, a2, a3, a4; /* Components of address in xxx.xxx.xxx.xxx
form */
int client_length; /* Length of client struct */
int bytes_received; /* Bytes received from client */
SOCKET sd; /* Socket descriptor of server */
struct sockaddr_in client; /* Information about the client */
char buffer[BUFFER_SIZE]; /* Where to store received data */
struct hostent *hp; /* Information about this computer */
char host_name[256]; /* Name of the server */
char sendBuffer[8];
unsigned char universalByte = 255;
int iExit=0;
/* Interpret command line */
if (argc == 2)
{
/* Use local address */
if (sscanf(argv[1], "%u", &port_number) != 1)
{
usage();
}
}
else if (argc == 3)
{
/* Copy address */
if (sscanf(argv[1], "%d.%d.%d.%d", &a1, &a2, &a3, &a4) != 4)
{
usage();
}
if (sscanf(argv[2], "%u", &port_number) != 1)
{
usage();
}
}
else
{
usage();
}
/* Open windows connection */
if (WSAStartup(0x0101, &w) != 0)
{
fprintf(stderr, "Could not open Windows connection.\n");
exit(0);
}
/* Open a datagram socket */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
exit(0);
}
/* Clear out server struct */
memset((void *)&client, '\0', sizeof(struct sockaddr_in));
printf("Press CTRL + C to quit\n");
/* Set family and port */
client.sin_family = AF_INET;
client.sin_port = htons(port_number);
/* Bind address to socket */
if (bind(sd, (struct sockaddr *)&client, sizeof(struct sockaddr_in))
== -1)
{
fprintf(stderr, "Could not bind name to socket.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
/* Get host name of this computer */
gethostname(host_name, sizeof(host_name));
hp = gethostbyname(host_name);
/* Check for NULL pointer */
if (hp == NULL)
{
fprintf(stderr, "Could not get host name.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
/* Print out server information */
printf("Server running on %u.%u.%u.%u\n", (unsigned
char)hp->h_addr_list[0][0],
(unsigned char)hp->h_addr_list[0][1],
(unsigned char)hp->h_addr_list[0][2],
(unsigned char)hp->h_addr_list[0][3]);
universalByte=192;
client.sin_addr.S_un.S_un_b.s_b1 = universalByte;
universalByte=168;
client.sin_addr.S_un.S_un_b.s_b2 = universalByte;
universalByte=0;
client.sin_addr.S_un.S_un_b.s_b3 = universalByte;
universalByte=255;
client.sin_addr.S_un.S_un_b.s_b4 = universalByte;
/* Loop and get data from clients */
while (iExit==0)
{
client_length = (int)sizeof(struct sockaddr_in);
strcpy(sendBuffer,"12345678");
/* Send data back */
if (sendto(sd, (char *)sendBuffer, (int)sizeof(sendBuffer), 0,
(struct sockaddr *)&client, client_length) != (int)sizeof(sendBuffer))
{
fprintf(stderr, "Error sending datagram.\n");
}
else
{
/* Receive bytes from client */
bytes_received = recvfrom(sd, buffer, BUFFER_SIZE, 0, (struct
sockaddr *)&client, &client_length);
if (bytes_received < 0)
{
fprintf(stderr, "Could not receive datagram.\n");
}
else
{
fprintf(stderr, "Made contact with a client: %s \n",buffer);
printf("Client running on %u.%u.%u.%u\n", (unsigned
char)client.sin_addr.S_un.S_un_b.s_b1,
(unsigned char)client.sin_addr.S_un.S_un_b.s_b2,
(unsigned char)client.sin_addr.S_un.S_un_b.s_b3,
(unsigned char)client.sin_addr.S_un.S_un_b.s_b4);
iExit=1;
}
}
}
closesocket(sd);
WSACleanup();
return 0;
}