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

在linux和windows上实现

c/s模式

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

ax^2+bx+c=0

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

暂未考虑非法数据等问题

linux:

tcpclient.cpp

基于linux或windows的c/s的循环服务器求一元二次方程的根
  1 #include<iostream>
  2 #include <unistd.h>
  3 #include<sys/types.h>
  4 #include<sys/socket.h>
  5 #include<netdb.h>
  6 #include<arpa/inet.h>
  7 #include<cstring>
  8 #include<sstream>
  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<argc;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 << recvbuf << 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 << recvbuf << endl;
127             break;
128         }
129     }
130 
131     cout<<"exit..."<<endl;
132 
133     close(sockfd);//终止通信并释放套接字描述符
134 
135     return 0;
136 }
View Code

tcpserv.cpp

基于linux或windows的c/s的循环服务器求一元二次方程的根
  1 #include<iostream>
  2 #include<string.h>
  3 #include <unistd.h>
  4 #include<sys/types.h>
  5 #include<sys/socket.h>
  6 #include<netdb.h>
  7 #include<arpa/inet.h>
  8 #include<cmath>
  9 #include<cstring>
 10 #include<sstream>
 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(tmp<0)
 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 "<<inet_ntoa(client_addr.sin_addr)<<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::cout<<reqbuf<<std::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<<sendbuf<<std::endl; 
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::cout<<reqbuf<<std::endl;
114         
115         std::istringstream sts(reqbuf);
116         std::string sx,sy,sz;
117         sts>>sx>>sy>>sz;
118         // std::cout<<sx<<std::endl<<sy<<std::endl<<sz<<std::endl;
119         
120         double a=stod(sx);
121         double b=stod(sy);
122         double c=stod(sz);
123         // std::cout<<a<<' '<<b<<std::endl<<c<<std::endl;
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::cout<<ch<<std::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 << "输入 a, b 和 c: ";
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 << "Roots are real and different." << endl;
164         // cout << "x1 = " << x1 << endl;
165         // cout << "x2 = " << x2 << endl;
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 << "实根相同:" << endl;
173         // x1 = (-b + sqrt(discriminant)) / (2*a);
174         // cout << "x1 = x2 =" << x1 << endl;
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 << "实根不同:"  << endl;
183         // cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
184         // cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
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

基于linux或windows的c/s的循环服务器求一元二次方程的根
  1 #include<iostream>
  2 #include <unistd.h>
  3 #include<sys/types.h>
  4 #include<sys/socket.h>
  5 #include<netdb.h>
  6 #include<arpa/inet.h>
  7 #include<cstring>
  8 #include<sstream>
  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<argc;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))) < 0)
 78     // {
 79     //     cout << "Connect Error::" << GetLastError() << endl;
 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 << recvbuf << 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 << recvbuf << endl;
130             break;
131         }
132     }
133 
134     cout<<"exit..."<<endl;
135 
136     close(sockfd);//终止通信并释放套接字描述符
137 
138     return 0;
139 }
View Code

udpserv.cpp

基于linux或windows的c/s的循环服务器求一元二次方程的根
  1 #include<iostream>
  2 #include<string.h>
  3 #include <unistd.h>
  4 #include<sys/types.h>
  5 #include<sys/socket.h>
  6 #include<netdb.h>
  7 #include<arpa/inet.h>
  8 #include<cmath>
  9 #include<cstring>
 10 #include<sstream>
 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::cout<<reqbuf<<std::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::cout<<reqbuf<<std::endl;
 97         
 98         std::istringstream sts(reqbuf);
 99         std::string sx,sy,sz;
100         sts>>sx>>sy>>sz;
101         // std::cout<<sx<<std::endl<<sy<<std::endl<<sz<<std::endl;
102         
103         double a=stod(sx);
104         double b=stod(sy);
105         double c=stod(sz);
106         // std::cout<<a<<' '<<b<<std::endl<<c<<std::endl;
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::cout<<ch<<std::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 << "输入 a, b 和 c: ";
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 << "Roots are real and different." << endl;
144         // cout << "x1 = " << x1 << endl;
145         // cout << "x2 = " << x2 << endl;
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 == 0) {
152         // cout << "实根相同:" << endl;
153         // x1 = (-b + sqrt(discriminant)) / (2*a);
154         // cout << "x1 = x2 =" << x1 << endl;
155         std::string s1=std::to_string(x1);
156         res="real roots are same:\nx1=x2="+s1+"\n";
157     }
158  
159     else {
160         realPart = -b/(2*a);
161         imaginaryPart =sqrt(-discriminant)/(2*a);
162         // cout << "实根不同:"  << endl;
163         // cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
164         // cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
165         std::string s1r=std::to_string(realPart);
166         std::string s1i=std::to_string(imaginaryPart);
167         std::string s2r=std::to_string(realPart);
168         std::string s2i=std::to_string(imaginaryPart);
169         res="real roots are defferent:\nx1="+s1r+"+"+s1i+"i\nx2="+s2r+"-"+s2i+"i\n";
170     }
171  
172     return res;
173 }
View Code

windows:

tcplient.cpp

基于linux或windows的c/s的循环服务器求一元二次方程的根
  1 #include<iostream>
  2 #include<WinSock2.h>
  3 #include<cstring>
  4 #include<sstream>
  5 
  6 #pragma comment(lib, "ws2_32")
  7 
  8 using namespace std;
  9 
 10 // #define SERVERIP "192.168.41.32" 
 11 // #define SERVERPORT 4140
 12 
 13 void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
 14 {
 15     for(int i=0;i<argc;i++)
 16     {
 17         istringstream iss(argv[i]);
 18         string str;
 19         iss>>str;
 20         if(str=="ip")
 21         {
 22             *SERVERIP=argv[i+1];
 23         }
 24         else if(str=="port")
 25         {
 26             istringstream sts(argv[i+1]);
 27             string s_port;
 28             sts>>s_port;
 29             *SERVERPORT=stoi(s_port);
 30         }
 31         else
 32         {
 33             
 34         }
 35         
 36     }
 37 }
 38 
 39 int main(int argc,char* argv[])
 40 {
 41     const char* SERVERIP="192.168.41.32";
 42     int SERVERPORT=4140;
 43 
 44     getarg(argc,argv,&SERVERIP,&SERVERPORT);
 45 
 46     SOCKET sockfd;
 47     struct sockaddr_in server_addr;
 48     WSADATA ws;
 49     const char* sendbuf = (char*)"hello,this is client";
 50     char recvbuf[MAX_PATH];
 51 
 52     //init windows socket
 53     if (WSAStartup(MAKEWORD(2, 2), &ws) != 0)
 54     {
 55         cout << "Init Windows Socket Failed::" << GetLastError() << endl;
 56         return -1;
 57     }
 58 
 59     //create socket
 60     if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)//创建套接字
 61     {
 62         cout << "Create Socket Failed::" << GetLastError() << endl;
 63         return -1;
 64     }
 65 
 66     ZeroMemory(&server_addr, sizeof(server_addr));
 67     server_addr.sin_family = AF_INET;
 68     server_addr.sin_port = htons(SERVERPORT);
 69     server_addr.sin_addr.S_un.S_addr = inet_addr(SERVERIP);
 70 
 71     if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr))) < 0)//连接远程对等实体
 72     {
 73         cout << "Connect Error::" << GetLastError() << endl;
 74         return -1;
 75     }
 76 
 77     if ((send(sockfd, sendbuf, strlen(sendbuf), 0)) != strlen(sendbuf))//发送数据
 78     {
 79         cout << "Send Error::" << GetLastError() << endl;
 80         return -1;
 81     }
 82 
 83     ZeroMemory(recvbuf, sizeof(recvbuf));
 84     while (true)
 85     {
 86         int num = recv(sockfd, recvbuf, sizeof(recvbuf), 0);//获取数据
 87         if (num < 0)
 88         {
 89             cout << "Recv Error::" << GetLastError() << endl;
 90             return -1;
 91         }
 92         else
 93         {
 94             cout << recvbuf << endl;
 95             break;
 96         }
 97     }
 98 
 99     // double a=1.1,b=2.2,c=3.3;
100     double a,b,c;
101     cout<<"please input a,b,c (ax^2+bx+c=0) :"<<endl;
102     cin>>a>>b>>c;
103     string str=to_string(a)+' '+to_string(b)+' '+to_string(c);
104     sendbuf=str.data();
105     if ((send(sockfd, sendbuf, strlen(sendbuf), 0)) != strlen(sendbuf))//发送数据
106     {
107         cout << "Send Error::" << GetLastError() << endl;
108         return -1;
109     }
110 
111     while (true)
112     {
113         int num = recv(sockfd, recvbuf, sizeof(recvbuf), 0);
114         if (num < 0)
115         {
116             cout << "Recv Error::" << GetLastError() << endl;
117             return -1;
118         }
119         else
120         {
121             cout << recvbuf << endl;
122             break;
123         }
124     }
125 
126     cout<<"exit..."<<endl;
127 
128     closesocket(sockfd);//终止通信并释放套接字
129     WSACleanup();
130 
131     system("pause");
132     
133     return 0;
134 }
View Code

tcpserv.cpp

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

udpclient.cpp

基于linux或windows的c/s的循环服务器求一元二次方程的根
  1 #include<iostream>
  2 #include<WinSock2.h>
  3 #include<cstring>
  4 #include<sstream>
  5 
  6 #pragma comment(lib, "ws2_32")
  7 
  8 using namespace std;
  9 
 10 // #define SERVERIP "192.168.2.169"
 11 // #define SERVERPORT 4140
 12 
 13 void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
 14 {
 15     for(int i=0;i<argc;i++)
 16     {
 17         istringstream iss(argv[i]);
 18         string str;
 19         iss>>str;
 20         if(str=="ip")
 21         {
 22             *SERVERIP=argv[i+1];
 23         }
 24         else if(str=="port")
 25         {
 26             istringstream sts(argv[i+1]);
 27             string s_port;
 28             sts>>s_port;
 29             *SERVERPORT=stoi(s_port);
 30         }
 31         else
 32         {
 33             
 34         }
 35         
 36     }
 37 }
 38 
 39 int main(int argc,char* argv[])
 40 {
 41     const char* SERVERIP="192.168.41.32";
 42     int SERVERPORT=4140;
 43 
 44     getarg(argc,argv,&SERVERIP,&SERVERPORT);
 45 
 46     SOCKET sockfd;
 47     struct sockaddr_in server_addr;
 48     struct sockaddr_in client_addr;
 49     WSADATA ws;
 50     const char* sendbuf = (char*)"hello,this is client";
 51     char recvbuf[MAX_PATH];
 52 
 53     //init windows socket
 54     if (WSAStartup(MAKEWORD(2, 2), &ws) != 0)
 55     {
 56         cout << "Init Windows Socket Failed::" << GetLastError() << endl;
 57         return -1;
 58     }
 59 
 60     //create socket
 61     if ((sockfd = socket(AF_INET, SOCK_DGRAM,0)) == INVALID_SOCKET)//创建套接字描述符
 62     {
 63         cout << "Create Socket Failed::" << GetLastError() << endl;
 64         return -1;
 65     }
 66 
 67     ZeroMemory(&server_addr, sizeof(server_addr));
 68     server_addr.sin_family = AF_INET;
 69     server_addr.sin_port = htons(SERVERPORT);
 70     server_addr.sin_addr.S_un.S_addr = inet_addr(SERVERIP);
 71 
 72     // if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr))) < 0)
 73     // {
 74     //     cout << "Connect Error::" << GetLastError() << endl;
 75     //     return -1;
 76     // }
 77 
 78     if ((sendto(sockfd, sendbuf, strlen(sendbuf), 0,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in))) != strlen(sendbuf))//发送数据
 79     {
 80         cout << "Sendto Error::" << GetLastError() << endl;
 81         return -1;
 82     }
 83 
 84     ZeroMemory(&client_addr,sizeof(client_addr));
 85     ZeroMemory(recvbuf, sizeof(recvbuf));
 86     while (true)
 87     {
 88         int len=sizeof(struct sockaddr_in);
 89         int num = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0,(struct sockaddr*)&client_addr,&len);//获取数据
 90         if (num < 0)
 91         {
 92             cout << "Recvfrom Error::" << GetLastError() << endl;
 93             return -1;
 94         }
 95         else
 96         {
 97             cout << recvbuf << endl;
 98             break;
 99         }
100     }
101 
102     double a,b,c;
103     cout<<"please input a,b,c (ax^2+bx+c=0) :"<<endl;
104     cin>>a>>b>>c;
105     string str=to_string(a)+' '+to_string(b)+' '+to_string(c);
106     sendbuf=str.data();
107     if ((sendto(sockfd, sendbuf, strlen(sendbuf), 0,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in))) != strlen(sendbuf))//发送数据
108     {
109         cout << "Sendto Error::" << GetLastError() << endl;
110         return -1;
111     }
112 
113     while (true)
114     {
115         int len=sizeof(struct sockaddr_in);
116         int num = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0,(struct sockaddr*)&client_addr,&len);//获取数据
117         if (num < 0)
118         {
119             cout << "Recv Error::" << GetLastError() << endl;
120             return -1;
121         }
122         else
123         {
124             cout << recvbuf << endl;
125             break;
126         }
127     }
128 
129     cout<<"exit..."<<endl;
130 
131     closesocket(sockfd);//终止通信并释放套接字
132     WSACleanup();
133 
134     system("pause");
135     
136     return 0;
137 }
View Code

udpserv.cpp

基于linux或windows的c/s的循环服务器求一元二次方程的根
  1 #include<iostream>
  2 #include<winsock2.h>
  3 // #include<string.h>
  4 // #include<unistd.h>
  5 // #include<sys/types.h>
  6 #include<cmath>
  7 #include<cstring>
  8 #include<sstream>
  9 
 10 #pragma comment(lib, "ws2_32")
 11 
 12 using namespace std;
 13 
 14 #define BUFSIZE 512
 15 #define PORT 4140
 16 #define MAXLISTEN 128
 17 
 18 /*error report*/
 19 static void bail(const char *on_what){
 20     fputs(strerror(errno), stderr);
 21     fputs(": ", stderr);
 22     fputs(on_what, stderr);
 23     fputc('\n', stderr);  
 24     exit(1);
 25 }
 26 
 27 int main()
 28 {
 29     string cal(double,double,double);
 30     double a=0.0,b=0.0,c=0.0;
 31 
 32 
 33     int sockfd;//server fd
 34     // int port;
 35     int newfd;//connect fd
 36     WSADATA ws;
 37     struct sockaddr_in server_addr;
 38     struct sockaddr_in client_addr;
 39 
 40     char reqbuf[BUFSIZE];
 41 
 42     // signal(SIGINT,my_handler);
 43     
 44     //init windows socket
 45     if(WSAStartup(MAKEWORD(2,2),&ws)!=0)
 46     {
 47         cout << "Init Windows Socket Failed::" << GetLastError() << endl;
 48         exit(-1);
 49     }
 50 
 51     /*create server socket*/
 52     if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)//创建套接字描述符
 53     {
 54         fprintf(stderr,"Socket error %s\n",strerror(errno));
 55         exit(-1);
 56     }
 57 
 58     /*ready for server addr*/
 59     ZeroMemory(&server_addr,sizeof(server_addr));
 60     server_addr.sin_family=AF_INET;
 61     server_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
 62     server_addr.sin_port=htons(PORT);
 63 
 64     /*bind socket addr*/
 65     if((bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)))<0)//将ip和port绑定到套接字上
 66     {
 67         fprintf(stderr,"Bind error %s\n",strerror(errno));
 68         exit(-1);
 69     }
 70 
 71     /*server main while*/
 72     while(true)
 73     {
 74         int size;
 75         size=sizeof(struct sockaddr_in);
 76         ZeroMemory(&client_addr,sizeof(client_addr));
 77         ZeroMemory(&reqbuf,sizeof(reqbuf));
 78 
 79         /*recv data from client*/
 80         if((recvfrom(sockfd,reqbuf,sizeof(reqbuf),0,(struct sockaddr*)&client_addr,&size))<0)//获取数据
 81         {
 82             fprintf(stderr,"Recvfrom error %s\n",strerror(errno));
 83             exit(-1);
 84         }
 85         cout<<reqbuf<<endl;
 86         
 87         /*send data to client*/
 88         char * cip=inet_ntoa(client_addr.sin_addr);
 89         char tmp[strlen(cip)];
 90         strcpy(tmp,cip);
 91         strcat(tmp," connected");
 92         const char *sendbuf=tmp;
 93         if((sendto(sockfd,sendbuf,strlen(sendbuf),0,(struct sockaddr*)&client_addr,size))!=strlen(sendbuf))//发送数据
 94         {
 95             fprintf(stderr,"Sendto error %s\n",strerror(errno));
 96             exit(-1);
 97         }
 98 
 99         /*do  with data*/
100         ZeroMemory(&reqbuf,sizeof(reqbuf));
101         if((recvfrom(sockfd,reqbuf,sizeof(reqbuf),0,(struct sockaddr*)&client_addr,&size))<0)//获取数据
102         {
103             fprintf(stderr,"Recv error %s\n",strerror(errno));
104             exit(-1);
105         }
106         cout<<reqbuf<<endl;
107         
108         istringstream sts(reqbuf);
109         string sx,sy,sz;
110         sts>>sx>>sy>>sz;
111         // cout<<sx<<endl<<sy<<endl<<sz<<endl;
112         
113         double a=stod(sx);
114         double b=stod(sy);
115         double c=stod(sz);
116         // cout<<a<<' '<<b<<endl<<c<<endl;
117 
118         string res=cal(a,b,c);
119         sendbuf=res.data();
120         // sendbuf="123345";
121 
122         char ch[BUFSIZE];
123         strcpy(ch,sendbuf);
124         cout<<ch<<endl;
125 
126         if((sendto(sockfd,sendbuf,strlen(sendbuf),0,(struct sockaddr*)&client_addr,size))!=strlen(sendbuf))//发送数据
127         {
128             fprintf(stderr,"Send error %s\n",strerror(errno));
129             exit(-1);
130         }
131 
132     }
133 
134     /*close server fd*/
135     closesocket(sockfd);//终止通信并释放套接字
136     WSACleanup();
137 
138     cout<<"exit"<<endl;
139 
140     return 0;
141 }
142 
143 string cal(double a,double b,double c) {
144     string res="";
145  
146     double  x1, x2, discriminant, realPart, imaginaryPart;
147     // cout << "输入 a, b 和 c: ";
148     // cin >> a >> b >> c;
149     discriminant = b*b - 4*a*c;
150     
151     if (discriminant > 0) {
152         x1 = (-b + sqrt(discriminant)) / (2*a);
153         x2 = (-b - sqrt(discriminant)) / (2*a);
154         // cout << "Roots are real and different." << endl;
155         // cout << "x1 = " << x1 << endl;
156         // cout << "x2 = " << x2 << endl;
157         string s1=to_string(x1);
158         string s2=to_string(x2);
159         res="Roots are real and different.\nx1="+s1+"\nx2="+s2+"\n";
160     }
161     
162     else if (discriminant == 0) {
163         // cout << "实根相同:" << endl;
164         // x1 = (-b + sqrt(discriminant)) / (2*a);
165         // cout << "x1 = x2 =" << x1 << endl;
166         string s1=to_string(x1);
167         res="real roots are same:\nx1=x2="+s1+"\n";
168     }
169  
170     else {
171         realPart = -b/(2*a);
172         imaginaryPart =sqrt(-discriminant)/(2*a);
173         // cout << "实根不同:"  << endl;
174         // cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
175         // cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
176         string s1r=to_string(realPart);
177         string s1i=to_string(imaginaryPart);
178         string s2r=to_string(realPart);
179         string s2i=to_string(imaginaryPart);
180         res="real roots are defferent:\nx1="+s1r+"+"+s1i+"i\nx2="+s2r+"-"+s2i+"i\n";
181     }
182  
183     return res;
184 }
View Code

参考:

c++实例-求一元二次方程的根

其他:

循环的结束和跳出本来想用信号实现,但水平有限,发现是个坑,之后再填。

上一篇:C语言基础教程之错误处理


下一篇:subprocess模块