用 C 语言进行 Windows 套接字编程 [英] Windows Socket Programming in C

查看:22
本文介绍了用 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 套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆