linux下udp_server的实例

/* OS Specific Headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>

/*Socket*/
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <linux/ip.h>
#include <sys/un.h>
#include <fcntl.h>
u8   socket_init( )
{
s32 socket_id = 0 ;
s32 i = 0 ;
u8 ret;
int on = 1;
int  opts = 0 ;

printf("creat  socket_init    \n");
if ((socket_id = socket (AF_INET, SOCK_DGRAM, 0)) == -1){
 printf("creat  fail   socket \n");
return (-1);
}else {
 printf("creat  ok   socket \n");

}
opts = fcntl(socket_id,F_GETFL);
   if (opts < 0 ){
         printf("opts  value error     \n");
return (-1);
   }else {

printf("opts  value %d     \n",opts);
}

   opts  =  opts | O_NONBLOCK;
   if (fcntl(socket_id,F_SETFL,opts) < 0 ){
       printf("fcntl set fl failed\n" );
return (-1);
   }else {
printf("fcntl set fl ok\n" );
}

unsigned char  service_type = 0xe0 | IPTOS_LOWDELAY|IPTOS_RELIABILITY;
if(setsockopt(socket_id, SOL_IP/*IPPROTO_IP*/, IP_TOS, (void *)&service_type, sizeof(service_type)) < 0)
perror("[app_task_spawn]setsockopt(IP_TOS) failed:");

on = 1;
printf("socket_id  value %d     \n",socket_id);
//  *socket_idv =socket_id;
ret = setsockopt( socket_id, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );
        return    socket_id;
} 
/*******************************************************************************
* function name : main
* description   : main function for control_engine
* param[in] : none
* param[out] : none
* return : 0-success,-1-fail
*******************************************************************************/
s32 main (int argc, char ** argv)
{

  int listenfd=-1;
    int connfd;
s32 num_bytes = 0 ;
u8 ret=0;
u8 i;
fd_set rfds; 
u8 msg_buf[1024];

struct timeval tm;
struct sockaddr_in servaddr;
struct sockaddr_in client;
struct sockaddr_in server;
int socketfd_local = -1;
socklen_t client_len = sizeof(struct sockaddr);

socketfd_local= socket_init();
printf("socketfd_local  value %d     \n",socketfd_local);
bzero (&server, sizeof (struct sockaddr));
server.sin_port = htons (5013);
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;

if (bind (socketfd_local, (struct sockaddr *) &server, sizeof (struct sockaddr_in)) == -1){
printf("fail  bind \n");
   }
   else { 
       printf("success  bind \n");
   }

while(1)
{

tm.tv_sec  = 0;
tm.tv_usec = 500000;
        FD_ZERO(&rfds);
FD_SET(socketfd_local,&rfds);
ret = select(socketfd_local+1,&rfds,NULL,NULL,&tm);
if(ret==0){
printf( "ret:%d\n",ret) ; 
                continue; 

}if(ret<0){
printf( "select error") ; 

}

if(FD_ISSET(socketfd_local,&rfds)) {

printf( "FD_ISSET ok \n") ; 

if (0 <  (num_bytes = recvfrom (socketfd_local, msg_buf, 1024, 0, \
(struct sockaddr *) &client, &client_len))){
printf("  recvfrom %d \n",num_bytes);
if (num_bytes < 0 && (errno == EINTR || errno == EWOULDBLOCK || errno == EINPROGRESS)){
printf("  recvfrom %d error  \n",num_bytes);
continue;
}
for(i=0;i<10;i++){

printf("msg_buf[%d]:%x  bind \n",i,msg_buf[i]);
}

}

}

}

} udpserver5.png

sitemap