简体   繁体   English

Windows套接字问题!

[英]Windows sockets problem!

I've started on a school project, and i got some problem when i started to program! 我已经开始了一个学校项目,开始编程时遇到了一些问题! This is my code (far from finish): 这是我的代码(远远没有完成):

WSADATA wsaData; 
WORD wVersionRequested = MAKEWORD( 2, 2 ); 
int err = WSAStartup( wVersionRequested, &wsaData );
SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

struct addrinfo *info; 
int ok = getaddrinfo("www.bt.se","80",NULL,&info);

if(ok!=0) { 
    WCHAR * error = gai_strerror(ok); 
    printf("%s\n",error); 
} else while(info->ai_family != AF_INET && info->ai_next != NULL) 
    info = info->ai_next;

ok = connect(s, info->ai_addr, info->ai_addrlen);

char * message = "GET / HTTP/1.1\r\nHOST: www.bt.se\r\n\r\n"; 
ok = send(s,message,strlen(message),0);

WSACleanup();

The include files are "winsock2.h" and "Ws2tcpip.h"! 包含文件为“ winsock2.h”和“ Ws2tcpip.h”! When i try to compilate the program I got this output: 当我尝试编译程序时,得到以下输出:

------ Build started: Project: Ovning1, Configuration: Debug Win32 ------ ------开始构建:项目:Ovning1,配置:Win32调试------

Ovning.c Ovning.c

c:\\documents and settings\\fredrich\\desktop\\lokala nätverk\\ovning1\\ovning1\\ovning.c(16): warning C4013: 'printf' undefined; c:\\ documents and settings \\ fredrich \\ desktop \\ lokalanätverk\\ ovning1 \\ ovning1 \\ ovning.c(16):警告C4013:'printf'未定义; assuming extern returning int 假设extern返回int

c:\\documents and settings\\fredrich\\desktop\\lokala nätverk\\ovning1\\ovning1\\ovning.c(22): error C2143: syntax error : missing ';' c:\\ documents and settings \\ fredrich \\ desktop \\ lokalanätverk\\ ovning1 \\ ovning1 \\ ovning.c(22):错误C2143:语法错误:缺少';' before 'type' 在“类型”之前

c:\\documents and settings\\fredrich\\desktop\\lokala nätverk\\ovning1\\ovning1\\ovning.c(23): error C2065: 'message' : undeclared identifier c:\\ documents and settings \\ fredrich \\ desktop \\ lokalanätverk\\ ovning1 \\ ovning1 \\ ovning.c(23):错误C2065:'message':未声明的标识符

c:\\documents and settings\\fredrich\\desktop\\lokala nätverk\\ovning1\\ovning1\\ovning.c(23): warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int' c:\\ documents and settings \\ fredrich \\ desktop \\ lokalanätverk\\ ovning1 \\ ovning1 \\ ovning.c(23):警告C4047:'function':'const char *'的间接引用级别与'int'不同

c:\\documents and settings\\fredrich\\desktop\\lokala nätverk\\ovning1\\ovning1\\ovning.c(23): warning C4024: 'send' : different types for formal and actual parameter 2 c:\\ documents and settings \\ fredrich \\ desktop \\ lokalanätverk\\ ovning1 \\ ovning1 \\ ovning.c(23):警告C4024:“发送”:正式和实际参数2的不同类型

c:\\documents and settings\\fredrich\\desktop\\lokala nätverk\\ovning1\\ovning1\\ovning.c(23): error C2065: 'message' : undeclared identifier c:\\ documents and settings \\ fredrich \\ desktop \\ lokalanätverk\\ ovning1 \\ ovning1 \\ ovning.c(23):错误C2065:'message':未声明的标识符

c:\\documents and settings\\fredrich\\desktop\\lokala nätverk\\ovning1\\ovning1\\ovning.c(23): warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int' c:\\ documents and settings \\ fredrich \\ desktop \\ lokalanätverk\\ ovning1 \\ ovning1 \\ ovning.c(23):警告C4047:'function':'const char *'的间接引用级别与'int'不同

c:\\documents and settings\\fredrich\\desktop\\lokala nätverk\\ovning1\\ovning1\\ovning.c(23): warning C4024: 'strlen' : different types for formal and actual parameter 1 c:\\ documents and settings \\ fredrich \\ desktop \\ lokalanätverk\\ ovning1 \\ ovning1 \\ ovning.c(23):警告C4024:'strlen':正式和实际参数1的不同类型

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== ==========构建:0成功,1失败,0最新,跳过0 ==========

Anyone who knows wy these error occurs? 任何人都知道发生这些错误吗?

您是否未包含stdio.h,该文件中是否包含printf斜度

error C2143: syntax error : missing ';' 错误C2143:语法错误:缺少';' before 'type' 在“类型”之前

If this is C and not C++, you can't declare char * message in the middle of a block. 如果这是C而不是C ++,则不能在块中间声明char * message All variable declarations must be at the start of a block. 所有变量声明必须在块的开头。 (right after the { ). (在{ )。

Should be in the platform SDK http://msdn.microsoft.com/en-us/library/ms740673.aspx 应该在平台SDK中http://msdn.microsoft.com/en-us/library/ms740673.aspx

edit: although as the other answers says - this isn't the problem 编辑:虽然如其他答案所说-这不是问题

At the bottom it says 0 succeeded, 1 failed. 底部显示0成功,1失败。 That just means that one file failed to build. 这仅表示一个文件无法构建。 Scanning down the right hand side, there are errors and warnings from ovning.c. 向下扫描右侧,ovning.c提供了错误和警告。 Start by looking at each error, and fixing it. 首先查看每个错误,然后进行修复。

First error is that printf is undefined. 第一个错误是printf未定义。 Clearly you have either a typo, a missing function or a missing include. 显然,您有错别字,缺少功能或缺少包含。 As Tanuj says, you probably want to use the printf in stdio.h so add the include statement. 正如Tanuj所说,您可能要在stdio.h中使用printf,因此添加include语句。

Next you have two messages about an undeclared identifier. 接下来,您将收到两条有关未声明标识符的消息。 Again, there are probably missing includes. 再次,可能缺少包含。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM