linux网络编程-udp通信(二)
我们直接进入函数,由于网站编辑器不太好,所以会有点乱
这是一个udp server ,注意udpserver使用到的函数
/*******************************************************************************
* 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];
u8 loca_id;
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(&loca_id);
printf("socketfd_local value %d loca_id : %d \n",socketfd_local,loca_id);
bzero (&server, sizeof (struct sockaddr));//清空socket的结构体
server.sin_port = htons (5013);//这里用做服务器 监听5013这个端口
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;//使用任意网卡的ip地址,在这里如果有有线网卡和无线网卡 都可以配置
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);//使用select函数进行延时 500ms
ret = select(socketfd_local+1,&rfds,NULL,NULL,&tm);//socketfd要加1 是因为 select的函数不识别0
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, \ // udp使用这个函数读取接收数据
(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]);
}
}
}
}
}
u8 socket_init(u8 *loc_id )
{
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){//创建一个socket函数
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;
*loc_id = socket_id;
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;
}
头文件
/* 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>


