Hi everyone,
I am using UDP sockets to create a simple token ring protocol for one of my classes. For this to work I need to be able to send and receive between the client programs and the server.
What is working:
I send to the server program from the client
The server receives and stores the data
The server sends data back to the client
What isnt working:
The client isn't receiving the data, it is just blocking on the recvfrom call.
Any hints would be great, keep in mind this is an assignment.
Client Code:
Code:
/*
* Create a socket
* PF_INET : Protocol Family IPv4 Internet Protocol
* SOCK_STREAM : datagrams (connectionless, unreliable messages)
* 0 : Only used if multiple protocols in family
*/
socketFD = socket( PF_INET, SOCK_DGRAM, 0 );
if ( socketFD < 0 )
{
perror( "recvUDP:socket()" );
return -1;
}
/*
* Get the name of this host
*/
val = gethostname( hostname, 32 );
if ( val < 0 )
{
perror( "recvUDP:gethostname()" );
return -1;
}
/*
* Get DNS information about the server
*/
destptr = gethostbyname(argv[1]);
if (destptr == NULL)
{
perror( "recvUDP:gethostbyname()" );
return -1;
}
/*
* Clear all bits in srcAddrstructure
*/
memset( &srcAddr, 0, srclen );
memset( &destAddr, 0, srclen );
/*
* Copy server (registry) destination information into destAddr structure
*/
destAddr.sin_family = PF_INET;
memcpy( &destAddr.sin_addr, destptr->h_addr, destptr->h_length );
destAddr.sin_port = htons( (u_short)atoi(argv[2]) );
/* create message to send to server */
strcpy(buffer, "");
/* send initial message to the server proram */
sendlen = strlen(buffer) - 1;
int bytesSent = sendto(socketFD,
buffer,
(size_t)sendlen,
0,
(struct sockaddr *)&destAddr,
(socklen_t)sizeof(destAddr)
);
/* error checks */
if(bytesSent < 0)
{
perror( "sendUDP:sendto()" );
return -1;
}
if(bytesSent < sendlen)
{
fprintf(stderr, "sendUDP:sendto() - short write\n");
return -1;
}
/* close bound socket */
close(socketFD);
/* allocate a new socket */
socketFD = socket( PF_INET, SOCK_DGRAM, 0 );
if ( socketFD < 0 )
{
perror("sendUDP:socket()");
return -1;
}
/*
* Bind a name to the socket */
val = bind( socketFD, (struct sockaddr *)&srcAddr, (socklen_t)srclen);
if ( val < 0 )
{
perror( "recvUDP:bind()" );
return -1;
}
/*
* Recv the data from the server registry
* The data received will be the machine/port that this program sends to
*/
fromlen = sizeof(from);
memset(buffer, 0, sizeof(buffer));
val = recvfrom( socketFD, //!!!! The client sits here and does nothing
buffer,
(size_t)sizeof(buffer),
0,
(struct sockaddr *)&from,
(socklen_t *)&fromlen
);
printf("val: %d\n", val);
/* get the client machine's information */
fromptr = gethostbyaddr((char *)&from.sin_addr.s_addr, sizeof(from.sin_addr.s_addr), PF_INET);
printf("Buffer: %s\n", buffer);
/* error checks*/
if(fromptr == NULL)
{
perror("recvUDP:gethostbyaddr()");
return -1;
}
if ( val < 0 )
{
perror( "recvUDP:recvfrom" );
return -1;
}
/* print what the client received */
printf("Server program on [%s] says: \"%s\"\n\n", fromptr -> h_name , buffer);
(note: the client source file is called recvUDP.c but it is supposed to send and receive)
Thanks,
Hunter