共享內存的操作函數
#include <sys/shm.h>
int shmget(key_t key, int size, int shmflg);
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
void *shmat(int shmid, const void *shmaddr, int shmflg);
int shmdt(const void *shmaddr);
Unix域socket通信
#include<sys/un.h>
struct sockaddr_un
{
unit8_tsun_len;
sa_family_t sun_family;
charsun_path[104];
};
#include<sys/socket.h>
socket操作的相關函數
使用select調用處理多路管道
#include <sys/time.h>
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
void FD_ZERO(fd_set *fdset);/*initialize the mask pointed to by fdset */
void FD_SET(int fd, fd_set *fdset); /*set the bit, fd, in the mask pointed to by fdset*/
/*isthe bit, fd, set in the mask pointed to by fdset*/
void FD_ISSET(int fd, fd_set *fdset);
void FD_CLR(int fd, fd_set *fdset);
struct timeval{
long tv_sec;
long tv_usec;
};
調用實例
#include <sys/time.h>
#include <sys/type.h>
#include <fcntl.h>
……
int fd1, fd2;
fd_set readset;
fd1= open(“file1”, O_RDONLY);
fd2= open(“file2”, O_RDONLY);
FD_ZERO(&readset);
FD_SET(fd1, &readset);
FD_SET(fd2, &readset);
Switch (select(5, &readset, NULL, NULL, NULL))
{
……
}
#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>
#include <sys/un.h>
#define JOSEPH_IPNC_SYS_NAME "av_server"
#define SOCK_UNIX_VIDEO_STREAM_FILE "/tmp/video1_sock"
#define SOCK_UNIX_VIDEO_STREAM_FILE_SEVER "/tmp/video1_server"
#define SOCK_UNIX_VIDEO_STREAM_FILE_CLIENT "/tmp/video1_client"
struct sockaddr_un server_address_udp;
struct sockaddr_un server_address_client;
extern struct sockaddr_un server_address_udp;
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;
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);
#endif
return 0;
}
void net_send_stream_first(void *video_buf,int video_length)
{
int s_length;
#if 0
if(video_length <= 8) return;
#else
if(video_length <= 4) return;
#endif
if(joseph_ipnc_s_socket.joseph_ipnc_s_scoket_sta == 1)
{
#if 1
char buf_time[128] = {0};
memset(buf_time,0,128);
get_current_time(buf_time);
printf("%s %d %s send begin time is : \n",__FUNCTION__,__LINE__,buf_time);
printf("%s %d The num is %d ,The buf size is %d !\n",__FUNCTION__,__LINE__,joseph_ipnc_param.joseph_ipnc_network_attr.joseh_send_to_jss_frame_num,video_length);
printf("%s %d joseph_ipnc_s_socket.joseph_ipnc_s_socket_fd_udp is %d \n",__FUNCTION__,__LINE__,joseph_ipnc_s_socket.joseph_ipnc_s_socket_fd_udp);
#endif
#if 0
s_length = send(joseph_ipnc_s_socket.joseph_ipnc_s_scoket_fd, video_buf, video_length, 0);
#else
/*
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,SUN_LEN(&server_address_udp));
#endif
if(s_length <= 0)
{
printf("%s %d The send is err, The length is %d !!! \n",__FUNCTION__,__LINE__,s_length);
close(joseph_ipnc_s_socket.joseph_ipnc_s_scoket_fd);
//close(joseph_ipnc_s_socket.joseph_ipnc_s_socket_fd_udp);
joseph_net_lock();
joseph_ipnc_s_socket.joseph_ipnc_s_scoket_sta = -2;
joseph_net_unlock();
//exit(-2);
}
else
{
//printf("%s %d net_send_stream_first succeed \n",__FUNCTION__,__LINE__);
//do nothing
}
#if 0
memset(buf_time,0,128);
get_current_time(buf_time);
printf("%s %d %s send end time is : \n",__FUNCTION__,__LINE__,buf_time);
#endif
}
else
{
// do nothing
}
}