[C Language] -- C language and PHP implement local communication through af_unix

Source: Internet
Author: User
Tags php online

Recently, due to work needs, I learned PHP-related programming. The socket communication of PHP is needed. Originally, PHP socket communication is simpler than C language, but the server uses C code, the client uses PHP, and local communication, that is, communication is performed through local temporary files in the form of af_unix. It is relatively easy to use C language, and there are a lot of online materials. For details, refer. By referring to the PHP online reference manual, after modifying relevant parameters, You can implement C-server and PHP-Client Communication Based on af_unix. The Code is as follows:

Server


Reference: http://blog.csdn.net/ast_224/article/details/3962221

# Include <stdio. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <sys/UN. h>
# Define unix_domain "/tmp/Unix. Domain"
Int main (void)
{
Socklen_t clt_addr_len;
Int listen_fd;
Int com_fd;
Int ret;
Int I;
Static char recv_buf [1024];
Int Len;
Struct sockaddr_un clt_addr;
Struct sockaddr_un srv_addr;
Listen_fd = socket (pf_unix, sock_stream, 0 );
If (listen_fd <0)
{
Perror ("cannot create communication socket ");
Return 1;
}
Else
{
Printf ("socket start: \ n ");
}

// Set server addr_param
Srv_addr.sun_family = af_unix;
Strncpy (srv_addr.sun_path, unix_domain, sizeof (srv_addr.sun_path)-1 );
Unlink (unix_domain );
// Bind sockfd & ADDR
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 );
Return 1;
}
Else
{
Printf ("bind OK \ n ");
}
// Listen sockfd
Ret = listen (listen_fd, 1 );
If (ret =-1)
{
Perror ("cannot listen the client CONNECT request ");
Close (listen_fd );
Unlink (unix_domain );
Return 1;
}
Else
{
Printf ("start to listen \ n ");
}
// Have CONNECT request use accept
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 );
Return 1;
}
// Read and printf sent Client info
Printf ("/n ==== info ====/N ");
For (I = 0; I <4; I ++)
{
Memset (recv_buf, 0,1024 );
Int num = read (com_fd, recv_buf, sizeof (recv_buf ));
Printf ("Message from client (% d): % s/n", num, recv_buf );
}
Close (com_fd );
Close (listen_fd );
Unlink (unix_domain );
Return 0;
}

Client:

<? PHP
Define ("unix_domain", "/tmp/Unix. Domain ");
Echo unix_domain;

$ Socket = socket_create (af_unix, sock_stream, 0); // The third parameter is 0.
If ($ socket <0)
{
Echo "socket_create () failed: reason:". socket_strerror ($ socket). "\ n ";
}
Else

{
Echo "create OK. \ n ";
}

$ Result = socket_connect ($ socket, unix_domain); // you only need two parameters.
If ($ result <0)
{
Echo "socket_connect () failed. \ nreason: ($ result)". socket_strerror ($ result). "\ n ";
}
Else

{
Echo "Connect OK ";
}

// The following code communicates with a common PHP socket
$ In = "Hello, ni hao \ n ";
If (! Socket_write ($ socket, $ in, strlen ($ in )))
{
Echo "socket_write () failed: reason:". socket_strerror ($ socket). "\ n ";
}
Else

{
Echo "Send message OK! ";
}
 
 
Echo "Close socket ...";
Socket_close ($ socket );
Echo "close OK ";
 
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.