PracticeDev/study_clang/ipc_test/unix_socket/mult_udp/point2.cpp

139 lines
3.8 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*************************************************************************
> File Name: point2.cpp
> Author: TianLun Song
> Mail: songtianlun@frytea.com
> Blog: https://blog.frytea.com
> Created Time: Wed 13 Jan 2021 10:14:09 AM CST
************************************************************************/
#include "point.h"
void *t_client_1(void *args)
{
printf("Start UDP Client 1.\n");
char msg_buf[1024];
int iMsgIndex = 0;
memset(&client_addr1,0,sizeof(client_addr1));
client_addr1.sun_family = AF_UNIX;
strcpy(client_addr1.sun_path,client1_file);
socket_fd_client_1 = socket(AF_UNIX,SOCK_DGRAM,0);
if (socket_fd_client_1 < 0)
{
perror("client 1 socket");
return NULL;
}
if (access(client_addr1.sun_path,0) != -1)
{
remove(client_addr1.sun_path);
}
if(bind(socket_fd_client_1,(sockaddr*)&client_addr1,sizeof(client_addr1)) < 0)
{
perror("client 1 bind");
return NULL;
}
while(1)
{
snprintf(msg_buf,sizeof msg_buf, "I'm UN_UDP client 1, NO.%d.", iMsgIndex++);
int ssize = ipc_udp_client_send(socket_fd_client_1, msg_buf, sizeof msg_buf, 0);
if (ssize < 0)
{
printf("sent error to: %s\n",server_addr1.sun_path);
perror("client 1 sendto ");
sleep(1);
continue;
}
int rsize = ipc_udp_client_recv(socket_fd_client_1, msg_buf, sizeof(msg_buf), 0);
if (rsize < 0)
{
perror("client 1 recv");
sleep(1);
continue;
}
cout << "UN_UDP client 1receive a msg :" << msg_buf << endl;
sleep(1);
}
if (close(socket_fd_client_1) < 0)
{
perror("client 1 close");
return NULL;
}
return NULL;
}
void *t_server_2(void *args)
{
char msg_buf[1024];
int iMsgIndex = 0;
printf("Start UDP Server 2.\n");
socket_fd_server_2 = socket(AF_UNIX,SOCK_DGRAM,0);
printf("server 2 fd %d after socket.\n", socket_fd_server_2);
if (socket_fd_server_2 < 0)
{
perror("server 2 socket");
return NULL;
}
if (access(server_addr2.sun_path,0) != -1)
{
remove(server_addr2.sun_path);
}
if (bind(socket_fd_server_2,(sockaddr*)&server_addr2,server_addr_len2) < 0)
{
printf("server 2 fd %d after bind.\n", socket_fd_server_2);
printf("bind to %s error.\n", server_addr2.sun_path);
perror("server 2 bind");
return NULL;
}
while (1)
{
memset(msg_buf,'\0',1024);
int rsize = ipc_udp_server_recv(socket_fd_server_2,msg_buf,sizeof(msg_buf),0);
if (rsize < 0)
{
perror("server 2 recv error");
sleep(1);
continue;
}
printf("UN_UDP server 2, fd: %d, receive a msg from %s :%s\n",socket_fd_server_2 , client_addr2.sun_path, msg_buf);
snprintf(msg_buf,sizeof msg_buf, "OK,I got it! No.%d",iMsgIndex++);
int ssize = ipc_udp_server_send(socket_fd_server_2, msg_buf, sizeof msg_buf,0);
if (ssize < 0)
{
perror("server 2 send error");
sleep(1);
continue;
}
sleep(1);
}
if (close(socket_fd_server_2) < 0)
{
perror("server 2 close socket");
return NULL;
}
return NULL;
}
int main(int argc,char** argv)
{
pthread_t tid[2];
int iRet1, iRet2 = 0;
init_socket();
iRet1 = pthread_create(&tid[0], NULL, t_client_1, NULL);
if(iRet1 != 0){
printf("pthread_create 1 error: error_code = %d\n", iRet1);
}
iRet2 = pthread_create(&tid[1], NULL, t_server_2, NULL);
if(iRet2 != 0){
printf("pthread_create 2 error: error_code = %d\n", iRet2);
}
while(1)
{
sleep(1);
}
pthread_exit(NULL);
return 0;
}