• UDP-客户端简单快捷模板


    /* client.c */
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include "error.h"
    #define MAXLINE 80
    #define SERV_PORT 8000
    int main(int argc, char *argv[])
    {
     struct sockaddr_in servaddr;
     int sockfd, n;
     char buf[MAXLINE];
     char str[INET_ADDRSTRLEN];
     socklen_t servaddr_len;
     //socket
     sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
     //remote addr
     bzero(&servaddr, sizeof(servaddr));
     servaddr.sin_family = AF_INET;
     inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
     servaddr.sin_port = htons(SERV_PORT);

     while (fgets(buf, MAXLINE, stdin) != NULL)
     {
      //write(sockfd,buf,strlen(buf));
      n = sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)&servaddr,sizeof(servaddr));
      if (n == -1)
       perr_exit("sendto error");
      //
      n = recvfrom(sockfd, buf, MAXLINE, 0, NULL, 0);
      if (n == -1)
       perr_exit("recvfrom error");
      Write(STDOUT_FILENO, buf, n);
     }
     Close(sockfd);
     return 0;
    }
    //write(sockfd,buf,count)
    //send(sockfd,buf,count,flag)
    //ssize_t send(int sockfd, const void *buf, size_t
     //nbytes, int flags);


    //sendto(sockfd,buf,count,flag,struct
    /*ssize_t sendto(int sockfd, const void *buf, size_t
     nbytes, int flags,
                   const struct sockaddr *destaddr,
     socklen_t destlen);
    */
    /*
    read(sockfd,buf,count)
    recv(sockfd,buf,count,flags)
    recvfrom()


    */

  • 相关阅读:
    problems_jenkins
    JAVA异常处理之finally中最好不要使用return
    IntelliJ idea学习资源
    Mybatis 的分页条件查询语句编写
    maven相关的学习资料
    git相关的学习资料
    开端三(1)
    开端2(2)
    C# 排序小测试
    想为一个类定义[][]方法
  • 原文地址:https://www.cnblogs.com/chunlinge/p/3385774.html
Copyright © 2020-2023  润新知