zoukankan      html  css  js  c++  java
  • Socket相关函数(2)- sendto(), recvfrom() UDP模型

    udp_server.c

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <arpa/inet.h>
    #include <ctype.h>
    #include <string.h>
    
    #define SERV_PORT 49130
    #define MAXLINE 80
    
    
    int main()
    {
        int sockfd;
        struct sockaddr_in servaddr, cliaddr;    
        char buf[MAXLINE];
        char str[INET_ADDRSTRLEN];
        int cliaddr_len;
        int n, i;    
    
        if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
            perror("socket");
            exit(1);        
        }
    
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(SERV_PORT); // 一定要使用htons函数将端口转换为大端格式
        
        if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
            perror("bind");
            exit(1);
        }
    
        printf("Accepting connections...
    ");
    
        while (1) {
            cliaddr_len = sizeof(cliaddr);
            n = recvfrom(sockfd, buf, MAXLINE, 0, (struct sockaddr *)&cliaddr, &cliaddr_len);
            if (n == -1) {
                perror("recvfrom err");
            }
            // print source address's ip,port
            printf("Received from %s at PORT %d, recvlen is: %d
    ", inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port), n);
    
            for (i = 0; i < n; i++)
                buf[i] = toupper(buf[i]);    
            n = sendto(sockfd, buf, n, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
            // printf("send length is: %d
    ", n);
            if (n == -1) {
                perror("sendto err");
            }
        }
    
        close(sockfd);
        return 0;
    }

    udp_client.c

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    
    #define MAXLINE 80
    #define SERV_PORT 49130
    
    static const char *SERV_ADDR = "127.0.0.1";
    
    int main()
    {
        
        int sockfd;
        char buf[MAXLINE];
        struct sockaddr_in servaddr;
        int n, i;    
    
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        inet_pton(AF_INET, SERV_ADDR, &servaddr.sin_addr);    
        servaddr.sin_port = htons(SERV_PORT);    
    
        if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
            perror("socket");
            exit(1);
        }
    
        while (fgets(buf, MAXLINE, stdin) != NULL) {
            n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
            if (n == -1) {
                perror("sendto err");
            }
            
            n = recvfrom(sockfd, buf, MAXLINE, 0, NULL, 0);
            if (n == -1) {
                perror("recvfrom err");
            }
            
            //printf("recv len is: %d
    ", n);
            write(STDOUT_FILENO, buf, n);
        }
    
        close(sockfd);
        return 0;
    }

    先运行Server, 再运行Client,在client输入:

    yongdaimi@ubuntu:~/Documents/code$ gcc udp_client.c -o client
    yongdaimi@ubuntu:~/Documents/code$ ./client
    java
    JAVA
    j2sdk
    J2SDK
    jdbc
    JDBC
    cpp
    CPP
    jni
    JNI

    Server:

    Accepting connections...
    Received from 127.0.0.1 at PORT 45162, recvlen is: 3
    Received from 127.0.0.1 at PORT 51366, recvlen is: 3
    Received from 127.0.0.1 at PORT 51366, recvlen is: 5
    Received from 127.0.0.1 at PORT 51366, recvlen is: 5
    Received from 127.0.0.1 at PORT 57741, recvlen is: 5
    Received from 127.0.0.1 at PORT 51904, recvlen is: 5
    Received from 127.0.0.1 at PORT 51904, recvlen is: 6
    Received from 127.0.0.1 at PORT 51904, recvlen is: 5
    Received from 127.0.0.1 at PORT 51904, recvlen is: 4
    Received from 127.0.0.1 at PORT 51904, recvlen is: 4

  • 相关阅读:
    Android笔记之调用系统相机拍照
    Android笔记之RoundedImageView
    Java笔记之public、protected、default和private
    Android笔记之ExpandableListView(悬浮吸顶Demo)
    Android笔记之Fragment中创建ViewModel的正确方式
    Android代号、版本及API级别之间的对应关系
    【Phabricator】教科书一般的Phabricator安装教程(配合官方文档并带有踩坑解决方案)
    【Ansible】记一次技术博客害死人的经历——ansible模板变量注入探究
    【linux杂谈】遇到REMOTE HOST IDENTIFICATION HAS CHANGED怎么办?
    【linux杂谈】在SSH连接中,openssh如何解决'Connection refused'错误?
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/8295372.html
Copyright © 2011-2022 走看看