首頁 >後端開發 >php教程 > C与php socket通讯

C与php socket通讯

WBOY
WBOY原創
2016-06-13 13:14:411046瀏覽

C与php socket通信

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define PORT 80

#define SERVER_IP "192.168.1.21"

?

int main(int argc,char *argv[])

{

int ii,i,serverSocket;

struct sockaddr_in clientAddr;

char buffer[4096];

serverSocket =socket(AF_INET,SOCK_STREAM,0);

if(serverSocket==-1)

? {

?printf("Invalid socket\n");

?exit(1);

? }

bzero(&clientAddr,sizeof(clientAddr));?

clientAddr.sin_family=AF_INET;

clientAddr.sin_port=htons(PORT);

clientAddr.sin_addr.s_addr=inet_addr(SERVER_IP);

if(connect(serverSocket,&clientAddr,sizeof(clientAddr))

{

perror("connect");

exit(1);

}

strcpy(buffer,"GET / HTTP/1.1\nHost:192.168.1.21\n\n");

printf("\n%s\n",buffer);

if(send(serverSocket,buffer,sizeof(buffer),0)

{

perror("send error");

exit(1);

}

ii=(int)recv(serverSocket,buffer,sizeof(buffer),0);

printf("\nrev=%d length\n",ii);

for(i=0;i

{

printf("%c",buffer[i]);

}

return 1;

}


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn