后台c 语言为服务器端,前台php与客户端。
1.后台c语言代码:
(1)socket_connect.h
#ifndef SOCKET_CONNECT_H #define SOCKET_CONNECT_H #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> /* *函数介绍:套接字初始化 *输入参数:socket 套接字文件地址,port 监听端口 *输出参数:socket 套接字文件 *返回值: */ void socket_connect_init(int *sockfd,unsigned short port); /* *函数介绍:接收信息 *输入参数:client 客户端信息储存地址,msg 接受到的信息,len 表示msg存储空间 *输出参数:client 客户端信息 *返回值: */ void socket_connect_recv(int sock,struct sockaddr_in *client,char *msg,int len); /* *函数介绍:发送信息 *输入参数:client 客户端信息储存地址,msg 发送的信息,len 表示信息长度 *输出参数: *返回值: */ void socket_connect_send(int sock,struct sockaddr_in *client,char *msg,int len); /* *函数介绍:关闭套接字 *输入参数:sock套接字文件描述符 *输出参数: *返回值: */ void socket_connect_close(int sockfd); #endif
(2)soceket_connect.c
#include "socket_connect.h" void socket_connect_init(int *sockfd,unsigned short port) { int sock; struct sockaddr_in server; int structlength; memset((char *)&server,0,sizeof(server)); server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.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); } *sockfd = sock; } void socket_connect_recv(int sock,struct sockaddr_in *client,char *msg,int len) { memset(client,0,sizeof(struct sockaddr_in)); int structlength = sizeof(struct sockaddr_in); int recvd = recvfrom(sock,msg,len,0,(struct sockaddr *) client,&structlength); if(recvd < 0){ perror("recvfrom"); exit(EXIT_FAILURE); } else{ msg[recvd] = '\0'; } } void socket_connect_send(int sock,struct sockaddr_in *client,char *msg,int len){ int structlength = sizeof(struct sockaddr_in); int snd = sendto(sock,msg,len,0,(struct sockaddr *) client,structlength); if(snd < 0){ perror("sendto"); exit(1); } } void socket_connect_close(int sockfd){ close(sockfd); }
(3)使用socket_connect.h 和 socket_connnet.c 建立服务器
#include <stdio.h> #include "socket_connect.h" main(){ //sokcet文件描述符 int sock; //客户端信息 struct sockaddr_in client; char msg[200]; //初始化 socket_connect_init(&sock,8000); while(1) { //接受信息 socket_connect_recv(sock,&client,msg,200); printf("recv:%s\n",msg); msg[0]='s'; //发送信息 socket_connect_send(sock,&client,msg,1); } //关闭服务器 socket_connect_close(sock); }
2.前台php代码
(1)文件 Message.php
<?php class Message{ private $server_ip; private $server_port; private $sock ; function __construct($server_ip="192.168.101.201",$server_port=8000) { $this -> server_ip = $server_ip; $this -> server_port = $server_port; $this -> sock=@socket_create(AF_INET,SOCK_DGRAM,0); if(!$this -> sock) { echo "socket create failure"; } else echo "start\n"; } function sendMessage($buf) { if(!@socket_sendto($this -> sock,$buf,strlen($buf),0,$this -> server_ip, $this -> server_port)) { echo "send error\n"; socket_close($this -> sock); exit(); } $msg = ""; if(!@socket_recvfrom($this -> sock,$msg,256,0,&$this -> server_ip,&$this -> server_port)) { echo "recvieve error!"; socket_close($this ->sock); exit(); } return $msg; } function closeMessage() { socket_close($this -> sock); } } ?>
(2)使用Message类 建立客户端
<?php //Message.php 的存放路径 require_once("Message.php"); if(isset($_GET['hidden'])){ $message = $_GET['radio']; $m = new Message("192.168.101.201",8000); $msg = $m -> sendMessage($message); echo $msg; $m -> closeMessage(); } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>socket client </title> </head> <body> <form id="form1" name="form1" method="get" action="test.php"> <input name="radio" type="radio" id="radio" value="start" checked="checked" /> 开始: <input name="radio" type="radio" id="radio2" value="stop" /> 结束: <input type="radio" name="radio" id="radio3" value="restart" /> 重启: <label> <input type="submit" name="button" id="button" value="提交" /> </label> <input name="hidden" type="hidden" id="hidden" value="hidden" /> </form> </body> </html>