文档章节

TCP/UDP 客户端 服务端 简单测试程序

老汉-憨憨
 老汉-憨憨
发布于 2017/07/21 11:39
字数 1284
阅读 32
收藏 0

tcpsvr:

#include <stdio.h>  
#include <unistd.h>  
#include <errno.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <stdlib.h>  
#include <arpa/inet.h>  
#include <netinet/in.h>  

#define LY_ERR printf
void accept_handle(int connfd, struct sockaddr_in peeraddr);  

int main(int argc, const char *argv[])  
{  
    int        sockfd, connfd;  
    struct sockaddr_in  seraddr, peeraddr;  
    socklen_t  addrlen;  

    if (argc != 2) {  
        printf("Usage: %s <port>\n", argv[0]);  
        return -1;  
    }  

    sockfd = socket(AF_INET, SOCK_STREAM, 0);  
    if (sockfd < 0) {  
        LY_ERR("socket: %s\n", strerror(errno));  
        return -1;  
    }  
   
    setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (int[]){1}, sizeof(int));


    seraddr.sin_family = AF_INET;  
    seraddr.sin_port = ntohs(atoi(argv[1]));  
    seraddr.sin_addr.s_addr = htonl(INADDR_ANY);  

    if (bind(sockfd, (struct sockaddr *)&seraddr, sizeof(struct sockaddr))) {  
        LY_ERR("bind: %s\n", strerror(errno));  
        return -1;  
    }  
   
    #define MAX_CONN (10)
    if (listen(sockfd, MAX_CONN) < 0) {  
        LY_ERR("listen: %s\n", strerror(errno));  
        return -1;  
    }  

    addrlen = sizeof(struct sockaddr);  
    for (;;) {  
        connfd = accept(sockfd, (struct sockaddr *)(&peeraddr), &addrlen);  
        if (connfd < 0) {  
            LY_ERR("accept: %s\n", strerror(errno));  
            continue;  
        }  
        accept_handle(connfd, peeraddr);  
    }  

    return 0;  
}  

void accept_handle(int connfd, struct sockaddr_in peeraddr)  
{  
    char   buf1[32] = "Hello, welcome to server!";  
    char   buf[32]; 
    printf("Receive request from %s, port %d\n",  
            inet_ntop(AF_INET, &peeraddr.sin_addr, buf, sizeof(buf)),  
            ntohs(peeraddr.sin_port));  

    if (send(connfd, buf1, sizeof(buf1), 0) < 0) {  
        close(connfd);  
        return;  
    }  

    close(connfd);  
}  

tcpcli:

/* 
 * tcpclient.c - A simple TCP client
 * usage: tcpclient <host> <port>
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <errno.h>
#include <poll.h>

#define BUFSIZE 1024

/* 
 * error - wrapper for perror
 */
void error(char *msg) {
    perror(msg);
    exit(0);
}

int main(int argc, char **argv) 
{
    int sockfd, portno, n;
    struct sockaddr_in serveraddr, cli;
    struct hostent *server;
    char *hostname;
    char buf[BUFSIZE];

    /* check command line arguments */
    if (argc != 3) {
        fprintf(stderr,"usage: %s <hostname> <port>\n", argv[0]);
        exit(0);
    }

    hostname = argv[1];
    portno = atoi(argv[2]);

    /* socket: create the socket */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");

    int enable = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
        error("setsockopt(SO_REUSEADDR) failed");

#if 1 
    /* 客户端也是可以绑定某个端口的 */
    bzero((char *)&cli, sizeof(cli));
    cli.sin_family = AF_INET;  
    cli.sin_port = ntohs(3307);  
    cli.sin_addr.s_addr = htonl(INADDR_ANY);  

    if (bind(sockfd, (struct sockaddr *)&cli, sizeof(struct sockaddr))) {   
        printf("bind: %s\n", strerror(errno));  
        return -1;  
    } 
#endif

    server = gethostbyname(hostname);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host as %s\n", hostname);
        exit(0);
    }

    /* build the server's Internet address */
    bzero((char *) &serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
            (char *)&serveraddr.sin_addr.s_addr, server->h_length);
    serveraddr.sin_port = htons(portno);


    /* connect: create a connection with the server */
    if (connect(sockfd, (const sockaddr*)&serveraddr, sizeof(serveraddr)) < 0)  {
        printf("errno = %d\n", errno);
        error("ERROR connecting");
    }
    printf("conn success!");

    /* print the server's reply */
    bzero(buf, BUFSIZE);
    n = read(sockfd, buf, 4);
    if (n < 0) { 
        error("ERROR reading from socket");
    }

    n = read(sockfd, buf+4, BUFSIZE-4);
    if (n < 0) { 
        error("ERROR reading from socket");
    }
    printf("Echo from server: %s, %d\n", buf, n);

    close(sockfd);
    return 0;
}

TCP Client 这里调用了两次 read 是为了和 udp 的做一个对比。TCP 这里,server发一次, client read 两次会正常返回, 程序正常结束(前提是 svr 发送的数据长度 >4).

udpsvr:

/* 
 *  * udpserver.c - A simple UDP echo server 
 *   * usage: udpserver <port>
 *    */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define BUFSIZE 1024

/*
 *  * error - wrapper for perror
 *   */
void error(char *msg) {
    perror(msg);
    exit(1);
}

int main(int argc, char **argv) {
    int sockfd; /* socket */
    int portno; /* port to listen on */
    int clientlen; /* byte size of client's address */
    struct sockaddr_in serveraddr; /* server's addr */
    struct sockaddr_in clientaddr; /* client addr */
    struct hostent *hostp; /* client host info */
    char buf[BUFSIZE]; /* message buf */
    char *hostaddrp; /* dotted decimal host addr string */
    int optval; /* flag value for setsockopt */
    int n; /* message byte size */

    /* 
     *    * check command line arguments 
     *       */
    if (argc != 2) {
        fprintf(stderr, "usage: %s <port>\n", argv[0]);
        exit(1);
    }
    portno = atoi(argv[1]);

    /* 
     *    * socket: create the parent socket 
     *       */
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");

    /* setsockopt: Handy debugging trick that lets 
     *    * us rerun the server immediately after we kill it; 
     *       * otherwise we have to wait about 20 secs. 
     *          * Eliminates "ERROR on binding: Address already in use" error. 
     *             */
    optval = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, 
            (const void *)&optval , sizeof(int));

    /*
     *    * build the server's Internet address
     *       */
    bzero((char *) &serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serveraddr.sin_port = htons((unsigned short)portno);

    /* 
     *    * bind: associate the parent socket with a port 
     *       */
    if (bind(sockfd, (struct sockaddr *) &serveraddr, 
                sizeof(serveraddr)) < 0) 
        error("ERROR on binding");

    /* 
     *    * main loop: wait for a datagram, then echo it
     *       */
    clientlen = sizeof(clientaddr);
    while (1) {

        /*
         *      * recvfrom: receive a UDP datagram from a client
         *           */
        bzero(buf, BUFSIZE);
        n = recvfrom(sockfd, buf, 3, 0,
                (struct sockaddr *) &clientaddr, &clientlen);
        if (n < 0)
            error("ERROR in recvfrom");

        n = recvfrom(sockfd, buf + 3, (BUFSIZE - 3), 0,
                (struct sockaddr *) &clientaddr, &clientlen);
        if (n < 0)
            error("ERROR in recvfrom");

        /* 
         *      * gethostbyaddr: determine who sent the datagram
         *           */
        hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, 
                sizeof(clientaddr.sin_addr.s_addr), AF_INET);
        if (hostp == NULL)
            error("ERROR on gethostbyaddr");
        hostaddrp = inet_ntoa(clientaddr.sin_addr);
        if (hostaddrp == NULL)
            error("ERROR on inet_ntoa\n");
        printf("server received datagram from %s (%s)\n", 
                hostp->h_name, hostaddrp);
        printf("server received %d/%d bytes: %s\n", strlen(buf), n, buf);

        /* 
         *      * sendto: echo the input back to the client 
         *           */
        n = sendto(sockfd, buf, strlen(buf), 0, 
                (struct sockaddr *) &clientaddr, clientlen);
        if (n < 0) 
            error("ERROR in sendto");
    }
}

Udp Svr 里面调用了两次 readfrom!!!

 

udpcli:

/* 
 *  * udpclient.c - A simple UDP client
 *   * usage: udpclient <host> <port>
 *    */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

#define BUFSIZE 1024

/* 
 *  * error - wrapper for perror
 *   */
void error(char *msg) {
    perror(msg);
    exit(0);
}

int main(int argc, char **argv) {
    int sockfd, portno, n;
    int serverlen;
    struct sockaddr_in serveraddr;
    struct hostent *server;
    char *hostname;
    char buf[BUFSIZE];

    /* check command line arguments */
    if (argc != 3) {
        fprintf(stderr,"usage: %s <hostname> <port>\n", argv[0]);
        exit(0);
    }
    hostname = argv[1];
    portno = atoi(argv[2]);

    /* socket: create the socket */
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");

    /* gethostbyname: get the server's DNS entry */
    server = gethostbyname(hostname);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host as %s\n", hostname);
        exit(0);
    }

    /* build the server's Internet address */
    bzero((char *) &serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
            (char *)&serveraddr.sin_addr.s_addr, server->h_length);
    serveraddr.sin_port = htons(portno);

    /* get a message from the user */
    bzero(buf, BUFSIZE);
    printf("Please enter msg: ");
    fgets(buf, BUFSIZE, stdin);

    /* send the message to the server */
    serverlen = sizeof(serveraddr);
    n = sendto(sockfd, buf, strlen(buf), 0, (const struct sockaddr *)(&serveraddr), serverlen);
    if (n < 0) 
        error("ERROR in sendto");

    serverlen = sizeof(serveraddr);
    n = sendto(sockfd, buf, strlen(buf), 0, (const struct sockaddr *)(&serveraddr), serverlen);
    if (n < 0) 
        error("ERROR in sendto");

    /* print the server's reply */
    n = recvfrom(sockfd, buf, strlen(buf), 0, (struct sockaddr *)(&serveraddr), &serverlen);
    if (n < 0) 
        error("ERROR in recvfrom");
    printf("Echo from server: %s", buf);
    return 0;
}

udp 这里调用了两次 sendto, 如果只调用一次 sendto, 那么 udp srv 就会阻塞在第二个 readfrom 即便你发送的数据长度大于 3 字节.

udp 的输入与输出:

./server 10003
server received datagram from localhost (127.0.0.1)
server received 14/11 bytes: 1231234567890

./client 127.0.0.1 10003
Please enter msg: 1234567890
Echo from server: 12312345678

具体原理可以参考:

https://my.oschina.net/tsh/blog/995626

© 著作权归作者所有

共有 人打赏支持
老汉-憨憨
粉丝 18
博文 322
码字总数 68382
作品 0
深圳
程序员
网络性能测试工具Iperf介绍

【概要】Iperf是一款网络性能测试工具,可以方便的用它进行SDN网络带宽和网络质量的测试,Iperf支持协议、定时、缓冲区等参数的配置调整,报告TCP/UDP最大带宽、延迟抖动、数据包丢失等统计信...

SDN_LAB
2014/12/04
0
1
网络性能测试工具Iperf/Jperf

Iperf 是一个网络性能测试工具。Iperf 可以测试TCP 和UDP 带宽质量。Iperf 可以测量最大TCP 带宽,具有多种参数和UDP 特性。 Iperf 可以报告带宽,延时抖动和数据包丢失。利用 Iperf 这一特性...

ljb_job
07/26
0
0
网络测试工具--Iperf、MZ使用技巧

Iperf Iperf 是一个网络性能测试工具。Iperf可以测试最大TCP和UDP带宽性能,具有多种参数和UDP特性,可以根据需要调整,可以报告带宽、延迟抖动和数据包丢失。 0 客户端与服务器共用选项 服务...

造梦先森Kai的专栏
2017/12/18
0
0
android中的socket

什么是Socket? 所谓Socket通常也称作“套接字”,用于描述IP地址和端口,是一个通信连的句柄,应用程序通常通过“套接字”向网络发送请求或者应答网络请求,它就是网络通信过程中端点的抽象表...

一别经年
2013/12/28
0
0
Udp、Tcp、Http Socket

1.TCP与UDP 面向报文的传输方式是应用层交给UDP多长的报文,UDP就照样发送,即一次发送一个报文。因此,应用程序必须选择合适大小的报文。若报文太长,则IP层需要分片,降低效率。若太短,会...

V_joy
2016/07/26
45
0

没有更多内容

加载失败,请刷新页面

加载更多

Windows小技巧 – Win+R提高Windows使用效率

追求效率的朋友都需要一款顺手的快速启动工具,Win 平台上有键盘流的RunZ、Listary、ALTRun、Launchy、Wox 和图标流的 Fences、Rolan、 WinLaunch 等,而 Mac 上也有 Alfred、Spotlight。 而...

QQZZFT
5分钟前
0
0
Android 移植ffmpeg报错:"atof" referenced by "libavformat.so"

cannot locate symbol "atof" referenced by "libavformat-57.so"... 原因:android的stdlib.h中atof是内联的, 外部模块不能直接使用。跟android版本有关。 解决办法:将所有的atof改成str...

她叫我小渝
20分钟前
0
0
Java并发编程:volatile关键字解析

volatile这个关键字可能很多朋友都听说过,或许也都用过。在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果。在Java 5之后,volatile关键字才得以重获生...

engeue
36分钟前
1
0
php-fpm配置文件详解

第一部分:FPM 配置 参数          | 说明 -p            | 命令行中动态修改--prefix   ;include=etc/fpm.d/*.conf  | 用于包含一个或多个文件,如果glob(3)存在(...

bengozhong
38分钟前
1
0
Django允许外部ip访问

1、关闭防火墙 service iptables stop 2、设置django 开启django时,使用0.0.0.0:xxxx,作为ip和端口例如: python manage.py runserver 0.0.0.0:8000 3、在settings里修改ALLOWED_HOSTS = [......

MichaelShu
41分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部