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

编程入门 行业动态 更新时间:2024-10-24 04:43:37
本文介绍了用 C 语言进行 Windows 套接字编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在上一门网络课程,教授实际上是在课堂上读这本书.不用说,我不知道我在做什么.我们的学期项目是从我们的教科书中复制代码并制作客户端-服务器网络.直接从书中复制代码,不做任何修改.

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.

示例:错误 1 ​​错误 LNK2019:函数 _main C:UsersDocumentsVisual Studio 2010ProjectsClient_ServerClient_ServerClient_ServerServer.obj Client_Server 中引用的未解析的外部符号 impsendto@24

Example: Error 1 error LNK2019: unresolved external symbol impsendto@24 referenced in function _main C:UsersDocumentsVisual Studio 2010ProjectsClient_ServerClient_ServerClient_ServerServer.obj Client_Server

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

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.

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

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

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.

推荐答案

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

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

更多推荐

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

本文发布于:2023-11-06 21:36:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语言   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!