Socket Server side: SERVER.C
Socket read-Write default non-blocking
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #include <sys /types.h> #include <errno.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/select.h > #include <unistd.h> #include <termios.h> #include <sys/stat.h>/********** Timer header file ************** * * #include <sys/time.h> #include <signal.h>/*********** Socket communication header file **********/#include <sys/ socket.h> #include <sys/un.h> #define Unix_domain "/tmp/unix2.domain" static char recv_php_buf[256]; Buffer static int recv_php_num=0 receiving the client data;
Receives the total length of the client data const char RECV_PHP_BUF1[20]={0X00,0X01,0X02,0X03,0X04,0X05,0X06};
void Main () {socklen_t clt_addr_len;
int listen_fd;
int com_fd;
int ret=0;
int i;
int Len;
struct Sockaddr_un clt_addr;
struct Sockaddr_un srv_addr;
while (1) {//Create socket for communication, the communication domain is UNIX communication domain Listen_fd=socket (af_unix,sock_stream,0); if (listen_fd<0) {perror ("Cannot create listening socket ");
Continue
else {while (1) {//Set server address parameter Srv_addr.sun_family=af_unix;
strncpy (srv_addr.sun_path,unix_domain,sizeof (Srv_addr.sun_path)-1);
Unlink (Unix_domain);
Binding sockets and server address information Ret=bind (LISTEN_FD, (struct sockaddr*) &srv_addr,sizeof (SRV_ADDR));
if (ret==-1) {perror ("Cannot bind server socket");
Close (LISTEN_FD);
Unlink (Unix_domain);
Break
//the socket is monitored to determine whether there is a connection request Ret=listen (listen_fd,1);
if (ret==-1) {perror ("Cannot listen the client connect request");
Close (LISTEN_FD);
Unlink (Unix_domain);
Break chmod (unix_domain,00777);//Set Communication file permissions while (1) {///when there is a connection request, call the Accept function to establish a connection between the server and the client len=sizeof (CLT
_ADDR);
Com_fd=accept (LISTEN_FD, (struct sockaddr*) &clt_addr,&len);
if (com_fd<0) {perror ("Cannot accept client connect request");
Close (LISTEN_FD); Unlink (UNIX_domain);
Break
//Read and output the connection information sent by the client memset (recv_php_buf,0,256);
Recv_php_num=read (com_fd,recv_php_buf,sizeof (RECV_PHP_BUF));
printf ("\n=====recv=====\n");
for (i=0;i<recv_php_num;i++) printf ("%d", recv_php_buf[i]);
printf ("\ n"); /*if (recv_php_buf[0]==0x02) {if (recv_php_buf[recv_php_num-1]==0x00) {recv_php_buf[recv_php_num-1
]=0x01;
else {recv_php_buf[recv_php_num-1]=0x00;
}} * * *//recv_php_buf[20]+=1;
Write (Com_fd,recv_php_buf,recv_php_num);
printf ("\n=====send=====\n");
for (i=0;i<recv_php_num;i++) printf ("%d", recv_php_buf[i]);
printf ("\ n");
Write (com_fd,recv_php_buf,20);
Close (COM_FD)//Note To turn off the connection symbol, or it will exceed the number of connections and the error}}}
socket client: client.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #include <sys /types.h> #include <errno.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/select.h > #include <unistd.h> #include <termios.h> #include <sys/stat.h>/********** Timer header file ************** * * #include <sys/time.h> #include <signal.h>/*********** Socket communication header file **********/#include <sys/ socket.h> #include <sys/un.h> #include <sys/ioctl.h> #pragma pack (1)//set to 1-byte alignment #define UNIX_DOMAIN2 "
/tmp/unix2.domain "Static Char recv_php_buf[256]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};
struct Test {char A;
int b;
int C;
}se;
void Main (void) {int connect_fd;
int ret=0;
int i;
static struct Sockaddr_un srv_addr;
printf ("IPC communication thread \ n");
while (1)//{//create socket for communication, the communication domain is UNIX communication domain Connect_fd=socket (af_unix,sock_stream,0);
printf ("%d\n", CONNECT_FD); if (connect_fd<0) {PerroR ("Cannot create communication socket");
printf ("%d\n", CONNECT_FD);
Continue
else {//while (1)//{Srv_addr.sun_family=af_unix;
strcpy (srv_addr.sun_path,unix_domain2);
Connect Server Ret=connect (connect_fd, (struct sockaddr*) &srv_addr,sizeof (SRV_ADDR));
if (ret==-1) {close (CONNECT_FD);
printf ("Connect fail\n"); Break
Recreate the socket} else {//Otherwise, connect the server successfully//while (1)//{se.a=0x01;
se.b=0x01020304;
se.c=0x05060708;
Write (connect_fd,recv_php_buf,20);//Transfer data to an external application, sending the actual length//write (connect_fd,&se,sizeof (struct test)); memset (recv_php_buf,0,sizeof (RECV_PHP_BUF));
Empty Socket_buf//sleep (1);
Fcntl (Connect_fd,f_setel,o_nonblock);
Read (connect_fd,recv_php_buf,sizeof (RECV_PHP_BUF));
printf ("Receive over\n");
for (i=0;i<20;i++) printf ("%x", Recv_php_buf[i]);
printf ("%x", se.a);
printf ("%x", se.b); printf ("%x", se.c);
Close (CONNECT_FD);
Break
}//}//close (CONNECT_FD); }
//}
}