【实验八】Linux 的socket编程
家电维修 2023-07-16 19:17www.caominkang.com家电维修技术
目录
一、问题
二、代码
1、serMain.cpp
2、server.h
3、server.cpp
4、cliMain.cpp
5、client.h
6、client.cpp
7、tcp.mk
三、运行结果
一、问题
客户端命令行传入一个数,判断奇偶,然后结果返回客户端。
函数参考博客
二、代码
1、serMain.cpp
#include
#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
#include
#include
#include
#include
#include
#include
#include"server.h"
using SAI=struct sockaddr_in;
using SA=struct sockaddr;
const int LEN=4096;
void handle_mes(const int confd){
hile(true){
char buf[LEN]={''};
int len=read(confd,buf,LEN);
if(len<=0){
std::cerr<<"client closen";
break;
}
buf[len]='';
if(atoi(buf)%2 == 0){
strcpy(buf,"偶数");
}else{
strcpy(buf,"奇数");
}
rite(confd,buf,strlen(buf));
}
}
void process(const int listenfd){
hile(true){
int confd=aept(listenfd,NULL,NULL);
if(confd>0){
handle_mes(confd);
}
}
}
void server(int port){
int listenfd;
do{
int listenfd=socket(PF_INET,SOCK_STREAM,0);
if(listenfd == -1){
std::cerr<<"socket rongn";
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<<"bind rongn";
break;
}
if(listen(listenfd,5) == -1){
std::cerr<<"listen rongn";
break;
}
process(listenfd);
}hile(0);
close(listenfd);
}
4、cliMain.cpp
#include
#include"client.h"
int main(int argc,char argv[]){
client(argv[1],atoi(argv[2]),argv[3]);
return 0;
}
5、client.h
void client(const char ip,const int port,char num);
6、client.cpp
#include
#include
#include
#include
#include
#include
#include"client.h"
using SAI=struct sockaddr_in;
using SA=struct sockaddr;
const int LEN=4096;
void process(const int confd,char num){
hile(true){
char buf[LEN]={''};
rite(confd,num,strlen(num));
int len=read(confd,buf,LEN);
buf[len]='';
std::cout<
7、tcp.mk
GCC=g++
CFLAG=-c
OFLAG=-o
SEXE=serMain
SOBJ=serMain.o server.o
CEXE=cliMain
COBJ=cliMain.o client.o
all:${SEXE} ${CEXE}
${SEXE}:${SOBJ}
${GCC} ${OFLAG} $@ $^
${CEXE}:${COBJ}
${GCC} ${OFLAG} $@ $^
%.o:%.cpp
${GCC} ${CFLAG} $^
clean:
rm .o
三、运行结果
#include#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
#include
#include
#include
#include
#include
#include
#include"server.h"
using SAI=struct sockaddr_in;
using SA=struct sockaddr;
const int LEN=4096;
void handle_mes(const int confd){
hile(true){
char buf[LEN]={''};
int len=read(confd,buf,LEN);
if(len<=0){
std::cerr<<"client closen";
break;
}
buf[len]='';
if(atoi(buf)%2 == 0){
strcpy(buf,"偶数");
}else{
strcpy(buf,"奇数");
}
rite(confd,buf,strlen(buf));
}
}
void process(const int listenfd){
hile(true){
int confd=aept(listenfd,NULL,NULL);
if(confd>0){
handle_mes(confd);
}
}
}
void server(int port){
int listenfd;
do{
int listenfd=socket(PF_INET,SOCK_STREAM,0);
if(listenfd == -1){
std::cerr<<"socket rongn";
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<<"bind rongn";
break;
}
if(listen(listenfd,5) == -1){
std::cerr<<"listen rongn";
break;
}
process(listenfd);
}hile(0);
close(listenfd);
}
4、cliMain.cpp
#include
#include"client.h"
int main(int argc,char argv[]){
client(argv[1],atoi(argv[2]),argv[3]);
return 0;
}
5、client.h
void client(const char ip,const int port,char num);
6、client.cpp
#include
#include
#include
#include
#include
#include
#include"client.h"
using SAI=struct sockaddr_in;
using SA=struct sockaddr;
const int LEN=4096;
void process(const int confd,char num){
hile(true){
char buf[LEN]={''};
rite(confd,num,strlen(num));
int len=read(confd,buf,LEN);
buf[len]='';
std::cout<
7、tcp.mk
GCC=g++
CFLAG=-c
OFLAG=-o
SEXE=serMain
SOBJ=serMain.o server.o
CEXE=cliMain
COBJ=cliMain.o client.o
all:${SEXE} ${CEXE}
${SEXE}:${SOBJ}
${GCC} ${OFLAG} $@ $^
${CEXE}:${COBJ}
${GCC} ${OFLAG} $@ $^
%.o:%.cpp
${GCC} ${CFLAG} $^
clean:
rm .o
三、运行结果
#include#include #include #include #include #include #include #include"server.h" using SAI=struct sockaddr_in; using SA=struct sockaddr; const int LEN=4096; void handle_mes(const int confd){ hile(true){ char buf[LEN]={''}; int len=read(confd,buf,LEN); if(len<=0){ std::cerr<<"client closen"; break; } buf[len]=''; if(atoi(buf)%2 == 0){ strcpy(buf,"偶数"); }else{ strcpy(buf,"奇数"); } rite(confd,buf,strlen(buf)); } } void process(const int listenfd){ hile(true){ int confd=aept(listenfd,NULL,NULL); if(confd>0){ handle_mes(confd); } } } void server(int port){ int listenfd; do{ int listenfd=socket(PF_INET,SOCK_STREAM,0); if(listenfd == -1){ std::cerr<<"socket rongn"; 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<<"bind rongn"; break; } if(listen(listenfd,5) == -1){ std::cerr<<"listen rongn"; break; } process(listenfd); }hile(0); close(listenfd); }
4、cliMain.cpp
#include
#include"client.h"
int main(int argc,char argv[]){
client(argv[1],atoi(argv[2]),argv[3]);
return 0;
}
5、client.h
void client(const char ip,const int port,char num);
6、client.cpp
#include
#include
#include
#include
#include
#include
#include"client.h"
using SAI=struct sockaddr_in;
using SA=struct sockaddr;
const int LEN=4096;
void process(const int confd,char num){
hile(true){
char buf[LEN]={''};
rite(confd,num,strlen(num));
int len=read(confd,buf,LEN);
buf[len]='';
std::cout<
7、tcp.mk
GCC=g++
CFLAG=-c
OFLAG=-o
SEXE=serMain
SOBJ=serMain.o server.o
CEXE=cliMain
COBJ=cliMain.o client.o
all:${SEXE} ${CEXE}
${SEXE}:${SOBJ}
${GCC} ${OFLAG} $@ $^
${CEXE}:${COBJ}
${GCC} ${OFLAG} $@ $^
%.o:%.cpp
${GCC} ${CFLAG} $^
clean:
rm .o
三、运行结果
void client(const char ip,const int port,char num);
6、client.cpp
#include
#include
#include
#include
#include
#include
#include"client.h"
using SAI=struct sockaddr_in;
using SA=struct sockaddr;
const int LEN=4096;
void process(const int confd,char num){
hile(true){
char buf[LEN]={''};
rite(confd,num,strlen(num));
int len=read(confd,buf,LEN);
buf[len]='';
std::cout<
7、tcp.mk
GCC=g++
CFLAG=-c
OFLAG=-o
SEXE=serMain
SOBJ=serMain.o server.o
CEXE=cliMain
COBJ=cliMain.o client.o
all:${SEXE} ${CEXE}
${SEXE}:${SOBJ}
${GCC} ${OFLAG} $@ $^
${CEXE}:${COBJ}
${GCC} ${OFLAG} $@ $^
%.o:%.cpp
${GCC} ${CFLAG} $^
clean:
rm .o
三、运行结果
7、tcp.mk
GCC=g++
CFLAG=-c
OFLAG=-o
SEXE=serMain
SOBJ=serMain.o server.o
CEXE=cliMain
COBJ=cliMain.o client.o
all:${SEXE} ${CEXE}
${SEXE}:${SOBJ}
${GCC} ${OFLAG} $@ $^
${CEXE}:${COBJ}
${GCC} ${OFLAG} $@ $^
%.o:%.cpp
${GCC} ${CFLAG} $^
clean:
rm .o
三、运行结果
上一篇:分析日志文件
下一篇:Win10打不开EXCEL 提示向程序发送命令时出现问题的