针对ECHO服务的TCP客户软件的实现(附源码含注释)

Posted 5cmpers

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了针对ECHO服务的TCP客户软件的实现(附源码含注释)相关的知识,希望对你有一定的参考价值。

实验原理:TCP客户端交互模式

步骤一:引入头文件

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

步骤二:Echo客户端所需函数声明以及宏定义

/*访问Echo服务到特定端口号并输出回应*/
int TCPecho(const char *host, const char *service);  
/*TCP链接*/
int connectTCP(const char *host, const char *service);
/*连接函数*/
int connectsock(const char* host, const char* service, const char* transport);
/*报错并退出*/
int errexit(const char* format, ...);

#define LINELEN 128
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

步骤三:主函数的实现,定义本机IP和服务名,并调用TCPecho函数实现通过TCP访问Echo服务

int main(int argc, char *argv[])
    char *host = "localhost";
    char *service = "echo";
    switch (argc)
    
        case 1:
                host = "localhost";
                break;
        case 3:
                service = argv[2];
        case 2:
                host = argv[1];
                break;
        default:
                fprintf(stderr, "usage: TCPecho [host [prot]]\\n");
                exit(1);
    
    TCPecho(host, service);
    exit(0);

步骤四:TCPecho函数的实现。连接TCP后循环读取每行输入,并将该输入传输至Echo服务器,再循环读取Echo服务器返回的数据。

int TCPecho(const char* host, const char* service)

    char buf[LINELEN + 1];  //一行的字符个数
    int s, n;  //套接字描述符、读计数符
    int outchars, inchars;  //发送数据、接收数据

    s = connectTCP(host, service);  //连接TCP

    while (fgets(buf, sizeof(buf), stdin))  //循环重复读取每行输入
    
        buf[LINELEN] = '\\0';  //结束标志
        outchars = strlen(buf);  //(客户)输入数据大小
        (void)write(s, buf, outchars);  //将该行输入数据发送至Echo服务器

        for (inchars = 0; inchars < outchars; inchars += n)
        
            n = read(s, &buf[inchars], outchars = inchars);  //读取返回的数据
            if (n < 0)
            
                errexit("socket read failed: %s\\n", strerror(errno));  //若无返回数据,报错
            
            fputs(buf, stdout);  //打印读取到的数据
        
    

步骤五:connectTCP函数的实现,第二过程,调用socketsock选择TCP连接

int connectTCP(const char* host, const char* service)

    return connectsock(host, service, "tcp");

步骤六:connectsock函数的实现,第三过程,实现所有分配和连接TCP/UDP套接字的操作(连接的实现)

int connectsock(const char* host, const char* service, const char* transport)

    struct hostent *phe;  //主机信息
    struct servent *pse;  //服务信息
    struct protoent* ppe;  //协议信息
    struct sockaddr_in sin;  //网络端点地址 family类型 port协议号  sin_addr IP地址
    int s, type;
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;  //类型

    /*匹配服务名和端口号*/
    if (pse = getservbyname(service, transport))
    
        sin.sin_port = pse->s_port;
    
    else if ((sin.sin_port = htons((unsigned short)atoi(service))) == 0)
    
        errexit("can't get \\"%s\\" service entry\\n", service);
    

    /*匹配主机名至IP地址(允许点分十进制)*/
    if (phe = gethostbyname(host))
    
        memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
    
    else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
    
        errexit("can't get \\"%s\\" host entry\\n",host);
    

    /*匹配传输协议名至协议号*/
    if ((ppe = getprotobyname(transport)) == 0)
    
        errexit("can't get \\"%s\\" protocol entry\\n", host);
    

    /*通过协议选择套接字类型*/
    if (strcmp(transport, "udp") == 0)
    
        type = SOCK_DGRAM;  //不发起分组交换,不检测远程端点地址合法性
    
    else
    
        type = SOCK_STREAM;
    

    /*分配套接字*/
    s = socket(PF_INET, type, ppe->p_proto);
    if (s < 0)
    
        errexit("can't create socket: %s\\n", strerror(errno));
    

    /*连接套接字*/
    if (connect(s, (struct sockaddr*)&sin, sizeof(sin)) < 0)
    
        errexit("can't connect to %s.%s: %s\\n", host, service, strerror(errno));
    
    return s;

步骤七:报错函数的实现

int errexit(const char* format, ...)

    va_list args;
    va_start(args, format);
    vfprintf(stderr, format, args);
    va_end(args);
    exit(1);

实验结果

完整源码

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

/*访问Echo服务到特定端口号并输出回应*/
int TCPecho(const char *host, const char *service);  
/*TCP链接*/
int connectTCP(const char *host, const char *service);
/*连接函数*/
int connectsock(const char* host, const char* service, const char* transport);
/*报错并退出*/
int errexit(const char* format, ...);

#define LINELEN 128
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

int main(int argc, char *argv[])
    char *host = "localhost";
    char *service = "echo";
    switch (argc)
    
        case 1:
                host = "localhost";
                break;
        case 3:
                service = argv[2];
        case 2:
                host = argv[1];
                break;
        default:
                fprintf(stderr, "usage: TCPecho [host [prot]]\\n");
                exit(1);
    
    TCPecho(host, service);
    exit(0);


int TCPecho(const char* host, const char* service)

    char buf[LINELEN + 1];  //一行的字符个数
    int s, n;  //套接字描述符、读计数符
    int outchars, inchars;  //发送数据、接收数据

    s = connectTCP(host, service);  //连接TCP

    while (fgets(buf, sizeof(buf), stdin))  //循环重复读取每行输入
    
        buf[LINELEN] = '\\0';  //结束标志
        outchars = strlen(buf);  //(客户)输入数据大小
        (void)write(s, buf, outchars);  //将该行输入数据发送至Echo服务器

        for (inchars = 0; inchars < outchars; inchars += n)
        
            n = read(s, &buf[inchars], outchars = inchars);  //读取返回的数据
            if (n < 0)
            
                errexit("socket read failed: %s\\n", strerror(errno));  //若无返回数据,报错
            
            fputs(buf, stdout);  //打印读取到的数据
        
    


/*第二过程 选择TCP或UDP连接*/
int connectTCP(const char* host, const char* service)

    return connectsock(host, service, "tcp");


/*第三过程 连接的实现(包含所有分配和连接TCP/UDP套接字)*/
int connectsock(const char* host, const char* service, const char* transport)

    struct hostent *phe;  //主机信息
    struct servent *pse;  //服务信息
    struct protoent* ppe;  //协议信息
    struct sockaddr_in sin;  //网络端点地址 family类型 port协议号  sin_addr IP地址
    int s, type;
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;  //类型

    /*匹配服务名和端口号*/
    if (pse = getservbyname(service, transport))
    
        sin.sin_port = pse->s_port;
    
    else if ((sin.sin_port = htons((unsigned short)atoi(service))) == 0)
    
        errexit("can't get \\"%s\\" service entry\\n", service);
    

    /*匹配主机名至IP地址(允许点分十进制)*/
    if (phe = gethostbyname(host))
    
        memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
    
    else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
    
        errexit("can't get \\"%s\\" host entry\\n",host);
    

    /*匹配传输协议名至协议号*/
    if ((ppe = getprotobyname(transport)) == 0)
    
        errexit("can't get \\"%s\\" protocol entry\\n", host);
    

    /*通过协议选择套接字类型*/
    if (strcmp(transport, "udp") == 0)
    
        type = SOCK_DGRAM;  //不发起分组交换,不检测远程端点地址合法性
    
    else
    
        type = SOCK_STREAM;
    

    /*分配套接字*/
    s = socket(PF_INET, type, ppe->p_proto);
    if (s < 0)
    
        errexit("can't create socket: %s\\n", strerror(errno));
    

    /*连接套接字*/
    if (connect(s, (struct sockaddr*)&sin, sizeof(sin)) < 0)
    
        errexit("can't connect to %s.%s: %s\\n", host, service, strerror(errno));
    
    return s;


/*报错*/
int errexit(const char* format, ...)

    va_list args;
    va_start(args, format);
    vfprintf(stderr, format, args);
    va_end(args);
    exit(1);

以上是关于针对ECHO服务的TCP客户软件的实现(附源码含注释)的主要内容,如果未能解决你的问题,请参考以下文章

针对ECHO服务的TCP客户软件的实现( Linux + Socket + C + Code + Annotation)

针对TIME服务的UDP客户软件的实现(附源码含注释)

TCP通信聊天服务端和客户端(C/C++语言开发)附完整源码

基于SpringBoot的客户关系管理系统的设计与实现(附源码)

基于java TCP实现网络通信聊天室《建议收藏附完整源码》

基于java TCP实现网络通信聊天室《建议收藏附完整源码》