windows下建立tcp收发连接

在Windows 10环境下,尝试使用GCC编译包含socket接口的C程序时遇到头文件未找到及类型未定义的错误。通过查看gcc --version确认GCC版本为4.9.2。错误涉及stdc-predef.h、process.h、stdio.h、winsock2.h、stdlib.h等头文件。尝试使用VSCode和cl.exe编译,cl.exe成功完成编译。解决方法包括设置环境变量PATH、INCLUDE和LIB,指向Visual Studio和Windows Kits的相应目录。

socket接口详解

socket接口详解 - 木椅的博客 - 博客园

使用gcc无法编译问题仍未解决:

gcc --version能够返回:

D:\Download\UDP_CarSim>gcc --version

gcc (GCC) 4.9.2

Copyright (C) 2014 Free Software Foundation, Inc.

This is free software; see the source for copying conditions.  There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

但使用gcc编译时总是报找不到头文件:

D:\Download\UDP_CarSim>gcc tcp_test.c

cc1: error: no include path in which to search for stdc-predef.h

tcp_test.c:4:21: error: no include path in which to search for process.h

#include <process.h>

                    ^

tcp_test.c:5:19: error: no include path in which to search for stdio.h

#include <stdio.h>

                  ^

tcp_test.c:6:22: error: no include path in which to search for winsock2.h

#include <winsock2.h>

                      ^

tcp_test.c:7:20: error: no include path in which to search for stdlib.h

#include <stdlib.h>

                    ^

tcp_test.c: In function 'main':

tcp_test.c:14:5: error: unknown type name 'WORD'

    WORD sockVersion = MAKEWORD(2,2);

    ^

tcp_test.c:15:5: error: unknown type name 'WSADATA'

    WSADATA wsaData;

    ^

tcp_test.c:22:5: error: unknown type name 'SOCKET'

    SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    ^

tcp_test.c:22:29: error: 'AF_INET' undeclared (first use in this function)

    SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

                            ^

tcp_test.c:22:29: note: each undeclared identifier is reported only once for each function it appears in

tcp_test.c:22:38: error: 'SOCK_STREAM' undeclared (first use in this function)

    SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

                                      ^

tcp_test.c:22:51: error: 'IPPROTO_TCP' undeclared (first use in this function)

    SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

                                                  ^

tcp_test.c:23:19: error: 'INVALID_SOCKET' undeclared (first use in this function)

    if(slisten == INVALID_SOCKET)

                  ^

tcp_test.c:25:9: warning: incompatible implicit declaration of built-in function 'printf'

        printf("socket error !");

        ^

tcp_test.c:31:5: error: unknown type name 'SOCKADDR_IN'

    SOCKADDR_IN sin;

    ^

tcp_test.c:32:8: error: request for member 'sin_family' in something not a structure or union

    sin.sin_family = AF_INET;

        ^

tcp_test.c:33:8: error: request for member 'sin_port' in something not a structure or union

    sin.sin_port = htons(8888);

        ^

tcp_test.c:34:8: error: request for member 'sin_addr' in something not a structure or union

    sin.sin_addr.S_un.S_addr = INADDR_ANY;

        ^

tcp_test.c:34:32: error: 'INADDR_ANY' undeclared (first use in this function)

    sin.sin_addr.S_un.S_addr = INADDR_ANY;

                                ^

tcp_test.c:35:23: error: 'LPSOCKADDR' undeclared (first use in this function)

    if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)

                      ^

tcp_test.c:35:56: error: 'SOCKET_ERROR' undeclared (first use in this function)

    if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)

                                                        ^

tcp_test.c:37:9: warning: incompatible implicit declaration of built-in function 'printf'

        printf("bind error !");

        ^

tcp_test.c:43:9: warning: incompatible implicit declaration of built-in function 'printf'

        printf("listen error !");

        ^

tcp_test.c:48:5: error: unknown type name 'SOCKET'

    SOCKET sClient;

    ^

tcp_test.c:49:5: error: unknown type name 'SOCKADDR_IN'

    SOCKADDR_IN remoteAddr;

    ^

tcp_test.c:52:12: error: 'TRUE' undeclared (first use in this function)

    while (TRUE)

            ^

tcp_test.c:54:9: warning: incompatible implicit declaration of built-in function 'printf'

        printf("wait for connecting...\n");

        ^

tcp_test.c:55:36: error: 'SOCKADDR' undeclared (first use in this function)

        sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);

                                    ^

tcp_test.c:55:46: error: expected expression before ')' token

        sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);

                                              ^

tcp_test.c:61:68: error: request for member 'sin_addr' in something not a structure or union

        printf("accepted a connect锛?s \r\n", inet_ntoa(remoteAddr.sin_addr));

                                                                    ^

tcp_test.c:73:33: warning: incompatible implicit declaration of built-in function 'strlen'

        send(sClient, sendData, strlen(sendData), 0);

使用链接Win10使用VS Code编译运行,调试C/C++ - 知乎中的方法也是一样的结果。

最终使用VS的cl.exe成功编译:

Win10命令行窗口编译C代码 - 程序员大本营

在编译的计算机上环境变量设置为:

Path新增:C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64

include:C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt;C:\build\17.0\x64\sysroots\core2-64-nilrt-linux\usr\include;C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\atlmfc\src\atl\atls;C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0;C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\shared;

LIB: C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\um\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\ucrt\x64;C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\onecore\x64;

socket实现简单TCP循环通信(Python) - 彩虹然 - 博客园

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒墨阁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值