在linux和windows上实现

c/s模式

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

ax^2+bx+c=0

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

暂未考虑非法数据等问题

linux:

tcpclient.cpp

 #include<iostream>
#include <unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<cstring>
#include<sstream> using namespace std; #define BUFSIZE 512 // #define SERVERIP "192.168.41.32"
// #define SERVERPORT 4140 /*error report*/
static void bail(const char *on_what){
fputs(strerror(errno), stderr);
fputs(": ", stderr);
fputs(on_what, stderr);
fputc('\n', stderr);
exit();
} void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
{
for(int i=;i<argc;i++)
{
istringstream iss(argv[i]);
string str;
iss>>str;
if(str=="ip")
{
*SERVERIP=argv[i+];
}
else if(str=="port")
{
istringstream sts(argv[i+]);
string s_port;
sts>>s_port;
*SERVERPORT=stoi(s_port);
}
else
{ } }
} int main(int argc,char* argv[])
{
const char* SERVERIP="192.168.41.32";
int SERVERPORT=; getarg(argc,argv,&SERVERIP,&SERVERPORT); int sockfd;
struct sockaddr_in server_addr;
const char* sendbuf = (char*)"hello,this is client";
char recvbuf[BUFSIZE]; //create socket
if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <)//创建套接字描述符
{
fprintf(stderr,"Socket error %s\n",strerror(errno));
exit(-);
} memset(&server_addr, ,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVERPORT);
server_addr.sin_addr.s_addr = inet_addr(SERVERIP); if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr))) < )//连接远程对等实体
{
fprintf(stderr,"connect error %s\n",strerror(errno));
exit(-);
} if ((send(sockfd, sendbuf, strlen(sendbuf), )) != strlen(sendbuf))//发送数据
{
fprintf(stderr,"send error %s\n",strerror(errno));
exit(-);
} memset(recvbuf,'\0', sizeof(recvbuf));
while (true)
{
int num = recv(sockfd, recvbuf, sizeof(recvbuf), );//获取数据
if (num < )
{
fprintf(stderr,"recv error %s\n",strerror(errno));
exit(-);
}
else
{
cout << recvbuf << endl;
break;
}
} // double a=1.1,b=2.2,c=3.3;
double a,b,c;
cout<<"please input a,b,c (ax^2+bx+c=0) :"<<endl;
cin>>a>>b>>c;
string str=to_string(a)+' '+to_string(b)+' '+to_string(c);
sendbuf=str.data();
if ((send(sockfd, sendbuf, strlen(sendbuf), )) != strlen(sendbuf))//发送数据
{
fprintf(stderr,"send error %s\n",strerror(errno));
exit(-);
} while (true)
{
int num = recv(sockfd, recvbuf, sizeof(recvbuf), );//获取数据
if (num < )
{
fprintf(stderr,"recv error %s\n",strerror(errno));
exit(-);
}
else
{
cout << recvbuf << endl;
break;
}
} cout<<"exit..."<<endl; close(sockfd);//终止通信并释放套接字描述符 return ;
}

tcpserv.cpp

 #include<iostream>
#include<string.h>
#include <unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<cmath>
#include<cstring>
#include<sstream> #define BUFSIZE 512
#define PORT 4140
#define MAXLISTEN 128 /*error report*/
static void bail(const char *on_what){
fputs(strerror(errno), stderr);
fputs(": ", stderr);
fputs(on_what, stderr);
fputc('\n', stderr);
exit();
} int main()
{
std::string cal(double,double,double);
double a=0.0,b=0.0,c=0.0; int sockfd;//server fd
// int port;
int newfd;//connect fd
struct sockaddr_in server_addr;
struct sockaddr_in client_addr; char reqbuf[BUFSIZE]; /*create server socket*/
if((sockfd=socket(AF_INET,SOCK_STREAM,))<)//创建套接字描述符
{
fprintf(stderr,"Socket error %s\n",strerror(errno));
exit(-);
} /*ready for server addr*/
memset(&server_addr,,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(PORT); /*bind socket addr*/
int tmp=bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr));//将ip和port绑定到套接字上
if(tmp<)
{
fprintf(stderr,"Bind error %s\n",strerror(errno));
exit(-);
} /*listen*/
if((listen(sockfd,MAXLISTEN))<)//将套接字设置为被动模式
{
fprintf(stderr,"Listen error %s\n",strerror(errno));
exit(-);
}
std::cout<<"waiting for client ..."<<std::endl; /*server main while*/
while(true)
{
socklen_t size;
size=sizeof(struct sockaddr_in); memset(&client_addr,,sizeof(client_addr));
memset(&reqbuf,'\0',sizeof(reqbuf)); /*accept client & create new fd*/
if((newfd=accept(sockfd,(struct sockaddr*)&client_addr,&size))<)//接受传入连接
{
fprintf(stderr,"Accept error %s\n",strerror(errno));
exit(-);
}
std::cout<<"Server got connect from "<<inet_ntoa(client_addr.sin_addr)<<std::endl; /*recv data from client*/
if((recv(newfd,reqbuf,sizeof(reqbuf),))<)//获取数据
{
fprintf(stderr,"Recv error %s\n",strerror(errno));
exit(-);
}
std::cout<<reqbuf<<std::endl; /*send data to client*/
char * cip=inet_ntoa(client_addr.sin_addr);
char tmp[strlen(cip)];
strcpy(tmp,cip);
strcat(tmp," connected");
const char *sendbuf=tmp;
// std::cout<<sendbuf<<std::endl;
if((send(newfd,sendbuf,strlen(sendbuf),))!=strlen(sendbuf))//发送数据
{
fprintf(stderr,"Send error %s\n",strerror(errno));
exit(-);
} /*do with data*/
memset(&reqbuf,'\0',sizeof(reqbuf));
if((recv(newfd,reqbuf,sizeof(reqbuf),))<)//获取数据
{
fprintf(stderr,"Recv error %s\n",strerror(errno));
exit(-);
}
std::cout<<reqbuf<<std::endl; std::istringstream sts(reqbuf);
std::string sx,sy,sz;
sts>>sx>>sy>>sz;
// std::cout<<sx<<std::endl<<sy<<std::endl<<sz<<std::endl; double a=stod(sx);
double b=stod(sy);
double c=stod(sz);
// std::cout<<a<<' '<<b<<std::endl<<c<<std::endl; std::string res=cal(a,b,c);
sendbuf=res.data();
// sendbuf="123345"; char ch[BUFSIZE];
strcpy(ch,sendbuf);
std::cout<<ch<<std::endl; if((send(newfd,sendbuf,strlen(sendbuf),))!=strlen(sendbuf))//发送数据
{
fprintf(stderr,"Send error %s\n",strerror(errno));
exit(-);
} /*close new fd*/
close(newfd);//终止通信并释放套接字描述符
} /*close server fd*/
close(sockfd);//释放套接字描述符 std::cout<<"exit"<<std::endl; return ;
} std::string cal(double a,double b,double c) {
std::string res=""; double x1, x2, discriminant, realPart, imaginaryPart;
// cout << "输入 a, b 和 c: ";
// cin >> a >> b >> c;
discriminant = b*b - *a*c; if (discriminant > ) {
x1 = (-b + sqrt(discriminant)) / (*a);
x2 = (-b - sqrt(discriminant)) / (*a);
// cout << "Roots are real and different." << endl;
// cout << "x1 = " << x1 << endl;
// cout << "x2 = " << x2 << endl;
std::string s1=std::to_string(x1);
std::string s2=std::to_string(x2);
res="Roots are real and different.\nx1="+s1+"\nx2="+s2+"\n";
} else if (discriminant == ) {
// cout << "实根相同:" << endl;
// x1 = (-b + sqrt(discriminant)) / (2*a);
// cout << "x1 = x2 =" << x1 << endl;
std::string s1=std::to_string(x1);
res="real roots are same:\nx1=x2="+s1+"\n";
} else {
realPart = -b/(*a);
imaginaryPart =sqrt(-discriminant)/(*a);
// cout << "实根不同:" << endl;
// cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
// cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
std::string s1r=std::to_string(realPart);
std::string s1i=std::to_string(imaginaryPart);
std::string s2r=std::to_string(realPart);
std::string s2i=std::to_string(imaginaryPart);
res="real roots are defferent:\nx1="+s1r+"+"+s1i+"i\nx2="+s2r+"-"+s2i+"i\n";
} return res;
}

udpclient.cpp

 #include<iostream>
#include <unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<cstring>
#include<sstream> using namespace std; #define BUFSIZE 512 // #define SERVERIP "192.168.2.169"
// #define SERVERPORT 4140 /*error report*/
static void bail(const char *on_what){
fputs(strerror(errno), stderr);
fputs(": ", stderr);
fputs(on_what, stderr);
fputc('\n', stderr);
exit();
} void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
{
for(int i=;i<argc;i++)
{
istringstream iss(argv[i]);
string str;
iss>>str;
if(str=="ip")
{
*SERVERIP=argv[i+];
}
else if(str=="port")
{
istringstream sts(argv[i+]);
string s_port;
sts>>s_port;
*SERVERPORT=stoi(s_port);
}
else
{ } }
} int main(int argc,char* argv[])
{
const char* SERVERIP="192.168.41.32";
int SERVERPORT=; getarg(argc,argv,&SERVERIP,&SERVERPORT); int sockfd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
const char* sendbuf = (char*)"hello,this is client";
char recvbuf[BUFSIZE]; //create socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM,)) <)//创建套接字描述符
{
fprintf(stderr,"Socket error %s\n",strerror(errno));
exit(-);
} memset(&server_addr, ,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVERPORT);
server_addr.sin_addr.s_addr = inet_addr(SERVERIP); // if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr))) < 0)
// {
// cout << "Connect Error::" << GetLastError() << endl;
// return -1;
// } if ((sendto(sockfd, sendbuf, strlen(sendbuf), ,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in))) != strlen(sendbuf))//发送数据
{
fprintf(stderr,"sendto error %s\n",strerror(errno));
exit(-);
} memset(&client_addr,,sizeof(client_addr));
memset(recvbuf, ,sizeof(recvbuf));
while (true)
{
socklen_t len=sizeof(struct sockaddr_in);
int num = recvfrom(sockfd, recvbuf, sizeof(recvbuf), ,(struct sockaddr*)&client_addr,&len);//获取数据
if (num < )
{
fprintf(stderr,"recvfrom error %s\n",strerror(errno));
exit(-);
}
else
{
cout << recvbuf << endl;
break;
}
} double a,b,c;
cout<<"please input a,b,c (ax^2+bx+c=0) :"<<endl;
cin>>a>>b>>c;
string str=to_string(a)+' '+to_string(b)+' '+to_string(c);
sendbuf=str.data();
if ((sendto(sockfd, sendbuf, strlen(sendbuf), ,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in))) != strlen(sendbuf))//发送数据
{
fprintf(stderr,"sendto error %s\n",strerror(errno));
exit(-);
} while (true)
{
socklen_t len=sizeof(struct sockaddr_in);
int num = recvfrom(sockfd, recvbuf, sizeof(recvbuf), ,(struct sockaddr*)&client_addr,&len);//获取数据
if (num < )
{
fprintf(stderr,"recvfrom error %s\n",strerror(errno));
exit(-);
}
else
{
cout << recvbuf << endl;
break;
}
} cout<<"exit..."<<endl; close(sockfd);//终止通信并释放套接字描述符 return ;
}

udpserv.cpp

 #include<iostream>
#include<string.h>
#include <unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<cmath>
#include<cstring>
#include<sstream> #define BUFSIZE 512
#define PORT 4140
#define MAXLISTEN 128 /*error report*/
static void bail(const char *on_what){
fputs(strerror(errno), stderr);
fputs(": ", stderr);
fputs(on_what, stderr);
fputc('\n', stderr);
exit();
} int main()
{
std::string cal(double,double,double);
double a=0.0,b=0.0,c=0.0; int sockfd;//server fd
// int port;
int newfd;//connect fd
struct sockaddr_in server_addr;
struct sockaddr_in client_addr; char reqbuf[BUFSIZE]; // signal(SIGINT,my_handler); /*create server socket*/
if((sockfd=socket(AF_INET,SOCK_DGRAM,))<)//创建套接字描述符
{
fprintf(stderr,"Socket error %s\n",strerror(errno));
exit(-);
} /*ready for server addr*/
memset(&server_addr,,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(PORT); /*bind socket addr*/
if((bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)))<)//将ip和port绑定到套接字上
{
fprintf(stderr,"Bind error %s\n",strerror(errno));
exit(-);
} /*server main while*/
while(true)
{
socklen_t size;
size=sizeof(struct sockaddr_in);
memset(&client_addr,,sizeof(client_addr));
memset(&reqbuf,'\0',sizeof(reqbuf)); /*recv data from client*/
if((recvfrom(sockfd,reqbuf,sizeof(reqbuf),,(struct sockaddr*)&client_addr,&size))<)//获取数据
{
fprintf(stderr,"Recvfrom error %s\n",strerror(errno));
exit(-);
}
std::cout<<reqbuf<<std::endl; /*send data to client*/
char * cip=inet_ntoa(client_addr.sin_addr);
char tmp[strlen(cip)];
strcpy(tmp,cip);
strcat(tmp," connected");
const char *sendbuf=tmp;
if((sendto(sockfd,sendbuf,strlen(sendbuf),,(struct sockaddr*)&client_addr,size))!=strlen(sendbuf))//发送数据
{
fprintf(stderr,"Sendto error %s\n",strerror(errno));
exit(-);
} /*do with data*/
memset(&reqbuf,'\0',sizeof(reqbuf));
if((recvfrom(sockfd,reqbuf,sizeof(reqbuf),,(struct sockaddr*)&client_addr,&size))<)//获取数据
{
fprintf(stderr,"Recv error %s\n",strerror(errno));
exit(-);
}
std::cout<<reqbuf<<std::endl; std::istringstream sts(reqbuf);
std::string sx,sy,sz;
sts>>sx>>sy>>sz;
// std::cout<<sx<<std::endl<<sy<<std::endl<<sz<<std::endl; double a=stod(sx);
double b=stod(sy);
double c=stod(sz);
// std::cout<<a<<' '<<b<<std::endl<<c<<std::endl; std::string res=cal(a,b,c);
sendbuf=res.data();
// sendbuf="123345"; char ch[BUFSIZE];
strcpy(ch,sendbuf);
std::cout<<ch<<std::endl; if((sendto(sockfd,sendbuf,strlen(sendbuf),,(struct sockaddr*)&client_addr,size))!=strlen(sendbuf))//发送数据
{
fprintf(stderr,"Send error %s\n",strerror(errno));
exit(-);
} } /*close server fd*/
close(sockfd);//终止通信并释放套接字描述符 std::cout<<"exit"<<std::endl; return ;
} std::string cal(double a,double b,double c) {
std::string res=""; double x1, x2, discriminant, realPart, imaginaryPart;
// cout << "输入 a, b 和 c: ";
// cin >> a >> b >> c;
discriminant = b*b - *a*c; if (discriminant > ) {
x1 = (-b + sqrt(discriminant)) / (*a);
x2 = (-b - sqrt(discriminant)) / (*a);
// cout << "Roots are real and different." << endl;
// cout << "x1 = " << x1 << endl;
// cout << "x2 = " << x2 << endl;
std::string s1=std::to_string(x1);
std::string s2=std::to_string(x2);
res="Roots are real and different.\nx1="+s1+"\nx2="+s2+"\n";
} else if (discriminant == ) {
// cout << "实根相同:" << endl;
// x1 = (-b + sqrt(discriminant)) / (2*a);
// cout << "x1 = x2 =" << x1 << endl;
std::string s1=std::to_string(x1);
res="real roots are same:\nx1=x2="+s1+"\n";
} else {
realPart = -b/(*a);
imaginaryPart =sqrt(-discriminant)/(*a);
// cout << "实根不同:" << endl;
// cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
// cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
std::string s1r=std::to_string(realPart);
std::string s1i=std::to_string(imaginaryPart);
std::string s2r=std::to_string(realPart);
std::string s2i=std::to_string(imaginaryPart);
res="real roots are defferent:\nx1="+s1r+"+"+s1i+"i\nx2="+s2r+"-"+s2i+"i\n";
} return res;
}

windows:

tcplient.cpp

 #include<iostream>
#include<WinSock2.h>
#include<cstring>
#include<sstream> #pragma comment(lib, "ws2_32") using namespace std; // #define SERVERIP "192.168.41.32"
// #define SERVERPORT 4140 void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
{
for(int i=;i<argc;i++)
{
istringstream iss(argv[i]);
string str;
iss>>str;
if(str=="ip")
{
*SERVERIP=argv[i+];
}
else if(str=="port")
{
istringstream sts(argv[i+]);
string s_port;
sts>>s_port;
*SERVERPORT=stoi(s_port);
}
else
{ } }
} int main(int argc,char* argv[])
{
const char* SERVERIP="192.168.41.32";
int SERVERPORT=; getarg(argc,argv,&SERVERIP,&SERVERPORT); SOCKET sockfd;
struct sockaddr_in server_addr;
WSADATA ws;
const char* sendbuf = (char*)"hello,this is client";
char recvbuf[MAX_PATH]; //init windows socket
if (WSAStartup(MAKEWORD(, ), &ws) != )
{
cout << "Init Windows Socket Failed::" << GetLastError() << endl;
return -;
} //create socket
if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)//创建套接字
{
cout << "Create Socket Failed::" << GetLastError() << endl;
return -;
} ZeroMemory(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVERPORT);
server_addr.sin_addr.S_un.S_addr = inet_addr(SERVERIP); if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr))) < )//连接远程对等实体
{
cout << "Connect Error::" << GetLastError() << endl;
return -;
} if ((send(sockfd, sendbuf, strlen(sendbuf), )) != strlen(sendbuf))//发送数据
{
cout << "Send Error::" << GetLastError() << endl;
return -;
} ZeroMemory(recvbuf, sizeof(recvbuf));
while (true)
{
int num = recv(sockfd, recvbuf, sizeof(recvbuf), );//获取数据
if (num < )
{
cout << "Recv Error::" << GetLastError() << endl;
return -;
}
else
{
cout << recvbuf << endl;
break;
}
} // double a=1.1,b=2.2,c=3.3;
double a,b,c;
cout<<"please input a,b,c (ax^2+bx+c=0) :"<<endl;
cin>>a>>b>>c;
string str=to_string(a)+' '+to_string(b)+' '+to_string(c);
sendbuf=str.data();
if ((send(sockfd, sendbuf, strlen(sendbuf), )) != strlen(sendbuf))//发送数据
{
cout << "Send Error::" << GetLastError() << endl;
return -;
} while (true)
{
int num = recv(sockfd, recvbuf, sizeof(recvbuf), );
if (num < )
{
cout << "Recv Error::" << GetLastError() << endl;
return -;
}
else
{
cout << recvbuf << endl;
break;
}
} cout<<"exit..."<<endl; closesocket(sockfd);//终止通信并释放套接字
WSACleanup(); system("pause"); return ;
}

tcpserv.cpp

 #include<iostream>
#include<WinSock2.h>
// #include<string.h>
// #include<unistd.h>
// #include<sys/types.h>
#include<cmath>
#include<cstring>
#include<sstream> #pragma comment(lib, "ws2_32") using namespace std; #define BUFSIZE 512
#define PORT 4140
#define MAXLISTEN 128 /*error report*/
static void bail(const char *on_what){
fputs(strerror(errno), stderr);
fputs(": ", stderr);
fputs(on_what, stderr);
fputc('\n', stderr);
exit();
} int main()
{
string cal(double,double,double);
double a=0.0,b=0.0,c=0.0; int sockfd;//server fd
// int port;
int newfd;//connect fd
WSADATA ws;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr; char reqbuf[BUFSIZE]; //init windows socket
if(WSAStartup(MAKEWORD(,),&ws)!=)
{
cout << "Init Windows Socket Failed::" << GetLastError() << endl;
exit(-);
} /*create server socket*/
if((sockfd=socket(AF_INET,SOCK_STREAM,))==INVALID_SOCKET)//创建套接字
{
fprintf(stderr,"Socket error %s\n",strerror(errno));
exit(-);
} /*ready for server addr*/
ZeroMemory(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(PORT); /*bind socket addr*/
int tmp=bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr));//将ip和port绑定到套接字上
if(tmp<)
{
fprintf(stderr,"Bind error %s\n",strerror(errno));
exit(-);
} /*listen*/
if((listen(sockfd,MAXLISTEN))<)//设置为被动模式
{
fprintf(stderr,"Listen error %s\n",strerror(errno));
exit(-);
}
cout<<"waiting for client ..."<<endl; /*server main while*/
while(true)
{
int size;
size=sizeof(struct sockaddr_in); ZeroMemory(&client_addr,sizeof(client_addr));
ZeroMemory(&reqbuf,sizeof(reqbuf)); /*accept client & create new fd*/
if((newfd=accept(sockfd,(struct sockaddr*)&client_addr,&size))<)//接受传入请求
{
fprintf(stderr,"Accept error %s\n",strerror(errno));
exit(-);
}
cout<<"Server got connect from "<<inet_ntoa(client_addr.sin_addr)<<endl; /*recv data from client*/
if((recv(newfd,reqbuf,sizeof(reqbuf),))<)//获取数据
{
fprintf(stderr,"Recv error %s\n",strerror(errno));
exit(-);
}
cout<<reqbuf<<endl; /*send data to client*/
char * cip=inet_ntoa(client_addr.sin_addr);
char tmp[strlen(cip)];
strcpy(tmp,cip);
strcat(tmp," connected");
const char *sendbuf=tmp;
// cout<<sendbuf<<endl;
if((send(newfd,sendbuf,strlen(sendbuf),))!=strlen(sendbuf))//发送数据
{
fprintf(stderr,"Send error %s\n",strerror(errno));
exit(-);
} /*do with data*/
memset(&reqbuf,'\0',sizeof(reqbuf));
if((recv(newfd,reqbuf,sizeof(reqbuf),))<)//获取数据
{
fprintf(stderr,"Recv error %s\n",strerror(errno));
exit(-);
}
cout<<reqbuf<<endl; istringstream sts(reqbuf);
string sx,sy,sz;
sts>>sx>>sy>>sz;
// cout<<sx<<endl<<sy<<endl<<sz<<endl; double a=stod(sx);
double b=stod(sy);
double c=stod(sz);
// cout<<a<<' '<<b<<endl<<c<<endl; string res=cal(a,b,c);
sendbuf=res.data();
// sendbuf="123345"; char ch[BUFSIZE];
strcpy(ch,sendbuf);
cout<<ch<<endl; if((send(newfd,sendbuf,strlen(sendbuf),))!=strlen(sendbuf))//发送数据
{
fprintf(stderr,"Send error %s\n",strerror(errno));
exit(-);
} /*close new fd*/
closesocket(newfd);//终止通信并释放套接字
} /*close server fd*/
closesocket(sockfd);//释放套接字
WSACleanup(); cout<<"exit"<<endl; return ;
} string cal(double a,double b,double c) {
string res=""; double x1, x2, discriminant, realPart, imaginaryPart;
// cout << "输入 a, b 和 c: ";
// cin >> a >> b >> c;
discriminant = b*b - *a*c; if (discriminant > ) {
x1 = (-b + sqrt(discriminant)) / (*a);
x2 = (-b - sqrt(discriminant)) / (*a);
// cout << "Roots are real and different." << endl;
// cout << "x1 = " << x1 << endl;
// cout << "x2 = " << x2 << endl;
string s1=to_string(x1);
string s2=to_string(x2);
res="Roots are real and different.\nx1="+s1+"\nx2="+s2+"\n";
} else if (discriminant == ) {
// cout << "实根相同:" << endl;
// x1 = (-b + sqrt(discriminant)) / (2*a);
// cout << "x1 = x2 =" << x1 << endl;
string s1=to_string(x1);
res="real roots are same:\nx1=x2="+s1+"\n";
} else {
realPart = -b/(*a);
imaginaryPart =sqrt(-discriminant)/(*a);
// cout << "实根不同:" << endl;
// cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
// cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
string s1r=to_string(realPart);
string s1i=to_string(imaginaryPart);
string s2r=to_string(realPart);
string s2i=to_string(imaginaryPart);
res="real roots are defferent:\nx1="+s1r+"+"+s1i+"i\nx2="+s2r+"-"+s2i+"i\n";
} return res;
}

udpclient.cpp

 #include<iostream>
#include<WinSock2.h>
#include<cstring>
#include<sstream> #pragma comment(lib, "ws2_32") using namespace std; // #define SERVERIP "192.168.2.169"
// #define SERVERPORT 4140 void getarg(int argc,char* argv[],const char** SERVERIP,int* SERVERPORT)
{
for(int i=;i<argc;i++)
{
istringstream iss(argv[i]);
string str;
iss>>str;
if(str=="ip")
{
*SERVERIP=argv[i+];
}
else if(str=="port")
{
istringstream sts(argv[i+]);
string s_port;
sts>>s_port;
*SERVERPORT=stoi(s_port);
}
else
{ } }
} int main(int argc,char* argv[])
{
const char* SERVERIP="192.168.41.32";
int SERVERPORT=; getarg(argc,argv,&SERVERIP,&SERVERPORT); SOCKET sockfd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
WSADATA ws;
const char* sendbuf = (char*)"hello,this is client";
char recvbuf[MAX_PATH]; //init windows socket
if (WSAStartup(MAKEWORD(, ), &ws) != )
{
cout << "Init Windows Socket Failed::" << GetLastError() << endl;
return -;
} //create socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM,)) == INVALID_SOCKET)//创建套接字描述符
{
cout << "Create Socket Failed::" << GetLastError() << endl;
return -;
} ZeroMemory(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVERPORT);
server_addr.sin_addr.S_un.S_addr = inet_addr(SERVERIP); // if ((connect(sockfd, (struct sockaddr*) & server_addr, sizeof(struct sockaddr))) < 0)
// {
// cout << "Connect Error::" << GetLastError() << endl;
// return -1;
// } if ((sendto(sockfd, sendbuf, strlen(sendbuf), ,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in))) != strlen(sendbuf))//发送数据
{
cout << "Sendto Error::" << GetLastError() << endl;
return -;
} ZeroMemory(&client_addr,sizeof(client_addr));
ZeroMemory(recvbuf, sizeof(recvbuf));
while (true)
{
int len=sizeof(struct sockaddr_in);
int num = recvfrom(sockfd, recvbuf, sizeof(recvbuf), ,(struct sockaddr*)&client_addr,&len);//获取数据
if (num < )
{
cout << "Recvfrom Error::" << GetLastError() << endl;
return -;
}
else
{
cout << recvbuf << endl;
break;
}
} double a,b,c;
cout<<"please input a,b,c (ax^2+bx+c=0) :"<<endl;
cin>>a>>b>>c;
string str=to_string(a)+' '+to_string(b)+' '+to_string(c);
sendbuf=str.data();
if ((sendto(sockfd, sendbuf, strlen(sendbuf), ,(struct sockaddr*)&server_addr,sizeof(struct sockaddr_in))) != strlen(sendbuf))//发送数据
{
cout << "Sendto Error::" << GetLastError() << endl;
return -;
} while (true)
{
int len=sizeof(struct sockaddr_in);
int num = recvfrom(sockfd, recvbuf, sizeof(recvbuf), ,(struct sockaddr*)&client_addr,&len);//获取数据
if (num < )
{
cout << "Recv Error::" << GetLastError() << endl;
return -;
}
else
{
cout << recvbuf << endl;
break;
}
} cout<<"exit..."<<endl; closesocket(sockfd);//终止通信并释放套接字
WSACleanup(); system("pause"); return ;
}

udpserv.cpp

 #include<iostream>
#include<winsock2.h>
// #include<string.h>
// #include<unistd.h>
// #include<sys/types.h>
#include<cmath>
#include<cstring>
#include<sstream> #pragma comment(lib, "ws2_32") using namespace std; #define BUFSIZE 512
#define PORT 4140
#define MAXLISTEN 128 /*error report*/
static void bail(const char *on_what){
fputs(strerror(errno), stderr);
fputs(": ", stderr);
fputs(on_what, stderr);
fputc('\n', stderr);
exit();
} int main()
{
string cal(double,double,double);
double a=0.0,b=0.0,c=0.0; int sockfd;//server fd
// int port;
int newfd;//connect fd
WSADATA ws;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr; char reqbuf[BUFSIZE]; // signal(SIGINT,my_handler); //init windows socket
if(WSAStartup(MAKEWORD(,),&ws)!=)
{
cout << "Init Windows Socket Failed::" << GetLastError() << endl;
exit(-);
} /*create server socket*/
if((sockfd=socket(AF_INET,SOCK_DGRAM,))<)//创建套接字描述符
{
fprintf(stderr,"Socket error %s\n",strerror(errno));
exit(-);
} /*ready for server addr*/
ZeroMemory(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(PORT); /*bind socket addr*/
if((bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)))<)//将ip和port绑定到套接字上
{
fprintf(stderr,"Bind error %s\n",strerror(errno));
exit(-);
} /*server main while*/
while(true)
{
int size;
size=sizeof(struct sockaddr_in);
ZeroMemory(&client_addr,sizeof(client_addr));
ZeroMemory(&reqbuf,sizeof(reqbuf)); /*recv data from client*/
if((recvfrom(sockfd,reqbuf,sizeof(reqbuf),,(struct sockaddr*)&client_addr,&size))<)//获取数据
{
fprintf(stderr,"Recvfrom error %s\n",strerror(errno));
exit(-);
}
cout<<reqbuf<<endl; /*send data to client*/
char * cip=inet_ntoa(client_addr.sin_addr);
char tmp[strlen(cip)];
strcpy(tmp,cip);
strcat(tmp," connected");
const char *sendbuf=tmp;
if((sendto(sockfd,sendbuf,strlen(sendbuf),,(struct sockaddr*)&client_addr,size))!=strlen(sendbuf))//发送数据
{
fprintf(stderr,"Sendto error %s\n",strerror(errno));
exit(-);
} /*do with data*/
ZeroMemory(&reqbuf,sizeof(reqbuf));
if((recvfrom(sockfd,reqbuf,sizeof(reqbuf),,(struct sockaddr*)&client_addr,&size))<)//获取数据
{
fprintf(stderr,"Recv error %s\n",strerror(errno));
exit(-);
}
cout<<reqbuf<<endl; istringstream sts(reqbuf);
string sx,sy,sz;
sts>>sx>>sy>>sz;
// cout<<sx<<endl<<sy<<endl<<sz<<endl; double a=stod(sx);
double b=stod(sy);
double c=stod(sz);
// cout<<a<<' '<<b<<endl<<c<<endl; string res=cal(a,b,c);
sendbuf=res.data();
// sendbuf="123345"; char ch[BUFSIZE];
strcpy(ch,sendbuf);
cout<<ch<<endl; if((sendto(sockfd,sendbuf,strlen(sendbuf),,(struct sockaddr*)&client_addr,size))!=strlen(sendbuf))//发送数据
{
fprintf(stderr,"Send error %s\n",strerror(errno));
exit(-);
} } /*close server fd*/
closesocket(sockfd);//终止通信并释放套接字
WSACleanup(); cout<<"exit"<<endl; return ;
} string cal(double a,double b,double c) {
string res=""; double x1, x2, discriminant, realPart, imaginaryPart;
// cout << "输入 a, b 和 c: ";
// cin >> a >> b >> c;
discriminant = b*b - *a*c; if (discriminant > ) {
x1 = (-b + sqrt(discriminant)) / (*a);
x2 = (-b - sqrt(discriminant)) / (*a);
// cout << "Roots are real and different." << endl;
// cout << "x1 = " << x1 << endl;
// cout << "x2 = " << x2 << endl;
string s1=to_string(x1);
string s2=to_string(x2);
res="Roots are real and different.\nx1="+s1+"\nx2="+s2+"\n";
} else if (discriminant == ) {
// cout << "实根相同:" << endl;
// x1 = (-b + sqrt(discriminant)) / (2*a);
// cout << "x1 = x2 =" << x1 << endl;
string s1=to_string(x1);
res="real roots are same:\nx1=x2="+s1+"\n";
} else {
realPart = -b/(*a);
imaginaryPart =sqrt(-discriminant)/(*a);
// cout << "实根不同:" << endl;
// cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
// cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
string s1r=to_string(realPart);
string s1i=to_string(imaginaryPart);
string s2r=to_string(realPart);
string s2i=to_string(imaginaryPart);
res="real roots are defferent:\nx1="+s1r+"+"+s1i+"i\nx2="+s2r+"-"+s2i+"i\n";
} return res;
}

参考:

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

其他:

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

基于linux或windows的c/s的循环服务器求一元二次方程的根的更多相关文章

  1. 基于linux或windows平台上的c/s简单通信

    linux: tcpclient.cpp #include<iostream> #include<unistd.h> #include<sys/types.h> # ...

  2. Linux与Windows比较出的20个优势

    Linux相信大家并不会陌生,Android(安卓或安致)就是基于Linux平台的开源手机操作系统,在电脑方面有ubuntu(中文名:乌班图)等等也是基于linux. Windows与Linux Li ...

  3. ISE(Iris Server Engine)是一个基于现代C++的跨平台(Linux和Windows)框架

    ISE(Iris Server Engine)是一个基于现代C++的跨平台(Linux和Windows)的高性能多线程并发网络服务器程序框架.它封装了琐碎的socket以及各种操作系统APIs,以面向 ...

  4. 基于Linux平台的libpcap源码分析和优化

    目录 1..... libpcap简介... 1 2..... libpcap捕包过程... 2 2.1        数据包基本捕包流程... 2 2.2        libpcap捕包过程... ...

  5. Linux与Windows xp操作系统启动过程

    Linux启动过程: 第一步,加载BIOS,当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备 ...

  6. redis3.0.6安装(linux和windows)

    官网上描述安装方法如下:$ wget http://download.redis.io/releases/redis-3.0.6.tar.gz$ tar xzf redis-3.0.6.tar.gz$ ...

  7. 深度剖析Linux与Windows系统的区别

    当我们每个人接触Linux之前,应该先接触的都是windows吧?但我们一般接触Linux后,习惯linux的管理和使用方法后,我们再回过头再来使用windows的时候,内心其实是拒绝的.我们会觉得图 ...

  8. 深度剖析Linux与Windows系统的区别,新手必读!

    当我们每个人接触Linux之前,应该先接触的都是windows吧?但我们一般接触Linux后,习惯linux的管理和使用方法后,我们再回过头再来使用windows的时候,内心其实是拒绝的.我们会觉得图 ...

  9. 高性能Linux服务器 第10章 基于Linux服务器的性能分析与优化

    高性能Linux服务器 第10章    基于Linux服务器的性能分析与优化 作为一名Linux系统管理员,最主要的工作是优化系统配置,使应用在系统上以最优的状态运行.但硬件问题.软件问题.网络环境等 ...

随机推荐

  1. Kubernetes实战总结 - 系统初始化

    设置系统主机名以及Host文件的相互解析 hostnamectl set-hostname k8s-master01 cat >> /etc/hosts <<EOF 192.1 ...

  2. canvas技术概述

    canvas简介 在学习一项新技术之前,先了解这项技术的历史发展及成因会帮助我们更深刻的理解这项技术. 历史上,canvas最早是由Apple Inc. 提出的,在Mac OS X webkit中创建 ...

  3. Building Applications with Force.com and VisualForce(Dev401)( 八):Designing Applications for Multiple Users: Controling Access to Records.

    Module Objectives1.List feature that affect access to data at the record level.2.List the organizati ...

  4. 一 JVM垃圾回收模型

    一 JVM垃圾回收模型 一. GC算法 1.1 标记-清除算法(Mark-Sweep) 算法分为"标记"和"清除"两个阶段首先标记出所有需要回收的对象,然后回收 ...

  5. AC自动机(初步学习)

    一开始讲AC自动机就是在字典树上做一个KMP,吓得我感觉好难,不过了解了以后,感觉也就是有点难度,不吓人. 它只是在字典树上用了KMP的思想 典型问题:给n个模式串和一个文本串,问有多少个模式串在文本 ...

  6. javascript实现炫酷魔方

    实现效果: 魔方动态转换,同时每个面里的每个块都能进行动态变换. 实现代码: <!DOCTYPE html> <html> <head> <meta char ...

  7. A 皮呵德

    时间限制 : 5000 MS   空间限制 : 262144 KB 问题描述 Eyiz正在与邪恶的Dgdon战斗. 为了打败Dgdon,Eyiz决定召唤PhantasmDragon来帮助他. 但是,召 ...

  8. Blazor入门笔记(4)-组件的生命周期

    1.环境 VS2019 16.5.1.NET Core SDK 3.1.200Blazor WebAssembly Templates 3.2.0-preview2.20160.5 2.简介 Blaz ...

  9. 求你了,别再问我Zookeeper如何实现分布式锁了!!!

    导读 真是有人(锁)的地方就有江湖(事务),今天不谈江湖,来撩撩人. 分布式锁的概念.为什么使用分布式锁,想必大家已经很清楚了.前段时间作者写过Redis是如何实现分布式锁,今天这篇文章来谈谈Zook ...

  10. 监控一姐Grafana你可认识?

    我们先来认识一下格拉法纳——Grafana. 我去,这不就是实时监控大屏吗?记得 N 年前,部门为了做这么个功能,还花重金请专业公司搞过类似的图,现在想想其实也很简单呀. 话又说回来,其实 Grafa ...