嵌入式 hi3518c本地udp套接示例以及sendto用法
/*
Author : kj
Time :2013-12-24 22:05
Notice :
在一個.c中定義的全局變量,需要在其他.c中使用的話,需要的是在使用的.c中
extern struct sockaddr_un server_address_udp;
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
//#include <openssl/ssl.h>
//#include <openssl/err.h>
#include <pthread.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/route.h>
#include <unistd.h>
#include <signal.h>
#include <net/if_arp.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/vfs.h>
#define SOCK_UNIX_VIDEO_STREAM_FILE_SEVER "/tmp/video1_server"
#define SOCK_UNIX_VIDEO_STREAM_FILE_CLIENT "/tmp/video1_client"
int video_stream_net_func_udp(void)
{
#if 0
int server_sockfd;
//unlink (SOCK_UNIX_VIDEO_STREAM_FILE_SEVER);//delete the file link for the function of bind
server_sockfd = socket (AF_UNIX, SOCK_DGRAM, 0);
server_address_udp.sun_family = AF_UNIX;
//server_address_udp.sin_port=htons(PORT);
strcpy (server_address_udp.sun_path, SOCK_UNIX_VIDEO_STREAM_FILE_SEVER);
joseph_ipnc_s_socket.joseph_ipnc_s_socket_fd_udp_socket_size = sizeof (server_address_udp);
//bind (server_sockfd, (struct sockaddr *)&server_address_udp, server_len);
joseph_ipnc_s_socket.joseph_ipnc_s_socket_fd_udp = 0;
joseph_ipnc_s_socket.joseph_ipnc_s_socket_fd_udp = server_sockfd;
printf("%s %d The udp mode send to jss succeed ,joseph_ipnc_s_socket_fd_udp is %d !\n",__FUNCTION__,__LINE__,joseph_ipnc_s_socket.joseph_ipnc_s_socket_fd_udp);
#else
int client_sockfd, ret;
//struct sockaddr_un pmmanager_addr, pmapi_addr;
client_sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if(client_sockfd == -1)
{
perror("cannot create pmapi fd.");
}
unlink(SOCK_UNIX_VIDEO_STREAM_FILE_CLIENT); //configure pmapi's addr
memset(&server_address_client, 0, sizeof(server_address_client));
server_address_client.sun_family = AF_UNIX;
strcpy(server_address_client.sun_path, SOCK_UNIX_VIDEO_STREAM_FILE_CLIENT); //bind pmapi_fd to pmapi_addr
ret = bind(client_sockfd, (struct sockaddr*)&server_address_client, SUN_LEN(&server_address_client));
if(ret == -1)
{
perror("bind error.");
}
//configure pmmanager's addr
memset(&server_address_udp, 0, sizeof(server_address_udp));
server_address_udp.sun_family = AF_UNIX;
strcpy(server_address_udp.sun_path, SOCK_UNIX_VIDEO_STREAM_FILE_SEVER);
joseph_ipnc_s_socket.joseph_ipnc_s_socket_fd_udp = 0;
joseph_ipnc_s_socket.joseph_ipnc_s_socket_fd_udp = client_sockfd;
joseph_personal_video_print("%s %d The udp mode send to jss succeed ,joseph_ipnc_s_socket_fd_udp is %d !\n",__FUNCTION__,__LINE__,joseph_ipnc_s_socket.joseph_ipnc_s_socket_fd_udp);
#endif
return 0;
}
/*
int sendto ( socket s , const void * msg, int len, unsigned int flags, const struct sockaddr * to , int tolen ) ;
sendto(sockfd, buffer, len, 0, (struct addr*)&addr, addr_len);¡±
*/
s_length = sendto(joseph_ipnc_s_socket.joseph_ipnc_s_socket_fd_udp, video_buf, video_length, 0,(struct sockaddr*)&server_address_udp,sizeof(struct sockaddr));