首页 > 代码库 > Linux服务器、客户端

Linux服务器、客户端

#include <stdio.h>         //标准输入输出头文件#include <sys/socket.h>    //与套接字相关的函数声明和结构定义#include <netinet/in.h>    //某些结构体声明、宏定义#include <arpa/inet.h>    //某些函数声明#include <errno.h>      //查看错误代码/*以上为包含必要的头文件,其中几个头文件是几乎每一个网络程序所必需的,如<sys/socket.h>,<netinet/in.h>等*/int main(int argc,char *argv[]){ printf("=======================================\n"); printf("=       My first server program       =\n"); printf("=======================================\n"); int sockfd,new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size,portnumber; char hello[]="Hello, The Network! This is my first Server.\n"; if (argc!=2){   fprintf(stderr,"Usage:%s  portnumber \a\n",argv[0]);   exit(1);  //程序出现错误结束 }if ((portnumber=atoi(argv[1]))<0){   fprintf(stderr,"Usage:%s  portnumber \a\n",argv[0]);   exit(1); }   //atoi()将字符型转化为整型 //建立socket描述符(intnet协议簇,流套接字,使用默认协议) if ((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){   fprintf(stderr,"Socket error:%s \a\n",strerror(errno));   exit(1); }//填充服务器的套接字地址结构sockaddr bzero(&server_addr,sizeof (struct sockaddr_in));  server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY);  server_addr.sin_port=htons(portnumber); //开始捆绑sockfd描述符 if (bind(sockfd,(struct sockaddr *)(&server_addr),sizeof (struct sockaddr))==-1){   fprintf(stderr,"Bind error:%s \a\n",strerror(errno));   exit(1); } //监听sockfd描述符if (listen(sockfd,5)==-1){   //fprintf(stderr,"Listen error:%s \a\n",strerror(errno));   exit(1); }  while(1) {//阻塞服务器,直到客户程序建立连接   sin_size=sizeof (struct sockaddr_in);   if ((new_fd=accept(sockfd,(struct sockaddr*)(&client_addr),&sin_size))==-1){     //fprintf(stderr,"Accept error:%s \a\n",strerror(errno));     exit(1);   }     fprintf(stderr,"Server get connection from %s :\n",inet_ntoa(client_addr.sin_addr));     if (write(new_fd,hello,strlen(hello))==-1){       //fprintf(stderr,"Write error:%s \a\n",strerror(errno));       exit(1);         }     //通信结束,继续循环执行     close(new_fd);/*当服务器处理完这个客户机请求后,关闭连接套接字*/ }//关闭监听套接字描述符 close(sockfd); exit(0);   }
#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <errno.h>#include <netdb.h>     /*包括结构hostent(主机环境),获得主机信息的几个函数*/int main(int argc,char *argv[]){ printf("=======================================\n"); printf("=       My first client program!      =\n"); printf("=======================================\n"); int sockfd,new_fd; struct sockaddr_in server_addr; struct hostent *host; int nbytes,portnumber; char buffer[1024]; if (argc!=3){   fprintf(stderr,"Usage:%s hostname portnumber \a\n",argv[0]);   exit(1); }//获得服务器地址 if ((host=gethostbyname(argv[1]))==NULL){   fprintf(stderr,"HostName Erro!");   exit(1); } if ((portnumber=atoi(argv[2]))<0){   fprintf(stderr,"Usage:%s hostname  portnumber \a\n",argv[0]);   exit(1); } //建立客户端Sockfd描述符 if ((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){   fprintf(stderr,"Socket error:%s \a\n",strerror(errno));   exit(1); }//调用函数connect之前,需要指定服务器进程的套接字地址//填充sockaddr结构 bzero(&server_addr,sizeof (struct sockaddr_in)); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(portnumber);// server_addr.sin_addr= *((struct in_addr *) host->h_addr);//发起连接请求与远程服务器建立一个连接 if (connect(sockfd,(struct sockaddr *)(&server_addr),sizeof (struct sockaddr))==-1){   fprintf(stderr,"Bind error:%s \a\n",strerror(errno));   exit(1); } //进行读写数据操作,将数据显示到标准输出上 if ((nbytes=read(sockfd,buffer,1024))==-1){   fprintf(stderr,"Read error: %s  \n",strerror(errno));   exit(1); }  buffer[nbytes]=\\;   printf("收到: %s \n",buffer);//结束通信//关闭连接 close(sockfd); exit(0);   }

 

Linux服务器、客户端