当前位置 : 主页 > 编程语言 > 其它开发 >

linux套接字编程示例

来源:互联网 收集:自由互联 发布时间:2022-05-30
简单TCP、UDP编程示例。 linux套接字编程示例 简单TCP、UDP编程示例。 TCPTCP通信模型 服务器代码 #include sys/socket.h#include netinet/in.h#include arpa/inet.h#include errno.h#include stdio.h#include unistd.h#incl
简单TCP、UDP编程示例。 linux套接字编程示例

简单TCP、UDP编程示例。

TCP TCP通信模型

image

服务器代码
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(void)
{
    const char ip[] = "127.0.0.1";
    const int port = 9006;

    //创建socket,使用IP协议(PF_INET)+TCP协议(SOCK_STREAM)
    int fd_listen = socket(PF_INET, SOCK_STREAM, 0);

    //绑定固定ip:port地址
    in_addr_t ip_num = inet_addr(ip);
    sockaddr_in addr_server = {AF_INET, port, ip_num};
    bind(fd_listen, (sockaddr *)&addr_server, sizeof(addr_server));

    //监听socket
    listen(fd_listen, 10);

    //接受客户端连接,并返回连接socket
    sockaddr_in addr_client;
    socklen_t len_client_addr = sizeof(addr_client);
    int fd_conn = accept(fd_listen, (sockaddr *)&addr_client, &len_client_addr);

    //接收客户端数据
    printf("start receiving data...\n");

    char buf[4096];
    while (true)
    {
        memset(buf, '\0', sizeof(buf));
        int ret = recv(fd_conn, buf, sizeof(buf) - 1, 0);

        // recv返回0,表示客户端断开连接
        if (ret == 0)
        {
            printf("connection closed\n");
            break;
        }

        //打印接收的数据
        printf("%s", buf);
    }

    //关闭socket
    close(fd_conn);
    close(fd_listen);
    return 0;
}
客户端代码
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(void)
{
    const char ip[] = "127.0.0.1"; //服务器ip
    const int port = 9006;         //服务器port

    //创建socket,使用IP协议(PF_INET)+TCP协议(SOCK_STREAM)
    int fd_conn = socket(PF_INET, SOCK_STREAM, 0);

    //连接服务器
    in_addr_t ip_num = inet_addr(ip);
    sockaddr_in addr_server = {AF_INET, port, ip_num}; //服务器地址
    connect(fd_conn, (sockaddr *)&addr_server, sizeof(addr_server));

    const char message[] = "hello world!\n";
    for (int i = 0; i < 10; i++)
    {
        send(fd_conn, message, strlen(message), 0);
        sleep(1);
    }

    //关闭socket
    close(fd_conn);
    return 0;
};
UDP UDP通信模型

image

服务器代码
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(void)
{
    const char ip[] = "127.0.0.1";
    const int port = 9006;

    //创建socket,使用IP协议(PF_INET)+SOCK_DGRAM
    int fd_socket = socket(PF_INET, SOCK_DGRAM, 0);

    //绑定固定ip:port地址
    in_addr_t ip_num = inet_addr(ip);
    sockaddr_in addr_server = {AF_INET, port, ip_num};
    bind(fd_socket, (sockaddr *)&addr_server, sizeof(addr_server));

    //接收客户端数据
    printf("start receiving data...\n");

    //存放客户端地址
    sockaddr_in addr_client;
    socklen_t len_addr_client = sizeof(addr_client);

    char buf[4096];
    while (true)
    {
        //接收数据
        memset(buf, '\0', sizeof(buf));
        int ret = recvfrom(fd_socket, buf, sizeof(buf) - 1, 0, (sockaddr *)&addr_client, &len_addr_client);
        char *ip_client = inet_ntoa(addr_client.sin_addr);
        int port_client = addr_client.sin_port;

        //打印接收的数据
        printf("recived from %s:%d: %s\n", ip_client, port_client, buf);
    }

    //关闭socket
    close(fd_socket);
    return 0;
}
客户端代码
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(void)
{
    const char ip[] = "127.0.0.1";
    const int port = 9006;

    //创建socket,使用IP协议(PF_INET)+SOCK_DGRAM
    int fd_socket = socket(PF_INET, SOCK_DGRAM, 0);

    //绑定固定ip:port地址
    in_addr_t ip_num = inet_addr(ip);
    sockaddr_in addr_server = {AF_INET, port, ip_num};
    bind(fd_socket, (sockaddr *)&addr_server, sizeof(addr_server));

    //接收客户端数据
    printf("start receiving data...\n");

    //存放客户端地址
    sockaddr_in addr_client;
    socklen_t len_addr_client = sizeof(addr_client);

    char buf[4096];
    while (true)
    {
        //接收数据
        memset(buf, '\0', sizeof(buf));
        int ret = recvfrom(fd_socket, buf, sizeof(buf) - 1, 0, (sockaddr *)&addr_client, &len_addr_client);
        char *ip_client = inet_ntoa(addr_client.sin_addr);
        int port_client = addr_client.sin_port;

        //打印接收的数据
        printf("recived from %s:%d: %s\n", ip_client, port_client, buf);
    }

    //关闭socket
    close(fd_socket);
    return 0;
}
上一篇:Docker 存储引擎 overlay、overlay2 及 AUFS 简介
下一篇:没有了
网友评论