用C的Windows Socket编程 [英] Windows Socket Programming in C

查看:208
本文介绍了用C的Windows Socket编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在联网类,其中教授字面上读的书到类。不用说,我不知道我在做什么。我们的学期项目是从我们的教科书复制code,使客户端 - 服务器网络。从字面上德书没有修改复制code。

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.

这本书曾在code错误(缺少分号,额外paranthesis),但我设法至少编译code。但是,我碰上了一堆链接错误的。

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:​​解析外部符号impsendto @ 24在功能上_main引用Ç:\\用户\\文档\\ Visual Studio 2010的\\项目\\ Client_Server \\ Client_Server \\ Client_Server \\ Server.obj Client_Server

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

我抬头错误code,我认为code试图连接到未在头文件中存在的定义。我有一个艰难的时间来修复LNK错误VS语法错误。但是就像我说我不知道​​如何去解决这个。我送了code服务器端,我遇到了在客户端同样的错误。

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

}

任何提示,链接有用的信息,或建议将AP preciated。我试着读课本,但我完全失去了。此外,这是唯一的code相关分配我们所做的一切学期。一切已经收集使用数据包嗅探器的数据包。从字面上走进类和上述文本及运行code上页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 Socket编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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