C语言 Socket Demo

服务端 recv.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define LOCAL_IP "127.0.0.1"
#define BIND_PORT 50000
#define MSG_LEN (15 * 1024 * 1024)
#define RET_OK 0
#define RET_ERR -1

int main()
{
    int sockFd; // Socket Fd
    int workFd; // 发送消息用的Fd
    char* msgBuf = NULL; // 消息发送缓冲区
    struct sockaddr_in addr; // 地址信息
    int recvLen = 0; // 单次接收的消息长度
    int recvedLen = 0; // 累计接收的消息总长度
    int ret;

    // 创建Socket
    sockFd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sockFd == RET_ERR) {
        printf("[Recv]Socket Create failed\n");
        return RET_ERR;
    }

    // 绑定IP和端口
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET; //使用IPv4地址
    addr.sin_addr.s_addr = inet_addr(LOCAL_IP); //具体的IP地址
    addr.sin_port = htons(BIND_PORT); //端口
    ret = bind(sockFd, (struct sockaddr*)&addr, sizeof(addr));
    if (ret != RET_OK) {
        printf("[Recv]Socket bind failed, sockFd:%d, ip:%s, port:%d\n", sockFd, LOCAL_IP, BIND_PORT + 1);
        close(sockFd);
        return RET_ERR;
    }

    // 进入监听状态,等待客户端发起请求
    ret = listen(sockFd, 5);
    if (ret != RET_OK) {
        printf("[Recv]Socket listen failed, sockFd:%d, ip:%s, port:%d\n", sockFd, LOCAL_IP, BIND_PORT);
        close(sockFd);
        return RET_ERR;
    }

    // 接受客户端端请求
    workFd = accept(sockFd, NULL, NULL);
    if (workFd == RET_ERR) {
        printf("[Recv]Socket accept failed, sockFd:%d\n", sockFd);
        close(sockFd);
        return RET_ERR;
    }

    // 接收客户端传回的数据
    msgBuf = malloc(MSG_LEN);
    if (msgBuf == NULL) {
        printf("[Recv]Malloc failed\n");
        close(workFd);
        close(sockFd);
        return RET_ERR;
    }
    memset(msgBuf, 0, MSG_LEN);

    while (1) {
        recvLen = recv(workFd, msgBuf, MSG_LEN, 0);
        if (recvLen <= 0) {
            if (recvLen == RET_ERR) {
                printf("[Recv]Socket recv failed, sockFd:%d, workFd:%d, errno:%d\n", sockFd, workFd, errno);
                free(msgBuf);
                close(workFd);
                close(sockFd);
                return RET_ERR;
            }
            break;
        }
        recvedLen += recvLen;
        printf("[Recv]sockFd:%d, Msg: \"%s\" Len: %d\n", sockFd, msgBuf, recvLen);
    }
    printf("[Recv]total recv: %d, expected recv: %d\n", recvedLen, 10 * 1024 * 1024);

    // 关闭Fd
    ret = close(workFd);
    if (ret != RET_OK) {
        printf("[Recv]Socket close workFd failed, sockFd:%d\n", sockFd);
        free(msgBuf);
        return RET_ERR;
    }
    ret = close(sockFd);
    if (ret != RET_OK) {
        printf("[Recv]Socket close failed, sockFd:%d\n", sockFd);
        free(msgBuf);
        return RET_ERR;
    }

    free(msgBuf);
    return RET_OK;
}

客户端 send.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define LOCAL_IP "127.0.0.1"
#define CONN_PORT 50000
#define MSG_LEN (10 * 1024 * 1024)
#define MSG "Msg Content"
#define RET_OK 0
#define RET_ERR -1

int main()
{
    int sockFd; // Socket Fd
    char* msgBuf = NULL; // 消息接收缓冲区
    int sendLen; // 发送消息长度
    int ret;

    // 创建Socket
    sockFd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockFd == RET_ERR) {
        printf("[Send]Socket Create failed\n");
        return RET_ERR;
    }

    // 绑定IP和端口(该步可以省略)
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET; // 使用IPv4地址
    addr.sin_addr.s_addr = inet_addr(LOCAL_IP); // 具体的IP地址
    addr.sin_port = htons(CONN_PORT + 1); // 端口
    ret = bind(sockFd, (struct sockaddr*)&addr, sizeof(addr));
    if (ret != RET_OK) {
        printf("[Send]Socket bind failed, sockFd:%d, ip:%s, port:%d\n", sockFd, LOCAL_IP, CONN_PORT + 1);
        close(sockFd);
        return RET_ERR;
    }

    // 向服务端发起连接请求
    addr.sin_port = htons(CONN_PORT);
    ret = connect(sockFd, (struct sockaddr*)&addr, sizeof(addr));
    if (ret != RET_OK) {
        printf("[Send]Socket connect failed, sockFd:%d, ip:%s, port:%d\n", sockFd, LOCAL_IP, CONN_PORT);
        close(sockFd);
        return RET_ERR;
    }

    // 向服务端发送数据
    msgBuf = malloc(MSG_LEN);
    if (msgBuf == NULL) {
        printf("[Send]Malloc failed\n");
        close(sockFd);
        return RET_ERR;
    }
    memset(msgBuf, 0, MSG_LEN);

    strcpy(msgBuf, MSG);
    sendLen = send(sockFd, msgBuf, MSG_LEN, 0);
    if (sendLen == RET_ERR) {
        printf("[Send]Socket send failed, sockFd:%d, errno:%d\n", sockFd, errno);
        free(msgBuf);
        close(sockFd);
        return RET_ERR;
    }
    printf("[Send]sockFd:%d, Msg: \"%s\", Len: %d\n", sockFd, msgBuf, sendLen);

    // 关闭Fd
    ret = close(sockFd);
    if (ret != RET_OK) {
        printf("[Send]Socket close sockFd failed, sockFd:%d\n", sockFd);
        free(msgBuf);
        return RET_ERR;
    }

    free(msgBuf);
    return RET_OK;
}

你可能感兴趣的:(c,c语言,linux,网络)