国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C > 正文

C語(yǔ)言編寫基于TCP和UDP協(xié)議的Socket通信程序示例

2020-01-26 14:40:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Tcp多線程服務(wù)器和客戶端程序
服務(wù)器程序:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#define PORT 8082#define BUFSIZE 512char buf[BUFSIZE+1];void* fun(void* x){    //printf("enter thread!/r/n");    int new_fd=*((int*)x);  while(1)    {    int z=read(new_fd,buf,BUFSIZE);//第 6 步 讀取套接字  if(z==0){printf("client close !");break;};  buf[z]='/0';  printf("%s/r/n",buf);//打印  };}int newfd[512];int inewfd=0;int main(){  //第 1 步 創(chuàng)建套接字  int sockfd=socket(AF_INET,SOCK_STREAM,0);  //第 2 步 設(shè)置地址結(jié)構(gòu)體  struct sockaddr_in svraddr;  svraddr.sin_family=AF_INET;//使用 internet 協(xié)議  svraddr.sin_port=htons(PORT);  inet_aton("0.0.0.0",&svraddr.sin_addr);  //第 3 步 綁定  int ret=bind(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));    if(ret<0){printf("error bind!/r/n");exit(-1);};  //第 4 步 監(jiān)聽(tīng)  listen(sockfd,128);  while(1)  {  newfd[inewfd++]=accept(sockfd,NULL,NULL); //第 5 步 接收  pthread_t ntid;    pthread_create(&ntid,NULL,fun,(void*)&(newfd[inewfd-1]));   }}

注意:

gcc server.c -o server -lpthread

客戶端程序 cli.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#define PORT 8082#define BUFSIZE 512char buf[BUFSIZE+1];int main(){  //第 1 步 創(chuàng)建一個(gè)體套接字  int sockfd=socket(AF_INET,SOCK_STREAM,0);  //第 2 步 設(shè)置 addr 結(jié)構(gòu)體  struct sockaddr_in svraddr;  svraddr.sin_family=AF_INET;//使用 internet 協(xié)議  svraddr.sin_port=htons(PORT);  inet_aton("127.0.0.1",&svraddr.sin_addr);  //第 3 步 連接服務(wù)器  connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));  while(1)  {  scanf("%s",buf);  write(sockfd,buf,strlen(buf)); //第 4 步 向套接字中寫入字符串  }}


Udp的服務(wù)器程序和客戶端程序

服務(wù)器程序:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#define PORT 8082#define BUFSIZE 512char buf[BUFSIZE+1];int main(){  //第 1 步 創(chuàng)建套接字  int sockfd=socket(AF_INET,SOCK_DGRAM,0);  //第 2 步 設(shè)置地址結(jié)構(gòu)體  struct sockaddr_in svraddr;  svraddr.sin_family=AF_INET;//使用 internet 協(xié)議  svraddr.sin_port=htons(PORT);  inet_aton("0.0.0.0",&svraddr.sin_addr);  //第 3 步 綁定  int ret=bind(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));  if(ret<0){printf("cannot bind!/r/n");exit(-1);};  while(1)  {        struct sockaddr_in cli;        int len=sizeof(cli);    int z=recvfrom(sockfd,buf,BUFSIZE,0,(struct sockaddr*)&cli,&len);//第 6 步 讀取套接字      buf[z]='/0';    printf("%s/r/n",buf);//打印  }}

客戶端程序 cli.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#define PORT 8082#define BUFSIZE 512char buf[BUFSIZE+1];int main(){  //第 1 步 創(chuàng)建一個(gè)體套接字  int sockfd=socket(AF_INET,SOCK_DGRAM,0);  //第 2 步 設(shè)置 addr 結(jié)構(gòu)體  struct sockaddr_in svraddr;  svraddr.sin_family=AF_INET;//使用 internet 協(xié)議  svraddr.sin_port=htons(PORT);  inet_aton("127.0.0.1",&svraddr.sin_addr);  //第 3 步 連接服務(wù)器  //connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));  while(1)  {  scanf("%s",buf);  sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)&svraddr,sizeof(svraddr)); //第 4 步 向套接字中寫入字符串  }}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 根河市| 揭西县| 曲水县| 平定县| 文安县| 梅河口市| 舒兰市| 静乐县| 土默特右旗| 安远县| 宽甸| 惠东县| 信宜市| 南华县| 余庆县| 乌拉特前旗| 邳州市| 杭锦后旗| 灵宝市| 黄大仙区| 昌宁县| 浮山县| 台湾省| 当涂县| 文水县| 南华县| 法库县| 博野县| 合水县| 上杭县| 新建县| 乌兰察布市| 山西省| 万荣县| 保定市| 石渠县| 徐闻县| 凯里市| 皋兰县| 丹东市| 宁夏|