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;
}

运行结果:

相关推荐
冰橙子id1 小时前
linux-远程访问管理(sshd,scp,sftp)
linux·网络·ssh
光电的一只菜鸡2 小时前
ubuntu之坑(十五)——设备树
linux·数据库·ubuntu
saynaihe4 小时前
ubuntu 22.04 anaconda comfyui安装
linux·运维·服务器·ubuntu
企鹅与蟒蛇4 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
程序设计实验室4 小时前
小心误关了NAS服务器!修改Linux的电源键功能
linux·nas
不知道叫什么呀5 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
秋说5 小时前
【PTA数据结构 | C语言版】顺序队列的3个操作
c语言·数据结构·算法
渡我白衣7 小时前
Linux操作系统之信号:信号的产生
linux
阿巴~阿巴~7 小时前
理解Linux文件系统:从物理存储到统一接口
linux·运维·服务器
秋说8 小时前
【PTA数据结构 | C语言版】字符串插入操作(不限长)
c语言·数据结构·算法