多程序socket程式設計示例

2021-06-21 13:24:16 字數 1619 閱讀 4498

socket程式設計的基礎函式使用請自查詢,本文只給出在linux gcc可編譯執行的socket通訊的伺服器程式與客戶端程式。

**如下:server端

#include #include #include #include #include //signal

#include //socket

#include //sockaddr_in

#include #include //memset

const int buff_size=10;

void process(int client_fd,int cmd)

send(client_fd,"ok",buff_size,0);

}int main(void)

if (bind(server_fd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0)

listen(server_fd,5);

puts("now server begin listening ,the listen queue len is5");

signal(sigcld,sig_ign); //ignore teh detail of the child process exit

int client_len = sizeof(remote_addr);

while(1)

if( (pid = fork())== 0) //child

if(pid) //parent

}close(server_fd);

return exit_success;

}#include #include #include #include #include //signal

#include //socket

#include //sockaddr_in

#include #include //memset

const int buff_size=10;

int main(void)

if(connect(client_sockfd,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0)

printf("connected to server\n");

len=recv(client_sockfd,buf,bufsiz,0);//接收伺服器端資訊

buf[len]='/0';

printf("received %s\n",buf); //列印伺服器端資訊

puts("please input cmd");

scanf("%d",&cmd);

len=send(client_sockfd,&cmd,4,0);

len=recv(client_sockfd,buf,buff_size,0);

buf[len]='/0';

printf("received %s\n",buf); //列印伺服器端資訊

close(client_sockfd);//關閉套接字

return 0;

}



PHP多程序協作程式設計之 socket

基本思路 通過socket請求其他php腳步,實現php的多程序的非同步執行。廢話就不多說了,請看下面的腳步並執行之。a.php function runthread function a function b if isset get act get act a if get act a else...

PHP多程序協作程式設計之 socket

基本思路 通過socket請求其他php腳步,實現php的多程序的非同步執行。廢話就不多說了,請看下面的腳步並執行之。function runthread function a function b if isset get act get act a if get act a else if ge...

socket 多程序通訊

之前寫的那個socket太次了。只能連乙個,所以優化了一下。我管他叫superserver。獻上 include include include include include include include intmain int argc,const char ar int fd socket ...