• 【网络编程】——linux socket demo


    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    #if 0
    #define UDP
    #else
    #define TCP
    #endif
    
    int sockfd;
    char* IP = "10.8.2.60";
    //char *IP = "127.0.0.1";
    #ifdef UDP
    short PORT = 1025;
    #endif
    #ifdef TCP
    short PORT = 2222;
    #endif
    typedef struct sockaddr SA;
    
    void init(){
    #ifdef UDP
        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    #endif
    
    #ifdef TCP
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(PORT);
        addr.sin_addr.s_addr = inet_addr(IP);
        if(connect(sockfd, (SA*)&addr, sizeof(addr)) == -1){
            printf("connect failed!
    ");
            exit(-1);
        }
    #endif
    }
    
    int main (int argc, char *argv[]) {
        //char buf[2048];
        char buf[512];
        int index, i = 0;
    
        if (argc == 2) {
            index = atoi(argv[1]);
        } else {
            printf("Usage: ./client <times>
    ");
            return -1; 
        } 
    
        printf("start init ....
    ");
        init();
        printf("connected...
    ");
        memset(buf, 1, sizeof(buf));
    #ifdef UDP
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(PORT);
        addr.sin_addr.s_addr = inet_addr(IP);
    #endif
    
        while (1) {
            //printf("please input something:
    ");
            //scanf("%s", buf);
            //puts(buf);
            if (i == index)
                break;
    #ifdef TCP
            send(sockfd, buf, sizeof(buf), 0);
            printf("send 2048! index:%d
    ", i);
    #endif
    #ifdef UDP
            printf("sendto 2048! index:%d
    ", i);
            if (sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1){
                printf("sendto error!
    ");
                return -1;
            }
    #endif
            //sleep(1);
            i++;
        }
    
        close(sockfd);
    
        return 0;
    }

    client。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    int sockfd;
    char* IP = "10.8.2.56";
    //char *IP = "127.0.0.1";
    typedef struct sockaddr SA;
    
    #if 0
    #define UDP
    #else
    #define TCP
    #endif
    #ifdef UDP
    short PORT = 1025;
    #else
    short PORT = 2222;
    #endif
    
    void init(){
    #ifdef TCP
        sockfd = socket(AF_INET, SOCK_STREAM, 0);    //tcp
    #endif
    #ifdef UDP
        sockfd = socket(AF_INET, SOCK_DGRAM, 0);    //udp
    #endif
        if(sockfd == -1){
            printf("socket failed!
    ");
            exit(-1);
        }
    
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(PORT);
        addr.sin_addr.s_addr = inet_addr(IP);
        if(bind(sockfd, (SA *)&addr, sizeof(addr)) == -1) {
            printf("bind failed!
    ");
            exit(-1);
        }
    #ifdef TCP
        if(listen(sockfd, 10) == -1) {
            printf("listen failed!
    ");
            exit(-1);
        }
    #endif
    }
    
    int main (int argc, char *argv[]) {
        int fd;
        int ret;
        char buf[512];
        int index = 0;
        init();
    
        while (1) {
    #ifdef TCP
            struct sockaddr_in fromaddr;
            socklen_t len = sizeof(fromaddr);
            fd = accept(sockfd, (SA *)&fromaddr, &len);
            if (fd < 0) {
                printf("accept failed!
    ");
                continue;
            }
            printf("fd:%d
    ", fd);
    #endif
            memset(buf, 1, sizeof(buf));
            while (1) {
    #ifdef TCP
                if ((ret = recv(fd, buf, sizeof(buf), MSG_WAITALL)) > 0) {
    #endif
    #ifdef UDP
                printf("udp!
    ");
                if ((ret = recvfrom(sockfd, buf, sizeof(buf), MSG_WAITALL, NULL, NULL)) > 0) {
    #endif
                    printf("ret %d
    ", ret);
                } else {
                    printf("recvfrom failed! ret:%d
    ", ret);
                    break;
                }
                printf("index:%d
    ", index);
                index++;
            }
        }
    
        return 0;
    }

    server。

  • 相关阅读:
    C语言面试题分类->宏定义
    c语言位运算
    C语言一个程序的存储空间
    收藏的链接-English
    侧滑关闭Activity的解决方案——SwipeBackLayout
    实现ViewPager的联动效果
    由Toolbar造成的ListView最后一项显示不全
    收藏的链接-Stub
    收藏的链接-Git
    收藏的链接
  • 原文地址:https://www.cnblogs.com/ngnetboy/p/5594971.html
Copyright © 2020-2023  润新知