C语言 用 C 语言进行 Windows 套接字编程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6769981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:11:00  来源:igfitidea点击:

Windows Socket Programming in C

cwindowssocketsnetworkingtcp

提问by networkingNoob

I am taking a networking class where the Professor is literally reading the book to the class. Needless to say I have no Idea what I am doing. Our semester project is to copy code from our text book and make a client-server network. Literally copying the code from teh book with no modifications.

我正在参加网络课程,教授实际上是在课堂上读这本书。不用说,我不知道我在做什么。我们的学期项目是从我们的教科书中复制代码并制作客户端-服务器网络。从字面上复制代码从书中没有修改。

The book had mistakes in the code (missing semicolons, extra paranthesis) but I managed to at least compile the code. However, I run into a bunch of link errors.

这本书在代码中存在错误(缺少分号,额外的括号),但我至少设法编译了代码。但是,我遇到了一堆链接错误。

Example: Error 1 error LNK2019: unresolved external symbol impsendto@24 referenced in function _main C:\Users\Documents\Visual Studio 2010\Projects\Client_Server\Client_Server\Client_Server\Server.obj Client_Server

示例:错误 1 ​​错误 LNK2019:未解析的外部符号 impsendto@24 在函数 _main C:\Users\Documents\Visual Studio 2010\Projects\Client_Server\Client_Server\Client_Server\Server.obj Client_Server 中引用

i looked up the error code and I think the code is trying to link to definitions that are not existent in the header files. I have a tough time fixing LNK errors vs Syntax errors. But like I said I have no idea how to go about fixing this. I am sending the code for the server side, I ran into the same errors on the client side.

我查找了错误代码,我认为该代码正试图链接到头文件中不存在的定义。我很难修复 LNK 错误与语法错误。但就像我说的,我不知道如何解决这个问题。我正在发送服务器端的代码,我在客户端遇到了同样的错误。

include <stdio.h>
include <string.h>
include <WinSock2.h>
include <WinSock.h>
include <stdint.h>
include <time.h>

int main(void) {

int s;      
int len;
char  buffer[256];  
struct sockaddr_in servAddr; 
struct sockaddr_in clntAddr; 

int clntAddrLen; //length of client socket addre

//Build local (server) socket add

memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(21);
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);

   //create socket
if((s=socket(PF_INET, SOCK_DGRAM, 0) <0 ))
{
   perror("Error: Socket Failed!");
    exit(1);
}

//bind socket to local address and port
if((bind(s,(struct sockaddr*)&servAddr, sizeof(servAddr))<0))
{
    perror("Error:bind failed!");
    exit(1);
}

for(;;)
{
len = recvfrom(s,buffer, sizeof(buffer),0,(struct sockaddr*)&clntAddr, &clntAddrLen);

    //send string
    sendto(s, buffer, len, 0, (struct sockaddr*)&clntAddr, sizeof(clntAddr));
}

}

Any tips, links to useful info, or advice would be appreciated. I tried reading the text book but I am completely lost. Also, this is the only code related assignment we have done all semester. Everything else has been collecting packets using a packet sniffer. Literally came into class and said copy and run code on page X.

任何提示、有用信息的链接或建议将不胜感激。我试着读课本,但我完全迷失了。此外,这是我们整个学期完成的唯一与代码相关的作业。其他一切都使用数据包嗅探器收集数据包。从字面上进入课堂并说复制并运行第 X 页上的代码。

回答by bdonlan

You need to link the library Ws2_32.lib to use winsock. You also must call WSAStartupbefore using any other winsock functions (this isn't causing your current error, but will cause you problems once you fix the missing library issue).

您需要链接库 Ws2_32.lib 才能使用 winsock。在使用任何其他 winsock 函数之前,您还必须调用WSAStartup(这不会导致您当前的错误,但是一旦您修复了丢失的库问题,就会导致您出现问题)。

回答by Dydzio

At first I will try to help using your last comment: Let us assume you are using Visual Studio (I think it is best option to start winsock for windows programming as Microsoft cares about Windows basic libraries being up to date and they are compatible with helpful msdn support).

首先,我将尝试使用您的最后一条评论来帮助您:让我们假设您使用的是 Visual Studio(我认为启动 winsock 进行 Windows 编程是最佳选择,因为 Microsoft 关心 Windows 基本库是最新的,并且它们与有用的兼容msdn 支持)。

If you are getting error such as this one: 1>asdf.obj : error LNK2001: unresolved external symbol _imp_WSAStartup@8 it means that ws2_32.lib is not linked correctly. To do that right click on your project in solution explorer, go to linker -> input and add ws2_32.lib to additional dependencies. This library is part of windows SDK (I guess it is installed together with most versions of Visual Studio), so make sure the file exists on your computer.

如果您收到这样的错误: 1>asdf.obj : error LNK2001: unresolved external symbol _ imp_WSAStartup@8 这意味着 ws2_32.lib 没有正确链接。为此,在解决方案资源管理器中右键单击您的项目,转到链接器 -> 输入并将 ws2_32.lib 添加到其他依赖项。该库是 Windows SDK 的一部分(我猜它与大多数版本的 Visual Studio 一起安装),因此请确保该文件存在于您的计算机上。

And now how to make correct project in modern style without following ancient tutorials:

现在如何在不遵循古老教程的情况下以现代风格制作正确的项目:

The library you need to add is Winsock2.h. Winsock.h is old (deprecated) version and there is no need to use it in new applications. To start using sockets you need to call function WSAStartup, to do that you must initialize struct WSADATA at the beginning. The basic piece of code looks like this:

您需要添加的库是 Winsock2.h。Winsock.h 是旧的(已弃用)版本,无需在新应用程序中使用它。要开始使用套接字,您需要调用函数 WSAStartup,为此您必须在开始时初始化 struct WSADATA。基本代码如下所示:

#include <Winsock2.h>
int main()
{
WSADATA mywsadata; //your wsadata struct, it will be filled by WSAStartup
WSAStartup(0x0202,&mywsadata); //0x0202 refers to version of sockets we want to use.
//here goes your code with socket related things
return 0;
}

For more help you can visit here

如需更多帮助,您可以访问这里

A note: since question is old and I am not sure its author will ever find my answer helpful I want to help another users looking at this question

注意:由于问题很老,我不确定它的作者是否会发现我的回答有帮助我想帮助其他用户查看这个问题