基于linux或windows的c/s的循环服务器求一元二次方程的根

2021-01-29 13:12

阅读:595

标签:star   report   输入   opened   spl   strlen   tmp   pes   bind   

在linux和windows上实现

c/s模式

socket循环服务器求解一元二次方程的根

ax^2+bx+c=0

根据上式,客户端发送a,b,c给服务器,返回求解的根

暂未考虑非法数据等问题

linux:

tcpclient.cpp

技术图片技术图片
  1 #include  2 #include   3 #include  4 #include  5 #include  6 #include
  7 #include  8 #include  9 
 10 using namespace std;
 11 
 12 #define BUFSIZE 512
 13 
 14 // #define SERVERIP "192.168.41.32" 
 15 // #define SERVERPORT 4140
 16 
 17 /*error report*/
 18 static void bail(const char *on_what){
 19     fputs(strerror(errno), stderr);
 20     fputs(": ", stderr);
 21     fputs(on_what, stderr);
 22     fputc(\n, stderr);  
 23     exit(1);
 24 }
 25 
 26 void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
 27 {
 28     for(int i=0;i)
 29     {
 30         istringstream iss(argv[i]);
 31         string str;
 32         iss>>str;
 33         if(str=="ip")
 34         {
 35             *SERVERIP=argv[i+1];
 36         }
 37         else if(str=="port")
 38         {
 39             istringstream sts(argv[i+1]);
 40             string s_port;
 41             sts>>s_port;
 42             *SERVERPORT=stoi(s_port);
 43         }
 44         else
 45         {
 46             
 47         }
 48         
 49     }
 50 }
 51 
 52 int main(int argc,char* argv[])
 53 {
 54     const char* SERVERIP="192.168.41.32";
 55     int SERVERPORT=4140;
 56 
 57     getarg(argc,argv,&SERVERIP,&SERVERPORT);
 58 
 59     int sockfd;
 60     struct sockaddr_in server_addr;
 61     const char* sendbuf = (char*)"hello,this is client";
 62     char recvbuf[BUFSIZE];
 63 
 64     //create socket
 65     if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) 0)//创建套接字描述符
 66     {
 67         fprintf(stderr,"Socket error %s\n",strerror(errno));
 68         exit(-1);
 69     }
 70 
 71     memset(&server_addr, 0,sizeof(server_addr));
 72     server_addr.sin_family = AF_INET;
 73     server_addr.sin_port = htons(SERVERPORT);
 74     server_addr.sin_addr.s_addr = inet_addr(SERVERIP);
 75 
 76     if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr))) 0)//连接远程对等实体
 77     {
 78         fprintf(stderr,"connect error %s\n",strerror(errno));
 79         exit(-1);
 80     }
 81 
 82     if ((send(sockfd, sendbuf, strlen(sendbuf), 0)) != strlen(sendbuf))//发送数据
 83     {
 84         fprintf(stderr,"send error %s\n",strerror(errno));
 85         exit(-1);
 86     }
 87 
 88     memset(recvbuf,\0, sizeof(recvbuf));
 89     while (true)
 90     {
 91         int num = recv(sockfd, recvbuf, sizeof(recvbuf), 0);//获取数据
 92         if (num 0)
 93         {
 94             fprintf(stderr,"recv error %s\n",strerror(errno));
 95             exit(-1);
 96         }
 97         else
 98         {
 99             cout  endl;
100             break;
101         }
102     }
103 
104     // double a=1.1,b=2.2,c=3.3;
105     double a,b,c;
106     cout"please input a,b,c (ax^2+bx+c=0) :"endl;
107     cin>>a>>b>>c;
108     string str=to_string(a)+ +to_string(b)+ +to_string(c);
109     sendbuf=str.data();
110     if ((send(sockfd, sendbuf, strlen(sendbuf), 0)) != strlen(sendbuf))//发送数据
111     {
112         fprintf(stderr,"send error %s\n",strerror(errno));
113         exit(-1);
114     }
115 
116     while (true)
117     {
118         int num = recv(sockfd, recvbuf, sizeof(recvbuf), 0);//获取数据
119         if (num 0)
120         {
121             fprintf(stderr,"recv error %s\n",strerror(errno));
122             exit(-1);
123         }
124         else
125         {
126             cout  endl;
127             break;
128         }
129     }
130 
131     cout"exit..."endl;
132 
133     close(sockfd);//终止通信并释放套接字描述符
134 
135     return 0;
136 }
View Code

tcpserv.cpp

技术图片技术图片
  1 #include  2 #includestring.h>
  3 #include   4 #include  5 #include  6 #include  7 #include
  8 #include  9 #include 10 #include 11 
 12 #define BUFSIZE 512
 13 #define PORT 4140
 14 #define MAXLISTEN 128
 15 
 16 /*error report*/
 17 static void bail(const char *on_what){
 18     fputs(strerror(errno), stderr);
 19     fputs(": ", stderr);
 20     fputs(on_what, stderr);
 21     fputc(\n, stderr);  
 22     exit(1);
 23 }
 24 
 25 int main()
 26 {
 27     std::string cal(double,double,double);
 28     double a=0.0,b=0.0,c=0.0;
 29 
 30 
 31     int sockfd;//server fd
 32     // int port;
 33     int newfd;//connect fd
 34     struct sockaddr_in server_addr;
 35     struct sockaddr_in client_addr;
 36 
 37     char reqbuf[BUFSIZE];
 38 
 39     /*create server socket*/
 40     if((sockfd=socket(AF_INET,SOCK_STREAM,0))0)//创建套接字描述符
 41     {
 42         fprintf(stderr,"Socket error %s\n",strerror(errno));
 43         exit(-1);
 44     }
 45 
 46     /*ready for server addr*/
 47     memset(&server_addr,0,sizeof(server_addr));
 48     server_addr.sin_family=AF_INET;
 49     server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
 50     server_addr.sin_port=htons(PORT);
 51 
 52     /*bind socket addr*/
 53     int tmp=bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr));//将ip和port绑定到套接字上
 54     if(tmp0)
 55     {
 56         fprintf(stderr,"Bind error %s\n",strerror(errno));
 57         exit(-1);
 58     }
 59 
 60     /*listen*/
 61     if((listen(sockfd,MAXLISTEN))0)//将套接字设置为被动模式
 62     {
 63         fprintf(stderr,"Listen error %s\n",strerror(errno));
 64         exit(-1);
 65     }
 66     std::cout"waiting for client ..."std::endl;
 67 
 68     /*server main while*/
 69     while(true)
 70     {
 71         socklen_t size;
 72         size=sizeof(struct sockaddr_in);
 73 
 74         memset(&client_addr,0,sizeof(client_addr));
 75         memset(&reqbuf,\0,sizeof(reqbuf));
 76 
 77         /*accept client & create new fd*/
 78         if((newfd=accept(sockfd,(struct sockaddr*)&client_addr,&size))0)//接受传入连接
 79         {
 80             fprintf(stderr,"Accept error %s\n",strerror(errno));
 81             exit(-1);
 82         }
 83         std::cout"Server got connect from "std::endl;
 84 
 85         /*recv data from client*/
 86         if((recv(newfd,reqbuf,sizeof(reqbuf),0))0)//获取数据
 87         {
 88             fprintf(stderr,"Recv error %s\n",strerror(errno));
 89             exit(-1);
 90         }
 91         std::coutstd::endl;
 92         
 93         /*send data to client*/
 94         char * cip=inet_ntoa(client_addr.sin_addr);
 95         char tmp[strlen(cip)];
 96         strcpy(tmp,cip);
 97         strcat(tmp," connected");
 98         const char *sendbuf=tmp;
 99         // std::cout
100         if((send(newfd,sendbuf,strlen(sendbuf),0))!=strlen(sendbuf))//发送数据
101         {
102             fprintf(stderr,"Send error %s\n",strerror(errno));
103             exit(-1);
104         }
105 
106         /*do  with data*/
107         memset(&reqbuf,\0,sizeof(reqbuf));
108         if((recv(newfd,reqbuf,sizeof(reqbuf),0))0)//获取数据
109         {
110             fprintf(stderr,"Recv error %s\n",strerror(errno));
111             exit(-1);
112         }
113         std::coutstd::endl;
114         
115         std::istringstream sts(reqbuf);
116         std::string sx,sy,sz;
117         sts>>sx>>sy>>sz;
118         // std::cout
119         
120         double a=stod(sx);
121         double b=stod(sy);
122         double c=stod(sz);
123         // std::cout
124 
125         std::string res=cal(a,b,c);
126         sendbuf=res.data();
127         // sendbuf="123345";
128 
129         char ch[BUFSIZE];
130         strcpy(ch,sendbuf);
131         std::coutstd::endl;
132 
133         if((send(newfd,sendbuf,strlen(sendbuf),0))!=strlen(sendbuf))//发送数据
134         {
135             fprintf(stderr,"Send error %s\n",strerror(errno));
136             exit(-1);
137         }
138 
139 
140         /*close new fd*/
141         close(newfd);//终止通信并释放套接字描述符
142     }
143 
144     /*close server fd*/
145     close(sockfd);//释放套接字描述符
146 
147     std::cout"exit"std::endl;
148 
149     return 0;
150 }
151 
152 std::string cal(double a,double b,double c) {
153     std::string res="";
154  
155     double  x1, x2, discriminant, realPart, imaginaryPart;
156     // cout 157     // cin >> a >> b >> c;
158     discriminant = b*b - 4*a*c;
159     
160     if (discriminant > 0) {
161         x1 = (-b + sqrt(discriminant)) / (2*a);
162         x2 = (-b - sqrt(discriminant)) / (2*a);
163         // cout 164         // cout 165         // cout 
166         std::string s1=std::to_string(x1);
167         std::string s2=std::to_string(x2);
168         res="Roots are real and different.\nx1="+s1+"\nx2="+s2+"\n";
169     }
170     
171     else if (discriminant == 0) {
172         // cout 173         // x1 = (-b + sqrt(discriminant)) / (2*a);
174         // cout 
175         std::string s1=std::to_string(x1);
176         res="real roots are same:\nx1=x2="+s1+"\n";
177     }
178  
179     else {
180         realPart = -b/(2*a);
181         imaginaryPart =sqrt(-discriminant)/(2*a);
182         // cout 183         // cout 184         // cout 
185         std::string s1r=std::to_string(realPart);
186         std::string s1i=std::to_string(imaginaryPart);
187         std::string s2r=std::to_string(realPart);
188         std::string s2i=std::to_string(imaginaryPart);
189         res="real roots are defferent:\nx1="+s1r+"+"+s1i+"i\nx2="+s2r+"-"+s2i+"i\n";
190     }
191  
192     return res;
193 }
View Code

udpclient.cpp

技术图片技术图片
  1 #include  2 #include   3 #include  4 #include  5 #include  6 #include
  7 #include  8 #include  9 
 10 using namespace std;
 11 
 12 #define BUFSIZE 512
 13 
 14 // #define SERVERIP "192.168.2.169"
 15 // #define SERVERPORT 4140
 16 
 17 /*error report*/
 18 static void bail(const char *on_what){
 19     fputs(strerror(errno), stderr);
 20     fputs(": ", stderr);
 21     fputs(on_what, stderr);
 22     fputc(\n, stderr);  
 23     exit(1);
 24 }
 25 
 26 void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
 27 {
 28     for(int i=0;i)
 29     {
 30         istringstream iss(argv[i]);
 31         string str;
 32         iss>>str;
 33         if(str=="ip")
 34         {
 35             *SERVERIP=argv[i+1];
 36         }
 37         else if(str=="port")
 38         {
 39             istringstream sts(argv[i+1]);
 40             string s_port;
 41             sts>>s_port;
 42             *SERVERPORT=stoi(s_port);
 43         }
 44         else
 45         {
 46             
 47         }
 48         
 49     }
 50 }
 51 
 52 int main(int argc,char* argv[])
 53 {
 54     const char* SERVERIP="192.168.41.32";
 55     int SERVERPORT=4140;
 56 
 57     getarg(argc,argv,&SERVERIP,&SERVERPORT);
 58 
 59     int sockfd;
 60     struct sockaddr_in server_addr;
 61     struct sockaddr_in client_addr;
 62     const char* sendbuf = (char*)"hello,this is client";
 63     char recvbuf[BUFSIZE];
 64 
 65     //create socket
 66     if ((sockfd = socket(AF_INET, SOCK_DGRAM,0)) 0)//创建套接字描述符
 67     {
 68         fprintf(stderr,"Socket error %s\n",strerror(errno));
 69         exit(-1);
 70     }
 71 
 72     memset(&server_addr, 0,sizeof(server_addr));
 73     server_addr.sin_family = AF_INET;
 74     server_addr.sin_port = htons(SERVERPORT);
 75     server_addr.sin_addr.s_addr = inet_addr(SERVERIP);
 76 
 77     // if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr)))  78     // {
 79     //     cout  80     //     return -1;
 81     // }
 82 
 83     if ((sendto(sockfd, sendbuf, strlen(sendbuf), 0,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in))) != strlen(sendbuf))//发送数据
 84     {
 85         fprintf(stderr,"sendto error %s\n",strerror(errno));
 86         exit(-1);
 87     }
 88 
 89     memset(&client_addr,0,sizeof(client_addr));
 90     memset(recvbuf, 0,sizeof(recvbuf));
 91     while (true)
 92     {
 93         socklen_t len=sizeof(struct sockaddr_in);
 94         int num = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0,(struct sockaddr*)&client_addr,&len);//获取数据
 95         if (num 0)
 96         {
 97             fprintf(stderr,"recvfrom error %s\n",strerror(errno));
 98             exit(-1);
 99         }
100         else
101         {
102             cout  endl;
103             break;
104         }
105     }
106 
107     double a,b,c;
108     cout"please input a,b,c (ax^2+bx+c=0) :"endl;
109     cin>>a>>b>>c;
110     string str=to_string(a)+ +to_string(b)+ +to_string(c);
111     sendbuf=str.data();
112     if ((sendto(sockfd, sendbuf, strlen(sendbuf), 0,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in))) != strlen(sendbuf))//发送数据
113     {
114             fprintf(stderr,"sendto error %s\n",strerror(errno));
115             exit(-1);
116     }
117 
118     while (true)
119     {
120         socklen_t len=sizeof(struct sockaddr_in);
121         int num = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0,(struct sockaddr*)&client_addr,&len);//获取数据
122         if (num 0)
123         {
124             fprintf(stderr,"recvfrom error %s\n",strerror(errno));
125             exit(-1);
126         }
127         else
128         {
129             cout  endl;
130             break;
131         }
132     }
133 
134     cout"exit..."endl;
135 
136     close(sockfd);//终止通信并释放套接字描述符
137 
138     return 0;
139 }
View Code

udpserv.cpp

技术图片技术图片
  1 #include  2 #includestring.h>
  3 #include   4 #include  5 #include  6 #include  7 #include
  8 #include  9 #include 10 #include 11 
 12 #define BUFSIZE 512
 13 #define PORT 4140
 14 #define MAXLISTEN 128
 15 
 16 /*error report*/
 17 static void bail(const char *on_what){
 18     fputs(strerror(errno), stderr);
 19     fputs(": ", stderr);
 20     fputs(on_what, stderr);
 21     fputc(\n, stderr);  
 22     exit(1);
 23 }
 24 
 25 int main()
 26 {
 27     std::string cal(double,double,double);
 28     double a=0.0,b=0.0,c=0.0;
 29 
 30 
 31     int sockfd;//server fd
 32     // int port;
 33     int newfd;//connect fd
 34     struct sockaddr_in server_addr;
 35     struct sockaddr_in client_addr;
 36 
 37     char reqbuf[BUFSIZE];
 38 
 39     // signal(SIGINT,my_handler);
 40 
 41     /*create server socket*/
 42     if((sockfd=socket(AF_INET,SOCK_DGRAM,0))0)//创建套接字描述符
 43     {
 44         fprintf(stderr,"Socket error %s\n",strerror(errno));
 45         exit(-1);
 46     }
 47 
 48     /*ready for server addr*/
 49     memset(&server_addr,0,sizeof(server_addr));
 50     server_addr.sin_family=AF_INET;
 51     server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
 52     server_addr.sin_port=htons(PORT);
 53 
 54     /*bind socket addr*/
 55     if((bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)))0)//将ip和port绑定到套接字上
 56     {
 57         fprintf(stderr,"Bind error %s\n",strerror(errno));
 58         exit(-1);
 59     }
 60 
 61     /*server main while*/
 62     while(true)
 63     {
 64         socklen_t size;
 65         size=sizeof(struct sockaddr_in);
 66         memset(&client_addr,0,sizeof(client_addr));
 67         memset(&reqbuf,\0,sizeof(reqbuf));
 68 
 69         /*recv data from client*/
 70         if((recvfrom(sockfd,reqbuf,sizeof(reqbuf),0,(struct sockaddr*)&client_addr,&size))0)//获取数据
 71         {
 72             fprintf(stderr,"Recvfrom error %s\n",strerror(errno));
 73             exit(-1);
 74         }
 75         std::coutstd::endl;
 76         
 77         /*send data to client*/
 78         char * cip=inet_ntoa(client_addr.sin_addr);
 79         char tmp[strlen(cip)];
 80         strcpy(tmp,cip);
 81         strcat(tmp," connected");
 82         const char *sendbuf=tmp;
 83         if((sendto(sockfd,sendbuf,strlen(sendbuf),0,(struct sockaddr*)&client_addr,size))!=strlen(sendbuf))//发送数据
 84         {
 85             fprintf(stderr,"Sendto error %s\n",strerror(errno));
 86             exit(-1);
 87         }
 88 
 89         /*do  with data*/
 90         memset(&reqbuf,\0,sizeof(reqbuf));
 91         if((recvfrom(sockfd,reqbuf,sizeof(reqbuf),0,(struct sockaddr*)&client_addr,&size))0)//获取数据
 92         {
 93             fprintf(stderr,"Recv error %s\n",strerror(errno));
 94             exit(-1);
 95         }
 96         std::coutstd::endl;
 97         
 98         std::istringstream sts(reqbuf);
 99         std::string sx,sy,sz;
100         sts>>sx>>sy>>sz;
101         // std::cout
102         
103         double a=stod(sx);
104         double b=stod(sy);
105         double c=stod(sz);
106         // std::cout
107 
108         std::string res=cal(a,b,c);
109         sendbuf=res.data();
110         // sendbuf="123345";
111 
112         char ch[BUFSIZE];
113         strcpy(ch,sendbuf);
114         std::coutstd::endl;
115 
116         if((sendto(sockfd,sendbuf,strlen(sendbuf),0,(struct sockaddr*)&client_addr,size))!=strlen(sendbuf))//发送数据
117         {
118             fprintf(stderr,"Send error %s\n",strerror(errno));
119             exit(-1);
120         }
121 
122     }
123 
124     /*close server fd*/
125     close(sockfd);//终止通信并释放套接字描述符
126 
127     std::cout"exit"std::endl;
128 
129     return 0;
130 }
131 
132 std::string cal(double a,double b,double c) {
133     std::string res="";
134  
135     double  x1, x2, discriminant, realPart, imaginaryPart;
136     // cout 137     // cin >> a >> b >> c;
138     discriminant = b*b - 4*a*c;
139     
140     if (discriminant > 0) {
141         x1 = (-b + sqrt(discriminant)) / (2*a);
142         x2 = (-b - sqrt(discriminant)) / (2*a);
143         // cout 144         // cout 145         // cout 
146         std::string s1=std::to_string(x1);
147         std::string s2=std::to_string(x2);
148         res="Roots are real and different.\nx1="+s1+"\nx2="+s2+"\n";
149     }
150     
151     else if (discriminant == 


评论


亲,登录后才可以留言!