LinuxC语言-网络通信tcp/ip & errno获取错误描述字符串

目录

服务端代码:

获取errno错误码:

客户端代码:

运行结果:


服务端代码:

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


/*
socket
bind
listen
accept
send/receive
*/
#define SOCKET_PORT 8888
#define SOCKET_BACKLOG 10
 
 

int main(int argc, char **argv)
{
    char  *errStr;

    int iSocketSever;
    struct sockaddr_in tSocketSeverAddress;
    struct sockaddr_in tSocketClientAddress;
    int iRet;
    int iClientSocket;
    int iAddrLen;
    int iRecvLen;
    unsigned char ucReceiveBuffer[1000];
    int iClientNumber = -1;    
 
 
    iSocketSever = socket(AF_INET, SOCK_STREAM, 0);
    if (iSocketSever < 0)
    {
       
        printf("socket error\n");
    }
 
    tSocketSeverAddress.sin_family = AF_INET;
    tSocketSeverAddress.sin_port = htons(SOCKET_PORT);// host to network address,short port
    tSocketSeverAddress.sin_addr.s_addr = INADDR_ANY;//本机所有IP
    memset(tSocketSeverAddress.sin_zero , 0, 8);
 
 
    iRet = bind(iSocketSever, ( struct sockaddr *)&tSocketSeverAddress, sizeof(struct sockaddr));
    if (iRet == -1)
    {
        errStr = (char*)strerror(errno);
        printf("bind error, errno = %d\n",errno);
        printf("bind error, error = %s\n",errStr);
        return -1;
    }
 
    iRet = listen(iSocketSever, SOCKET_BACKLOG);
    if (iRet == -1)
    {
        printf("listen error\n");
        
        return -1;
    }
 
    while (1)
    {
       iAddrLen = sizeof(struct sockaddr); //
       iClientSocket = accept(iSocketSever, (struct sockaddr *)&tSocketClientAddress,&iAddrLen);
       if (iClientSocket != -1)
       {
            iClientNumber ++;
            printf("connect success,from client %d: %s\n", iClientNumber, inet_ntoa(tSocketClientAddress.sin_addr));
            if (!fork())
            {
                /*子进程的源码*/
                while (1)
                {
                    /* 接受客户端发来的数据并显示出来 */
                   iRecvLen = recv(iClientSocket, ucReceiveBuffer,999,0);
                    if (iRecvLen <= 0)
                    {
                        close(iClientSocket);
                        return -1;
                    }
                    else
                    {   
                        ucReceiveBuffer[iRecvLen+1] = '\0';
                        printf("get msg from client %d: %s\n", iClientNumber, ucReceiveBuffer);
                    }
                }
            }
       }
    
    }
    close(iSocketSever);
    return 0;
}

获取errno错误码:

函数原型:char *strerror(int errnum);

通过标准错误的编号errno,获得错误的描述字符串;

方便用户查找,返回值是指向错误信息的指针。

#include<errno.h>

#include<string.h>

char *errStr;

errStr = (char*)strerror(errno);

printf("bind error, error = %s\n",errStr);

客户端代码:

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


/*
socket
connect
send/recva
*/
#define SOCKET_PORT 8888

int main(int argc, char **argv)
{
    int iClientSocket;
    struct sockaddr_in tSocketSeverAddress;
    int iRet;   
    unsigned char ucSendBuffer[1000];
    int iSendLen;

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

    iClientSocket = socket(AF_INET, SOCK_STREAM, 0);
 
    tSocketSeverAddress.sin_family = AF_INET;
    tSocketSeverAddress.sin_port = htons(SOCKET_PORT);
    if ( inet_aton(argv[1],&tSocketSeverAddress.sin_addr) == 0 )
    {
        printf("invalid server IP \n");
        return -1;
    }
    memset(tSocketSeverAddress.sin_zero , 0, 8);
    iRet = connect(iClientSocket, (struct sockaddr *)&tSocketSeverAddress, sizeof(tSocketSeverAddress));
    if ( iRet == -1 )
    {
        printf("connect failed\n");
    }

    while (1)
    {
        if (fgets(ucSendBuffer,999,stdin))
        {
            iSendLen = send(iClientSocket, &ucSendBuffer ,strlen(ucSendBuffer),0);
            if ( iSendLen < 0)
            {
                close(iClientSocket);
                return -1;
            }
        }
    }
    
    return 0;
}

运行结果:

相关推荐
iiiiiankor1 小时前
C/C++内存管理 | new的机制 | 重载自己的operator new
java·c语言·c++
小辛学西嘎嘎1 小时前
C/C++精品项目之图床共享云存储(3):网络缓冲区类和main
c语言·开发语言·c++
Biomamba生信基地2 小时前
Linux也有百度云喔~
linux·运维·服务器·百度云
米饭是菜qy2 小时前
TCP 三次握手意义及为什么是三次握手
服务器·网络·tcp/ip
无敌最俊朗@2 小时前
stm32学习之路——八种GPIO口工作模式
c语言·stm32·单片机·学习
yaoxin5211232 小时前
第十九章 TCP 客户端 服务器通信 - 数据包模式
服务器·网络·tcp/ip
new_abc2 小时前
Ubuntu 22.04 ftp搭建
linux·运维·ubuntu
flying robot2 小时前
RPM的使用
linux
2301_799084673 小时前
超全排序C语言实现
c语言·数据结构·算法·排序算法