Clang实现socket

结构体说明

// sockaddr
typedef unsigned short int sa_family_t;
struct sockaddr {
  sa_family_t sa_family;
  char sa_data[14];
};
// sockaddr_in
typedef uint16_t in_port_t;
typedef uint32_t in_addr_t;
struct in_addr{
  in_addr_t s_addr;
};
struct sockaddr_in {
  sa_family_t sin_family;
  in_port_t sin_port;
  struct in_addr sin_addr;
  
  unsigned char sin_zero[sizeof (struct sockaddr) -
    (sizeof (unsigned short int)) -
    sizeof (in_port_t) -
    sizeof (struct in_addr)];
};

服务端

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#define SERVER_PORT 8088

int main() {
    // 文件描述符
    int fd_server, fd_client;
    // 填充地址结构体
    // AF_INET:Address Families,表示使用TCP/IP(IPv4)协议簇
    // htonl/htons:用于将主机字节序转化为网络字节序
    // INADDR_ANY:表示所有地址(0.0.0.0)
    struct sockaddr_in addr_server;
    addr_server.sin_family = AF_INET;
    addr_server.sin_addr.s_addr = htonl(INADDR_ANY);
    addr_server.sin_port = htons(SERVER_PORT);
    // client addr
    struct sockaddr_in addr_client;
    int addr_client_len = sizeof(addr_client);
    // buffer
    char buf[1024];
    int recv_num;

    // 创建socket
    // SOCK_STREAM:表示使用TCP,UDP使用SOCK_DGRAM
    fd_server = socket(AF_INET, SOCK_STREAM, 0);
    if (fd_server < 0) {
        printf("Create error.\n");
        exit(-1);
    }
    // 端口绑定
    if (bind(fd_server, (struct sockaddr *) &addr_server, sizeof(addr_server)) < 0) {
        printf("Bind error.\n");
        exit(-1);
    }
    // 端口监听
    if (listen(fd_server, 5) < 0) {
        printf("Listen error.\n");
        exit(-1);
    }
    while (1) {
        printf("Listening port: %d\n", SERVER_PORT);
        // 等待客户端连接
        fd_client = accept(fd_server, (struct sockaddr *) &addr_client,
                           (socklen_t * ) & addr_client_len);
        if (fd_client < 0) {
            printf("Accept error.\n");
            exit(-1);
        }
        // 连接成功
        char *ip = inet_ntoa(addr_client.sin_addr);
        int port = htons(addr_client.sin_port);
        printf("Connected, client ip/port: %s/%d.\n", ip, port);
        while (1) {
            // 接收客户端消息
            recv_num = recv(fd_client, buf, 1024, 0);
            buf[recv_num] = '\0';
            printf("Recv from client: %s\n", buf);
            if (strcmp(buf, "exit") == 0) {
                break;
            }
            // 发送到客户端
            printf("Send to client:");
            scanf("%s", buf);
            printf("\n");
            send(fd_client, buf, strlen(buf), 0);
            if (strcmp(buf, "exit") == 0) {
                break;
            }
        }
    }

    close(fd_server);
    return 0;
}

客户端

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#define SERVER_PORT 8088

int main() {
    // FD
    int fd_client;
    // server addr
    struct sockaddr_in addr_server;
    addr_server.sin_family = AF_INET;
    addr_server.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr_server.sin_port = htons(SERVER_PORT);
    // buffer
    char buf_send[1024];
    char buf_recv[1024];
    // 接收到数据尺寸
    int recv_num;

    // 创建socket
    fd_client = socket(AF_INET, SOCK_STREAM, 0);
    if (fd_client < 0) {
        printf("Create error.\n");
        exit(-1);
    }
    // 连接服务器
    if (connect(fd_client, (struct sockaddr *) &addr_server, sizeof(addr_server)) < 0) {
        printf("Connect error.\n");
        exit(-1);
    }
    printf("Connect success.\n");
    while (1) {
        // 发送到服务端
        printf("Send to server:");
        scanf("%s", buf_send);
        printf("\n");
        send(fd_client, buf_send, strlen(buf_send), 0);
        if (strcmp(buf_send, "exit") == 0) {
            break;
        }
        // 接收服务器消息
        recv_num = recv(fd_client, buf_recv, 1024, 0);
        buf_recv[recv_num] = '\0';
        printf("Recv from server: %s\n", buf_recv);
    }

    close(fd_client);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值