服务器端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void sig_handler(int signo)
{
pid_t pid;
int stat;
pid=waitpid(-1,&stat,WNOHANG);
while(pid>0){
printf("child process terminated (PID:%ld)\n",(long)getpid());
pid=waitpid(-1,&stat,WNOHANG);
}
return ;
}
typedef struct {
char s[10];
float a;
float b;
char m[1024];
}Message;
int main(int argc,char *argv[])
{
socklen_t clt_addr_len;
int listen_fd;
int com_fd;
int ret;
int result,a,b;
int s[2];
static char recv_buf[1024];
int len;
int port;
pid_t pid;
char *delim = " ";
char *p;
char *c;
struct sockaddr_in clt_addr;
struct sockaddr_in srv_addr;
//服务器端运行时要给出端口信息,该端口为监听端口
if(argc!=2){
printf("Usage:%s port\n",argv[0]);
return 1;
}
//获得输入的端口
port=atoi(argv[1]);
//设置信号处理函数,也可以使用sigaction函数
if(signal(SIGCHLD,sig_handler)<0){
perror("cannot set the signal");
return 1;
}
//创建套接字用于服务器的监听
listen_fd=socket(PF_INET,SOCK_STREAM,0);
if(listen_fd<0){
perror("cannot create listening socket");
return 1;
}
bzero(&srv_addr,sizeof(srv_addr));
srv_addr.sin_family=AF_INET;
srv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
srv_addr.sin_port=htons(port);
ret=bind(listen_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));
if(ret==1)
{
perror("cannot bind server socket");
close(listen_fd);
return 1;
}
//监听指定端口 ,连接一千个客户端
ret=listen(listen_fd,1000);
if(ret==-1){
perror("cannot listen the client connect request");
return 1;
}
//对每个连接来的客户端创建一个进程,单独与其进行通信
//首先调用read函数读取客户端发送来的信息
//当输入exit时程序退出
while(1){
len=sizeof(clt_addr);
com_fd=accept(listen_fd,(struct sockaddr*)&clt_addr,&len);
if(com_fd<0){
if(errno==EINTR){
continue;
}else{
perror("cannot accept client connect request");
close(listen_fd);
return 1;
}
}
pid=fork();
if(pid<0){
perror("cannot create the child process");
close(listen_fd);
return 1;
}else if(pid==0){
Message mes;
int n=sizeof(mes);
while((len = read(com_fd,&mes,n))>0){
if(strcmp(mes.s,"echo")==0){
printf("接收到客户端的信息: %s %s\n",mes.s,mes.m);
sprintf(recv_buf,"%s",mes.m);
}
else if(strcmp(mes.s,"add")==0){
printf("接收到客户端的信息:%s %f %f\n",mes.s,mes.a,mes.b);
float result=mes.a+mes.b;
sprintf(recv_buf,"%f",result);
}
else if(strcmp(mes.s,"sub")==0){
printf("接收到客户端的信息:%s %f %f\n",mes.s,mes.a,mes.b);
float result=mes.a-mes.b;
sprintf(recv_buf,"%f",result);
}
else if(strcmp(mes.s,"mul")==0){
printf("接收到客户端的信息:%s %f %f\n",mes.s,mes.a,mes.b);
float result=mes.a*mes.b;
sprintf(recv_buf,"%f",result);
}
else if(strcmp(mes.s,"div")==0){
printf("接收到客户端的信息:%s %f %f\n",mes.s,mes.a,mes.b);
if(mes.b==0){
printf("除数不能为零!\n");
}else{
float result=mes.a/mes.b;
sprintf(recv_buf,"%f",result);
}
}
else if(strcmp(mes.s,"exit")==0){
close(com_fd);
return 0;
}else
printf("非法操作!\n");
write(com_fd,recv_buf,strlen(recv_buf));
memset(recv_buf,0,1024);
}
close(com_fd);
return 0;
}
}
return 0;
}
客户端
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[]){
int connect_fd;
int ret;
int port;
int len;
int result;
static char snd_buf[1024];
static struct sockaddr_in srv_addr;
typedef struct {
char s[10];
float a;
float b;
char m[1024];
}Message;
//客户端运行需要给出具体的链接地址和端口
if(argc!=3){
printf("Usage:%s server_ip_address port\n",argv[0]);
return 1;
}
port=atoi(argv[2]);
connect_fd=socket(PF_INET,SOCK_STREAM,0);
if(connect_fd<0){
perror("cannot create communication socket");
return 1;
}
bzero(&srv_addr,sizeof(srv_addr));
srv_addr.sin_family=AF_INET;
srv_addr.sin_addr.s_addr=inet_addr(argv[1]);
srv_addr.sin_port=htons(port);
//连接指定的服务器
ret=connect(connect_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));
if(ret==-1){
perror("cannot connect to the server");
close(connect_fd);
return 1;
}
int size = 1024;
//用户输入信息后,程序将输入的信息通过套接字发送给服务器
//然后调用read函数从服务器中读取发送来的消息
//当输入exit时,程序退出
while(1){
Message mes;
int n=sizeof(mes);
printf("请输入需要的操作:");
scanf("%s",mes.s);
if(strcmp(mes.s,"echo")==0)
fgets(mes.m,1024,stdin);
else
scanf("%f%f",&mes.a,&mes.b);
write(connect_fd,&mes,n);
len = read(connect_fd,snd_buf,1024);
if(len>0)
printf("服务器返回:%s\n",snd_buf);
memset(snd_buf,0,1024);
}
close(connect_fd);
return 0;
}
程序运行:
#./server 5000
#./client 192.168.1.1245000
IP写你自己服务器的IP
你想让服务器监听哪个端口,后面就跟哪个端口