第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 条评论
登录 后参与评论

相关文章

来自专栏芋道源码1024

数据库中间件 MyCAT源码分析——跨库两表Join

1. 概述 2. 主流程 3. ShareJoin 3.1 JoinParser 3.2 ShareJoin.processSQL(...) 3.3 Batc...

6708
来自专栏跟着阿笨一起玩NET

SQL Server 2008使用自定义表类型

本文转载:http://www.cnblogs.com/chenxizhang/archive/2009/04/28/1445234.html

662
来自专栏程序员的SOD蜜

抽象SQL查询:SQL-MAP技术的使用

什么是参数化查询?我们来看百科对此的定义和示例: 一,定义 --------------------------------------------------...

27510
来自专栏大内老A

在Entity Framework中使用存储过程(三):逻辑删除的实现与自增长列值返回

本篇文章通过实例的方式,讨论两个在EF使用存储过程的主题:如何通过实体和存储过程的映射实现逻辑删除;对于具有自增长类型主键的数据表,在进行添加操作的时候如何将正...

1897
来自专栏技术小黑屋

Android 中 SQLite 性能优化

数据库是应用开发中常用的技术,在Android应用中也不例外。Android默认使用了SQLite数据库,在应用程序开发中,我们使用最多的无外乎增删改查。纵使操...

761
来自专栏芋道源码1024

数据库分库分表中间件 Sharding-JDBC 源码分析 —— SQL 改写

本文主要基于 Sharding-JDBC 1.5.0 正式版 1. 概述 2. SQLToken 3.SQL 改写 3.4.1 分页补充 3.1 TableTo...

3506
来自专栏HansBug's Lab

1475: 方格取数

1475: 方格取数 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 578  Solved: 309 [Subm...

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

pl/sql中的参数模式(r4笔记第54天)

在平时的工作中,可能通过pl/sql传入参数来做一些特定的操作,参数模式一般有In,out.in out这几种 比如dbms_sqltune下的PREPARE_...

3004
来自专栏飞扬的花生

qrCode生成二维码图片

QRCode.js 是一个用于生成二维码图片的插件。 1.文件脚本 var QRCode;!function(){function a(a){this.mode...

47211
来自专栏向治洪

Android ormLite复杂条件查询

OrmLite要继承一个OrmLiteSqliteOpenHelper,通过OrmLiteSqliteOpenHelper实例的getDao方法可以获取一个Da...

2008

扫码关注云+社区