IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> windows下建立tcp收发连接 -> 正文阅读

[网络协议]windows下建立tcp收发连接

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) - 彩虹然 - 博客园

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-03-10 22:59:52  更:2022-03-10 23:00:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/5 13:18:46-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码