【问题标题】:I am not getting the below header files in c language我没有得到以下 c 语言的头文件
【发布时间】:2015-09-24 03:40:51
【问题描述】:
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/wait.h>
    #include <sys/socket.h> //Unable to find all of this header files
    #include <signal.h>
    #include <ctype.h>          
    #include <arpa/inet.h>
    #include <netdb.h>

我想在我的程序中使用这个头文件,但我在互联网上找不到它,请提供我可以从中获取它们的来源

【问题讨论】:

  • 你的电脑?也许您需要重新安装 C?
  • 看起来像代码 sn-p 假定的 unix 系统。如果您不在 unix 系统上,您可能找不到它...
  • 程序中的目录配置了吗?通常您可能需要配置它们。
  • 您在哪个平台上运行?你使用哪个编译器?你如何编译代码(什么标志)?
  • 我不知道这一切,如果你知道,请指导

标签: c


【解决方案1】:

在 Windows 上,套接字 api 被打包到一组不同的标头中。但是 Windows 套接字的工作方式(如果以“便携式”方式使用)与在 unix 系统上几乎相同。给予或接受。

您链接的另一个问题的示例代码在 Windows 上如下所示:

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
//#include <netinet/in.h>
#include <WinSock2.h>
#include <Ws2tcpip.h>
//#include <sys/wait.h>
//#include <sys/socket.h>
//#include <signal.h>
#include <ctype.h>          
//#include <arpa/inet.h>
//#include <netdb.h>

#define PORT 20000
#define LENGTH 512 


void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
    /* Variable Definition */
    SOCKET sockfd;
    char revbuf[LENGTH];
    struct sockaddr_in remote_addr;

    /* Get the Socket file descriptor */
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor! (errno = %d)\n", errno);
        exit(1);
    }

    /* Fill the socket address struct */
    remote_addr.sin_family = AF_INET;
    remote_addr.sin_port = htons(PORT);
    inet_pton(AF_INET, "127.0.0.1", &remote_addr.sin_addr);
    ZeroMemory(&(remote_addr.sin_zero), 8);

    /* Try to connect the remote */
    if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1)
    {
        fprintf(stderr, "ERROR: Failed to connect to the host! (errno = %d)\n", errno);
        exit(1);
    }
    else
        printf("[Client] Connected to server at port %d...ok!\n", PORT);

    /* Send File to Server */
    //if(!fork())
    //{
    char* fs_name = "/home/aryan/Desktop/quotidiani.txt";
    char sdbuf[LENGTH];
    printf("[Client] Sending %s to the Server... ", fs_name);
    FILE *fs = NULL;
    errno_t err = fopen_s(&fs, fs_name, "r");
    if (fs == NULL)
    {
        printf("ERROR: File %s not found.\n", fs_name);
        exit(1);
    }

    ZeroMemory(sdbuf, LENGTH);
    int fs_block_sz;
    while ((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) > 0)
    {
        if (send(sockfd, sdbuf, fs_block_sz, 0) < 0)
        {
            fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", fs_name, errno);
            break;
        }
        ZeroMemory(sdbuf, LENGTH);
    }
    printf("Ok File %s from Client was Sent!\n", fs_name);
    //}

    /* Receive File from Server */
    printf("[Client] Receiveing file from Server and saving it as final.txt...");
    char* fr_name = "/home/aryan/Desktop/progetto/final.txt";
    FILE *fr = NULL;
    err = fopen_s(&fr, fr_name, "a");
    if (fr == NULL)
        printf("File %s Cannot be opened.\n", fr_name);
    else
    {
        ZeroMemory(revbuf, LENGTH);
        int fr_block_sz = 0;
        while ((fr_block_sz = recv(sockfd, revbuf, LENGTH, 0)) > 0)
        {
            int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr);
            if (write_sz < fr_block_sz)
            {
                error("File write failed.\n");
            }
            ZeroMemory(revbuf, LENGTH);
            if (fr_block_sz == 0 || fr_block_sz != 512)
            {
                break;
            }
        }
        if (fr_block_sz < 0)
        {
            if (errno == EAGAIN)
            {
                printf("recv() timed out.\n");
            }
            else
            {
                fprintf(stderr, "recv() failed due to errno = %d\n", errno);
            }
        }
        printf("Ok received from server!\n");
        fclose(fr);
    }
    //close(sockfd);
    closesocket(sockfd);
    printf("[Client] Connection lost.\n");
    return (0);
}

现在您可以比较一下,看看哪些是相同的,哪些是不同的。希望能帮助你理解。

如果您接下来想知道为什么会出现链接器错误:链接到 ws2_32.lib。与套接字是“libc”的一部分的 unix 系统相比,套接字在 windows 中被分解为单独的 dll。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2012-10-16
    • 2018-02-18
    • 1970-01-01
    相关资源
    最近更新 更多