zoukankan      html  css  js  c++  java
  • linux c socket 案源

    service结束
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <sys/shm.h>
    
    #define MYPORT  8887
    #define QUEUE   20
    #define BUFFER_SIZE 1024
    
    int main()
    {
        ///定义sockfd
        int server_sockfd = socket(AF_INET,SOCK_STREAM, 0);
    
        ///定义sockaddr_in
        struct sockaddr_in server_sockaddr;
        server_sockaddr.sin_family = AF_INET;
        server_sockaddr.sin_port = htons(MYPORT);
        server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
        ///bind,成功返回0。出错返回-1
        if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1)
        {
            perror("bind");
            exit(1);
        }
    
        ///listen。成功返回0,出错返回-1
        if(listen(server_sockfd,QUEUE) == -1)
        {
            perror("listen");
            exit(1);
        }
    
        ///客户端套接字
        char buffer[BUFFER_SIZE];
        struct sockaddr_in client_addr;
        socklen_t length = sizeof(client_addr);
    
        ///成功返回非负描写叙述字,出错返回-1
        int conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length);
        if(conn<0)
        {
            perror("connect");
            exit(1);
        }
    
        while(1)
        {
            memset(buffer,0,sizeof(buffer));
            int len = recv(conn, buffer, sizeof(buffer),0);
            if(strcmp(buffer,"exit
    ")==0)
                break;
            fputs(buffer, stdout);
            send(conn, buffer, len, 0);
        }
        close(conn);
        close(server_sockfd);
        return 0;
    }
    
    client端
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <string.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <sys/shm.h>
    
    
    #define MYPORT  8887
    #define BUFFER_SIZE 1024
    
    
    int main()
    {
        ///定义sockfd
        int sock_cli = socket(AF_INET,SOCK_STREAM, 0);
    
    
        ///定义sockaddr_in
        struct sockaddr_in servaddr;
        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(MYPORT);  ///server端口
        servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");  ///serverip
    
    
        ///连接server,成功返回0。错误返回-1
        if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
        {
            perror("connect");
            exit(1);
        }
    
    
        char sendbuf[BUFFER_SIZE];
        char recvbuf[BUFFER_SIZE];
        while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
        {
            send(sock_cli, sendbuf, strlen(sendbuf),0); ///发送
            if(strcmp(sendbuf,"exit
    ")==0)
                break;
            recv(sock_cli, recvbuf, sizeof(recvbuf),0); ///接收
            fputs(recvbuf, stdout);
    
    
            memset(sendbuf, 0, sizeof(sendbuf));
            memset(recvbuf, 0, sizeof(recvbuf));
        }
    
    
        close(sock_cli);
        return 0;
    }
    


    測试:





  • 相关阅读:
    冒泡排序改
    冒泡排序 (基本类型),(引用类型)
    关于随机数列,对给定数目的自0开始步长为1的数字序列进行乱序。(可用作洗牌)
    Java基础之 移位操作
    保留代码,狼羊过河的问题(暂未理解)
    非计算机专业学习计算机
    CompletableFuture 捕获异常方式:handle、whenComplete、exceptionally
    nio socket聊天室
    java socket server接收并返回数据
    java socket套接字编程入门
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5051350.html
Copyright © 2011-2022 走看看