第8章 基本UDP套接字编程

客户端:

#include "../Gnet.h"

void do_client(int udpfd, struct sockaddr* pserver_addr, socklen_t server_addr_len)
{
    char buf[MAX_LINE];
    int nread;

    while(fgets(buf, MAX_LINE, stdin) != NULL)
    {
        sendto(udpfd, buf, strlen(buf), 0, pserver_addr, server_addr_len);
        nread = recvfrom(udpfd, buf, MAX_LINE, 0, NULL, NULL);
        fputs(buf, stdout);
    }
}

int main(int argc, const char* argv[])
{
    int udpfd;
    struct sockaddr_in server_addr;

    if(argc < 2)
        perr_exit("usage : client <IPaddress>");

    udpfd = Socket(AF_INET, SOCK_DGRAM, 0);
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVER_PORT);
    inet_pton(AF_INET, argv[1], &server_addr.sin_addr);

    do_client(udpfd, (struct sockaddr*)&server_addr, sizeof(server_addr));

    return 0;
}
#include "../Gnet.h"

void do_client(int udpfd, struct sockaddr* pserver_addr, socklen_t server_addr_len)
{
    char buf[MAX_LINE];
    char reply_ip[INET_ADDRSTRLEN];
    int nread;
    struct sockaddr_in reply_addr;
    socklen_t reply_addr_len;

    reply_addr_len = server_addr_len;
    while(fgets(buf, MAX_LINE, stdin) != NULL)
    {
        sendto(udpfd, buf, strlen(buf), 0, pserver_addr, server_addr_len);
        nread = recvfrom(udpfd, buf, MAX_LINE, 0, (struct sockaddr*)&reply_addr, &reply_addr_len);
        if(reply_addr_len != server_addr_len ||
           memcmp(&reply_addr, pserver_addr, reply_addr_len) != 0)
        {
            inet_ntop(AF_INET, &reply_addr, reply_ip, INET_ADDRSTRLEN);
            printf("reply from %s (ignored)\n", reply_ip);
            continue;
        }
        fputs(buf, stdout);
    }
}

int main(int argc, const char* argv[])
{
    int udpfd;
    struct sockaddr_in server_addr;

    if(argc < 2)
        perr_exit("usage : client <IPaddress>");

    udpfd = Socket(AF_INET, SOCK_DGRAM, 0);
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVER_PORT);
    inet_pton(AF_INET, argv[1], &server_addr.sin_addr);

    do_client(udpfd, (struct sockaddr*)&server_addr, sizeof(server_addr));

    return 0;
}
#include "../Gnet.h"

void do_client(int udpfd, struct sockaddr* pserver_addr, socklen_t server_addr_len)
{
    char buf[MAX_LINE];
    int nread;

    Connect(udpfd, pserver_addr, server_addr_len);
    while(fgets(buf, MAX_LINE, stdin) != NULL)
    {
        Write(udpfd, buf, strlen(buf));
        nread = Read(udpfd, buf, MAX_LINE);
        
        fputs(buf, stdout);
    }
}

int main(int argc, const char* argv[])
{
    int udpfd;
    struct sockaddr_in server_addr;

    if(argc < 2)
        perr_exit("usage : client <IPaddress>");

    udpfd = Socket(AF_INET, SOCK_DGRAM, 0);
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVER_PORT);
    inet_pton(AF_INET, argv[1], &server_addr.sin_addr);

    do_client(udpfd, (struct sockaddr*)&server_addr, sizeof(server_addr));

    return 0;
}

服务器:

#include "../Gnet.h"

void do_server(int udpfd)
{
    ssize_t nread;
    char buf[MAX_LINE];
    struct sockaddr_in client_addr;
    socklen_t client_addr_len;

    while(1)
    {
        client_addr_len = sizeof(client_addr);
        nread = recvfrom(udpfd, buf, MAX_LINE, 0, (struct sockaddr*)&client_addr, &client_addr_len);
        sendto(udpfd, buf, nread, 0, (struct sockaddr*)&client_addr, client_addr_len);
    }
}

int main(int argc, const char* argv[])
{
    int udpfd;
    struct sockaddr_in server_addr;

    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(SERVER_PORT);

    udpfd = Socket(AF_INET, SOCK_DGRAM, 0);
    Bind(udpfd, (const struct sockaddr*)&server_addr, sizeof(server_addr));
    printf("waiting for connecting.\n");

    do_server(udpfd);

    return 0;
}

服务器(select模型+tcp+udp):

#include "../Gnet.h"

void do_server(int connfd)
{
    ssize_t nread;
    char buf[MAX_LINE];

    while((nread = Read(connfd, buf, MAX_LINE)) > 0)
        Write(connfd, buf, nread);
}

void sig_child(int signo)
{
    pid_t pid;
    int stat;

    printf("in sig_child.\n");
    while((pid = waitpid(-1,&stat, WNOHANG)) > 0)
        printf("child %d terminated\n", pid);
    printf("out sig_child.\n");
}

int main(int argc, const char* argv[])
{
    int lfd, connfd;
    int udpfd;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_len;
    pid_t child_id;
    const int on = 1;
    fd_set rset;
    int maxfd;
    int nready;
    int nread;
    char buf[MAX_LINE];

    struct sigaction sigaction_set, sigaction_get;
    sigaction_set.sa_handler = sig_child;
    sigemptyset(&sigaction_set.sa_mask);
    sigaction_set.sa_flags = 0;
    if(sigaction(SIGCHLD, &sigaction_set, &sigaction_get) <0)
        printf("sigaction(SIGCHLD) error!\n");

    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(SERVER_PORT);

    lfd = Socket(AF_INET, SOCK_STREAM, 0);
    setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));//端口复用
    Bind(lfd, (const struct sockaddr*)&server_addr, sizeof(server_addr));
    Listen(lfd, LISTENQ);

    udpfd = Socket(AF_INET, SOCK_DGRAM, 0);
    Bind(udpfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
    printf("waiting for connecting.\n");

    maxfd = lfd > udpfd ? lfd : udpfd;
    FD_ZERO(&rset);
    while(1)
    {
        FD_SET(lfd, &rset);
        FD_SET(udpfd, &rset);

        if((nready = select(maxfd+1, &rset, NULL, NULL, NULL)) < 0)
        {
            if(errno == EINTR)
                continue;
            else
                perr_exit("select error");
        }

        if(FD_ISSET(lfd, &rset))
        {
            client_addr_len = sizeof(client_addr);
            connfd = Accept(lfd, (struct sockaddr*)&client_addr, &client_addr_len);
            if((child_id = fork()) == 0)//子进程
            {
                Close(lfd);
                do_server(connfd);
                Close(connfd);
                exit(0);
            }
            else//父进程
            {
                printf("child %d connected\n", child_id);
                Close(connfd);
            }
        }

        if(FD_ISSET(udpfd, &rset))
        {
            client_addr_len = sizeof(client_addr);
            nread = recvfrom(udpfd, buf, MAX_LINE, 0, (struct sockaddr*)&client_addr, &client_addr_len);
            sendto(udpfd, buf, nread, 0, (struct sockaddr*)&client_addr, client_addr_len);
        }
    }

    return 0;
}

connect:绑定对端地址

bind:绑定本端地址 github:https://github.com/gongluck/CodeBase/tree/master/notes/unpv13-notes

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏xingoo, 一个梦想做发明家的程序员

Oracle基础知识-sqlplus使用

Oracle的sql*plus是与oracle进行交互的客户端工具。 在sql*plus中,可以运行sql*plus命令与sql*plus语句。 我们通常所说的...

19910
来自专栏算法修养

POJ-2029 Get Many Persimmon Trees(动态规划)

Get Many Persimmon Trees Time Limit: 1000MS Memory Limit: 30000K Total ...

2618
来自专栏算法修养

Code Forces Bear and Forgotten Tree 3 639B

B. Bear and Forgotten Tree 3 time limit per test2 seconds memory limit per t...

2687
来自专栏我的博客

Ubuntu 12.04 LTS 搭建svn,mysql,apache过程

1.apt-get install subversion libapache2-svn libapache2-mod-auth-mysql apache2 my...

2455
来自专栏数据和云

Oracle 的 DBMS_SCN 修正以及 SCN 的 auto-rollover 新特性

在 Oracle 11.2.0.2 之后,随着一系列 SCN 耗尽问题的出现,很多补丁涌现出来,一个新的 Package 增加进来。 这个 Package 就是...

3606
来自专栏文渊之博

利用PowerShell复制SQLServer账户的所有权限

问题   对于DBA或者其他运维人员来说授权一个账户的相同权限给另一个账户是一个很普通的任务。但是随着服务器、数据库、应用、使用人员地增加就变得很枯燥乏味又耗时...

2098
来自专栏转载gongluck的CSDN博客

第15章 Unix域协议

#include <sys/un.h> struct sockaddr_un { sa_family_t sun_family; /* AF_LOC...

2985
来自专栏杨建荣的学习笔记

生产环境sql语句调优实战第五篇(r2笔记41天)

今天在生产环境中发现一条sql语句尽管走了主键索引,但是查询还是很慢。 sql语句类似下面的形式: SELECT /*+ index (bl1_cyc_paye...

3358
来自专栏乐沙弥的世界

Oracle expdp 时遭遇ORA-39125 ORA-04063

    数据库在使用DataPump导出时碰到了ORA-39125与ORA-04063。完整的ORA-39125提示是Worker unexpected fat...

571
来自专栏FD的专栏

Effective Testing with RSpec 3 (英文版)(序言)

Early praise for Effective Testing with RSpec 3

1374

扫描关注云+社区