目录
一、问题
利用进程实现并发,通过socket通信实现信息的传输(加减乘除运算)。
服务端命令行输入:./serMain 9111
客户端命令行输入:./cilMain 127.0.0.1 9111
二、代码
1、serMain.cpp
#include<stdlib.h>
#include"server.h"
int main(int argc,char* argv[]){
server(atoi(argv[1]));
return 0;
}
2、server.h
void server(int port);
3、server.cpp
#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<cstring>
#include<stdlib.h>
#include<signal.h>
#include"server.h"
#include"message.h"
#include"mysig.h"
using SAI=struct sockaddr_in;
using SA=struct sockaddr;
const int LEN=sizeof(Message)+1;
void handle_mes(const int confd){
while(true){
char buf[LEN]={'\0'};
int len=read(confd,buf,LEN-1);
if(len<=0){
std::cerr<<"client close\n";
break;
}
Message m;
memset(&m,0,sizeof(m));
decode(buf,m);
switch(m.op){
case 0:
m.res=m.oprand1+m.oprand2;
break;
case 1:
m.res=m.oprand1-m.oprand2;
break;
case 2:
m.res=m.oprand1*m.oprand2;
break;
case 3:
if(m.oprand2!=0){
m.res=m.oprand1/m.oprand2;
}else{
m.status='F';
}
break;
default:
m.status='N';
break;
}
encode(buf,m);
write(confd,buf,LEN-1);
}
}
void process(const int listenfd){
signal(SIGCHLD,handle_child);
while(true){
int confd=accept(listenfd,NULL,NULL);
if(confd>0){
pid_t child=fork();
if(child == 0){
close(listenfd);
handle_mes(confd);
exit(0);
}
close(confd);
}
}
}
void server(int port){
int listenfd;
do{
int listenfd=socket(PF_INET,SOCK_STREAM,0);
if(listenfd == -1){
std::cerr<<"socket wrong\n";
break;
}
SAI saddr;
saddr.sin_family=AF_INET;
saddr.sin_addr.s_addr=htonl(INADDR_ANY);
saddr.sin_port=htons(port);
if(bind(listenfd,(SA*)&saddr,sizeof(saddr)) == -1){
std::cerr<<"listen wrong\n";
break;
}
if(listen(listenfd,5) == -1){
std::cerr<<"listen wrong\n";
break;
}
process(listenfd);
}while(0);
close(listenfd);
}
4、cliMain.cpp
#include<stdlib.h>
#include"client.h"
int main(int argc,char *argv[]){
client(argv[1],atoi(argv[2]));
return 0;
}
5、client.h
void client(const char* ip,const int port);
6、client.cpp
#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<cstring>
#include"client.h"
#include"message.h"
using SAI=struct sockaddr_in;
using SA=struct sockaddr;
const int LEN=sizeof(Message)+1;
void process(const int confd){
while(true){
Message m;
memset(&m,0,sizeof(m));
std::cout<<"please enter expression:\n";
std::cin>>m;
if(std::cin.eof()) break;
char buf[LEN]={'\0'};
m.status='S';
encode(buf,m);
write(confd,buf,LEN-1);
int len=read(confd,buf,LEN-1);
if(len<=0) {
std::cerr<<"server close\n";
break;
}
decode(buf,m);
std::cout<<m<<std::endl;
}
}
void client(const char *ip,const int port ){
int confd;
do{
confd=socket(PF_INET,SOCK_STREAM,0);
if(confd == -1){
std::cerr<<"socket wrong\n";
break;
}
SAI saddr;
saddr.sin_family=AF_INET;
inet_pton(AF_INET,ip,&saddr.sin_addr);
saddr.sin_port=htons(port);
if(connect(confd,(SA*)&saddr,sizeof(saddr))== -1){
std::cerr<<"connet wrong\n";
break;
}
process(confd);
}while(0);
close(confd);
}
7、message.h
#include<iostream>
struct Message{
int oprand1;
int op;
int oprand2;
int res;
char status;
};
std::istream &operator >>(std::istream& in, Message& m);
std::ostream &operator <<(std::ostream& out,const Message& m);
void decode(const char* buf,Message& m);
void encode(char* buf,const Message& m);
8、message.cpp
#include<arpa/inet.h>
#include<string.h>
#include"message.h"
std::istream& operator >>(std::istream& in ,Message& m){
in>>m.oprand1>>m.op>>m.oprand2;
return in;
}
std::ostream& operator <<(std::ostream& out ,const Message& m){
char ops[]={'+','-','*','/'};
if(m.status=='S'){
out<<m.oprand1<<ops[m.op]<<m.oprand2<<"="<<m.res;
}else if(m.status=='F'){
out<<"The divisor cannot be 0";
}else if(m.status=='N'){
out<<"Opreator iuput error";
}
return out;
}
void decode(const char* buf,Message& m){
size_t len=0;
memcpy(&m.oprand1,buf+len,sizeof(m.oprand1));
m.oprand1=ntohl(m.oprand1);
len+=sizeof(m.oprand1);
memcpy(&m.op,buf+len,sizeof(m.op));
m.op=ntohl(m.op);
len+=sizeof(m.op);
memcpy(&m.oprand2,buf+len,sizeof(m.oprand2));
m.oprand2=ntohl(m.oprand2);
len+=sizeof(m.oprand2);
memcpy(&m.res,buf+len,sizeof(m.res));
m.res=ntohl(m.res);
len+=sizeof(m.res);
memcpy(&m.status,buf+len,sizeof(m.status));
len+=sizeof(m.status);
}
void encode(char* buf,const Message& m){
size_t len=0;
*(int*)(buf+len)=htonl(m.oprand1);
len+=sizeof(m.oprand1);
*(int*)(buf+len)=htonl(m.op);
len+=sizeof(m.op);
*(int*)(buf+len)=htonl(m.oprand2);
len+=sizeof(m.oprand2);
*(int*)(buf+len)=htonl(m.res);
len+=sizeof(m.res);
memcpy(buf+len,&m.status,sizeof(m.status));
len+=sizeof(m.status);
}
9、mysig.h
void handle_child(int signo);
10、mysig.cpp
#include<iostream>
#include<sys/types.h>
#include<sys/wait.h>
#include"mysig.h"
void handle_child(int signo){
pid_t child;
while((child=waitpid(-1,NULL,WNOHANG)) > 0){
std::cerr<<child<<" finish\n";
}
}
11、tcp.mk
GCC=g++
CFLAG=-c
OFLAG=-o
SEXE=serMain
SOBJ=serMain.o server.o message.o mysig.o
CEXE=cliMain
COBJ=cliMain.o client.o message.o
all:${SEXE} ${CEXE}
${SEXE}:${SOBJ}
${GCC} ${OFLAG} $@ $^
${CEXE}:${COBJ}
${GCC} ${OFLAG} $@ $^
%.o:%.cpp
${GCC} ${CFLAG} $^
clean:
rm *.o
三、运行结果