⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpserver.c

📁 tcp/Ip的程序
💻 C
字号:
/*server.c*/#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <string.h>#include <stdlib.h>int port=8000;int main(void){  struct sockaddr_in sin;  struct sockaddr_in pin;  int    mysock;  int    tempsock;  int    addrsize;  char   str[100],str1[20],str2[20],str3[20];  char   buf[100];  int    i,len1,len2,len3,len4,len5,len6;  float  c;   /*new  socket*/    mysock= socket(AF_INET,SOCK_STREAM,0);    if(mysock == -1)     {      perror("call to socket");      exit(1);     }    /*new  IPV4*/     bzero(&sin,sizeof(sin));           //zero成员并未使用,它是为了和通用套接字地址struct sockaddr兼容性而设定的。在编程中,一般都通过bzero()或将其置零。     sin.sin_family = AF_INET;//设置服务器套接字的IP地址为特殊值INADDR_ANY,表示服务器愿意接收来自任何网络设备接口的客户机连接。htonl()函数将主机顺序的字节转换成网络顺序的字节     sin.sin_addr.s_addr = htonl(INADDR_ANY);     sin.sin_port = htons(port);          /*bind socket*/     if(bind(mysock,(struct sockaddr *)&sin,sizeof(sin))==-1)      {     perror("call to bind");     exit(1);      }      /*listen client */     if(listen(mysock,20)==-1)      {      perror("call to listen");      exit(1);      }           printf("accepting connections....\n");     while(1)      {      /*receive client connection*/      tempsock = accept(mysock,(struct sockaddr *)&pin ,&addrsize);      if(tempsock ==-1){        perror("call to accept");         exit(1);        }             /*receive client message*/      len1=recv(tempsock,str,100,0);        printf("%d\n",len1);        str[len1]=0;          printf("received from client %s\n",str);       if(len1>0){        strcpy(str1,strtok(str," "));           //strtok 分割字符串         printf("the first string is %s\n",str1);        strcpy(str2,strtok(NULL," "));         printf("the second string is %s\n",str2);        strcpy(str3,strtok(NULL," "));         printf("the third string is %s\n",str3);         c=atof(str3)*1.05;               //atof将字符串变为实数          sprintf(buf,"the number is %s\n  the food is %s\n  the cost is %s\n ",str1,str2,str3);        printf("%f\n",c);       printf("%s",buf);         }        len2=strlen(buf);       printf("%d\n",len2);       if(send(tempsock,buf,len2,0)==-1);{          perror("not to send");         exit(1);            }        /*len5=send(tempsock,buf,len2,0);        len3=recv(tempsock,str,100,0);        printf("%d\n",len3);        str[len3]=0;          printf("received from client %s\n",str);      if(len3>0){        strcpy(str1,strtok(str," "));           //strtok 分割字符串         printf("the first string is %s\n",str1);        strcpy(str2,strtok(NULL," "));         printf("the second string is %s\n",str2);        strcpy(str3,strtok(NULL," "));         printf("the third string is %s\n",str3);         //c=atof(str3)*1.05;               //atof将字符串变为实数          sprintf(buf,"the number is %s\n  the food is %s\n  the cost is %s\n ",123,456,789);         }       len4=strlen(buf);      if(send(tempsock,buf,len4,0)==-1);{          perror("call to send");         exit(1);            }        len6=send(tempsock,buf,len4,0);*/         close(tempsock);      }        return 0;} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -