Linux网络编程示例

2022/4/30 7:12:42

本文主要是介绍Linux网络编程示例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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;
}


这篇关于Linux网络编程示例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程