Home > AI > Uncategorized

Socket – Php服务端,C客户端

Client.h

#ifndef Client_h
#define Client_h

#include <stdio.h>
#include <string.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>




void clientTest(void);

#endif

 


Client.c

#include "Client.h"


#define PORT 9519
#define IP "192.168.1.2"
#define BUFSIZE 2048


void clientTest() {
    printf("start create client \n");
    
    //1 create
    int client = socket(AF_INET, SOCK_STREAM, 0);
    
    if (client == -1) {
        printf("client create failed \n");
        return ;
    }
    
    printf("client create successfully \n");
        
    //2
    struct sockaddr_in addr_4;
    memset(&addr_4, 0, sizeof(addr_4));
    addr_4.sin_family = AF_INET;
    addr_4.sin_len = sizeof(addr_4);
    addr_4.sin_port = htons(PORT);
    addr_4.sin_addr.s_addr = inet_addr(IP);
    bzero(&(addr_4.sin_zero), 8);
    
    //3 connect
    int result = connect(client, (struct sockaddr*)&addr_4, sizeof(struct sockaddr));
    if (result == -1){
        printf("client connect failed \n");
    }
    
    printf("client connect successfully \n");
    
    //4 send
    char buf[BUFSIZE];
    strcpy(buf, "1st msg from client");
    send(client, buf, sizeof(buf), 0);
    
    //read
    int numBytes;
    numBytes = recv(client, buf, sizeof(buf), 0);
    printf("%d", numBytes);
    if (numBytes > 0){
        printf("receive successfully \n");
    } else {
        printf("receive failed \n");
    }
    buf[numBytes] = '\0';
    printf("%s", buf);
    

    close(client);
    return ;
}

socketServer.php

<?php
  //确保在连接客户端时不会超时
  set_time_limit(0);
  
  //$ip = '127.0.0.1';
  //$port = 1935;
  $ip = '192.168.1.2';
  $port = 9519;
  
  /*
   +-------------------------------
  *    @socket通信整个过程
  +-------------------------------
  *    @socket_create
  *    @socket_bind
  *    @socket_listen
  
  *    @socket_accept
  *    @socket_read
  *    @socket_write
  *    @socket_close
  *	   @socket_set_option
  *    @socket_strerror()
  +--------------------------------
  */
 
 /*----------------    以下操作都是手册上的    -------------------*/
 
echo "server begin start.... \n";

//1
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
	echo "socket_create() fail:".socket_strerror($sock)."\n";
} else {
 	echo "socket_create() succeed \n";
}


if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
    echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
}


$linger = array('l_linger' => 1, 'l_onoff' => 1);
socket_set_option($sock, SOL_SOCKET, SO_LINGER, $linger);


//2
if (($ret = socket_bind($sock, $ip, $port)) < 0){
	echo "socket_bind() fail:".socket_strerror($ret)."\n";
} else {
	echo "socket bind at ".$ip." : ".$port." succeed \n";
}
 
//3
if (($ret = socket_listen($sock, 4)) < 0) {
	echo "socket_listen() 失败的原因是:".socket_strerror($ret)."\n";
} else {
	echo "socket_listen() socceed \n";
}

 

$count = 0;
 
do {

	if (($client = socket_accept($sock)) < 0) {
    	echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
        break;
    } else {
    	echo "接受了一个来自客户的连接,socket_accept() success \n";
    	
    	//read msg from client
        $buf = socket_read($client, 8192);
        echo "[msg from client]: $buf \n";
        
    	//send msg to client
        $msg ="我是服务器,给你发个短信,封闭你的手机号码 \n";
        socket_write($client, $msg, strlen($msg));
        	
        
        if(++$count >= 5){ //限制发送数为5
        	echo "see you";
        	//break;
        };
    }

    //echo $buf;
    socket_close($client);
 
} while (true);
 

 
socket_close($sock);
echo "socket_close() succeed \n";


?>

 


说明:recv和send(c),read和write(php)是阻塞式的,所以服务端等待客户端发数据,客户端等待服务端,程序就卡住了,进入fin_wait_2状态。

Related posts:

Leave a Reply