C语言实现UDP连接的参考代码

C语言实现UDP连接的参考代码,Client连接上Server后将自己所在目录下的"liu"文件中的前三行文字发送到Server端去,然后Server负责接收和显示。

/**************server.c**************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
int port=8888;
int main(){
   int sockfd;
   int len;
   int z;
   char buf[256];
   struct sockaddr_in adr_inet;
   struct sockaddr_in adr_clnt;
   printf("waiting for client...\n");
   adr_inet.sin_family=AF_INET;
   adr_inet.sin_port=htons(port);
   adr_inet.sin_addr.s_addr=htonl(INADDR_ANY);
   bzero(&(adr_inet.sin_zero),8);
   len=sizeof(adr_clnt);
   sockfd=socket(AF_INET,SOCK_DGRAM,0);
   if(sockfd==-1){
     perror("socket error_1");
     exit(1);
   }
   z=bind(sockfd,(struct sockaddr *)&adr_inet,sizeof(adr_inet));
   if(z==-1){
     perror("bind error_1");
     exit(1);
   }
   while(1){
     z=recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&adr_clnt,&len);
     if(z<0){
       perror("recvfrom error_1");
       exit(1);
     }
     buf[z]=0;
     printf("接收:%s",buf);
     if(strncmp(buf,"stop",4)==0){
       printf("结束....\n");
       break;
     }
   }
   close(sockfd);
   exit(0);
}

/***************client.c***************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
int port=8888;

int main(int argc,char *argv[]){
  int sockfd;
  int i=0;
  int z;
  char buf[80],str1[80];
  struct hostent *host;
  struct sockaddr_in adr_srvr;
  if(argc<2){
    fprintf(stderr,"please enter the server's hostname!\n");
    exit(1);
  }
  if((host=gethostbyname(argv[1]))==NULL){
    herror("gethostbyname error!");
    exit(1);
  }
  FILE *fp;
  printf("open file....\n");
  fp=fopen("liu","r");
  if(fp==NULL){
    perror("failed to open file");
    exit(1);
  }
  printf("connecting server....\n");
  adr_srvr.sin_family=AF_INET;
  adr_srvr.sin_port=htons(port);
  adr_srvr.sin_addr=*((struct in_addr *)host->h_addr);
  bzero(&(adr_srvr.sin_zero),8);
  sockfd=socket(AF_INET,SOCK_DGRAM,0);
  if(sockfd==-1){
    perror("socket error!");
    exit(1);
  }
  printf("send file ...\n");
  for(i=0;i<3;i++){
     fgets(str1,80,fp);
     printf("%d:%s",i,str1);
     sprintf(buf,"%d:%s",i,str1);
     z=sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&adr_srvr,sizeof(adr_srvr));
     if(z<0){
       perror("recvfrom error");
       exit(1);
     }
  }
  printf("send ....\n");
  sprintf(buf,"stop\n");
  z=sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&adr_srvr,sizeof(adr_srvr));
  if(z<0){
    perror("sendto error");
    exit(1);
  }
  fclose(fp);
  close(sockfd);
  exit(0);
}

运行截图:

Server端:

Client端:

(————完————)

你可能感兴趣的:(嵌入式相关,linux)