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

📄 z.c

📁 linux下的人机对话编程
💻 C
字号:
/* Mserver_B_getdata.c */#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <string.h>#include <errno.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>main(){	struct sockaddr_in sin;	int sock_descriptor;	int temp_sock_descriptor;	int address_size=sizeof(sin);	int port=8020;	char x[30];	char *a="AAAA";	char *b="BBBB";	int i,len1,len2;	FILE *fp; /*create a connection*/while(1){        sock_descriptor=socket(AF_INET,SOCK_STREAM,0);	memset((char *) &sin, 0, sizeof(sin));	if(sock_descriptor==-1)	{        	perror("call to socket");         	continue;	}	bzero(&sin,sizeof(sin));	sin.sin_family=AF_INET;	sin.sin_addr.s_addr=htonl(INADDR_ANY);	sin.sin_port=htons(port);	if(bind(sock_descriptor,(struct sockaddr *)&sin,sizeof(sin))==-1)	{        	close(sock_descriptor);                                perror("callaaaaaaaaaaa to bind");       		continue;      	}      	if(listen(sock_descriptor,20)==-1)	{        	perror("call to listen");	        continue;      	}        temp_sock_descriptor=accept(sock_descriptor,(struct sockaddr *)&sin,&address_size);	if(temp_sock_descriptor==-1)	{        	 perror("call to accept");	         continue;       	}	close(sock_descriptor);while(1){	 char buf[16348]={};      if(recv(temp_sock_descriptor,buf,16384,0)==-1)	{        	perror("call to recv");	        break;       	}      printf("\nReceived form client: %s\n",buf);      // for this server example,we just convert the      // characters to upper case:       len1=strlen(buf);       for(i=0;i<len1;i++)buf[i]=toupper(buf[i]);         if(strncmp(buf,a,4)==0)          { 		strcpy(x,"\nThis is the  aaaa head.\n");           	fp=fopen("/mnt/tmp/A.out","a+");          	if(fp==NULL)printf("error opening!\n");          	else fputs(buf,fp);               	fputs(x,fp);          	fclose(fp);          }        else if(strncmp(buf,b,4)==0)       	  {        	strcpy(x,"\nThis is the  bbbb head.\n");	        fp=fopen("/mnt/tmp/B.out","a+");        	if(fp==NULL)printf("error opening!\n");          	else fputs(buf,fp);               	fputs(x,fp);         	fclose(fp);         }        else 		strcpy(x,"\nERROR!\n");            	len2=strlen(x);            	len1+=len2;        	strcat(buf,x);   	if(send(temp_sock_descriptor,buf,len1,0)==-1)	{        	perror("call to send");        	break;	}       //close(temp_sock_descriptor);    }//whilesleep(1);	}//while(1)}

⌨️ 快捷键说明

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