| /*
* filename: broadcast_serv.c
* DESC: the sever endpoint of the UDP broadcast program
* Author: linjian
* Date: 2008/10/28
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "../my_error.h"
#define PORT 8003
#define BUF_SIZE 512
int main( int argc, char *argv[] )
{
int sockfd, connfd;
int optval;
int client_len;
struct sockaddr_in serv_addr, client_addr;
char recv_buf[ BUF_SIZE ];
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
my_error( "socket failed" );
memset( &serv_addr, 0, sizeof(struct sockaddr_in) );
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons( PORT );
serv_addr.sin_addr.s_addr = htons( INADDR_ANY );
if ( bind(sockfd, (struct sockaddr *)&serv_addr,
sizeof(struct sockaddr)) < 0 )
my_error( "bind failed" );
client_len = sizeof( struct sockaddr_in );
if ( recvfrom(sockfd, recv_buf, BUF_SIZE, 0,
(struct sockaddr *)&client_addr,
&client_len) < 0 )
my_error( "recvfrom failed" );
printf( "recv from client: %s\n", recv_buf );
close( sockfd );
return 0;
}
|