利用PHP和C语言通过Socket通信方法

jackxiang 2009-11-27 00:35 | |

/*server.c*/

#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

main(){
     int sock;
     struct sockaddr_in server,client;
     int recvd,snd;
     int structlength;
     char * server_ip = "128.168.10.1";/*server ip address*/
     int port = 8888;
     char recvbuf[2000],sendbuf[2000];  

     char str1[]="I have received:\n";

     memset((char *)&server,0,sizeof(server));
     server.sin_family = AF_INET;
     server.sin_addr.s_addr = inet_addr(server_ip);
     server.sin_port = htons(port);

     memset((char *)&client,0,sizeof(client));
     client.sin_family = AF_INET;
     client.sin_addr.s_addr = htonl(INADDR_ANY);
     client.sin_port = htons(port);
  
     if((sock = socket (AF_INET,SOCK_DGRAM,0)) < 0 ){
         printf("socket create error!\n");
         exit(1);
     }  
     structlength = sizeof(server);
     if( bind(sock,(struct sockaddr *) &server,structlength) < 0){
         printf("socket bind error!\n");
         perror("bind");
         exit(1);
     }

     while(1){
         structlength = sizeof(client);  
            
         printf("waiting.......\n");
         recvd = recvfrom(sock,recvbuf,sizeof(recvbuf),0,
             (struct sockaddr *) & client,&structlength);
         if(recvd < 0){
             perror("recvfrom");
             exit(EXIT_FAILURE);  
         }
         else{
             printf("received:%s\n",recvbuf);
        
             memset(sendbuf,0,strlen(sendbuf));
             memcpy(sendbuf,str1,strlen(str1));
                    
             snd = sendto(sock,sendbuf,strlen(str1),0,
             (struct sockaddr *) &client,structlength);

             if(snd < 0){
             perror("sendto");
             exit(1);
             }
             printf("sendok!\n");
         }          
     }        
     close(sock);
}

/*
gcc -o server server.c生成server程序,在服务器端运行./server
*/
/*client.php*/

<?php
error_reporting(E_ALL);
set_time_limit(0);
echo "<h2>TCP/IP Connection</h2>\n";

$service_port = 9501;
$address = "127.0.0.1";

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
  echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
} else {
  echo "OK.\n";
}

echo "试图连接 '$address' 端口 '$service_port'...<br>";
$result = socket_connect($socket, $address, $service_port);
if ($result < 0) {
  echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
} else {
  echo "连接OK<br>";
}

$in = "Hello\r\n";
$in .= " I am msingle.\r\n";
$out = '';
$out1 = '';

if (!socket_write($socket, $in, strlen($in))) {
  echo "socket_write() failed: reason: " . socket_strerror($socket) . "\n";
} else {
  echo "发送到服务器信息成功!<br>";
  echo "发送的内容为:<font color='red'>$in</font> <br&gt;";
} while ($out = socket_read($socket, $service_port)) {
  echo "接收服务器回传信息成功!<br>";
  echo "接受的内容为:", $out;
}

echo "关闭SOCKET...<br>";
socket_close($socket);
echo "关闭OK<br>";

?>




/*这个过程很简单,就是客户端提交一个信息,服务端接收,
并返回给客户端一个"接收到"的确认信息。
*/

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/2279/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2012-11-2 10:41
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]