Windows 套接字问题!
我已经开始了一个学校项目,当我开始编程时遇到了一些问题!这是我的代码(远未完成):
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();
包含文件是“winsock2.h”和“Ws2tcpip.h”!当我尝试编译程序时,我得到以下输出:
------ 构建开始:项目:Ovning1,配置:调试 Win32 ------
Ovning.c
c:\documents and settings\fredrich \desktop\lokala nätverk\ovning1\ovning1\ovning.c(16): 警告 C4013: 'printf' 未定义;假设 extern 返回 int
c:\documents and settings\fredrich\desktop\lokala nätverk\ovning1\ovning1\ovning.c(22): 错误 C2143: 语法错误: 缺少 ';'在“键入”之前
c:\documents and settings\fredrich\desktop\lokala nätverk\ovning1\ovning1\ovning.c(23): error C2065: 'message' : 未声明的标识符
c:\documents and settings\fredrich\desktop\lokala 间接级别不同
nä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): 警告 C4024: 'send' : 形式参数和实际参数 2
c:\documents and settings\fredrich\desktop\lokala nätverk\ovning1\ovning1\ovning.c(23): 错误C2065: 'message' : 未声明的标识符
c:\documents and settings\fredrich\desktop\lokala nä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' : 形式参数和实际参数 1 的不同类型
====== ==== 构建:0 成功,1 失败,0 最新,0 跳过 ==========
有人知道这些错误发生的原因吗?
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"! When i try to compilate the program I got this output:
------ Build started: Project: Ovning1, Configuration: Debug Win32 ------
Ovning.c
c:\documents and settings\fredrich\desktop\lokala nätverk\ovning1\ovning1\ovning.c(16): warning C4013: 'printf' undefined; assuming extern returning int
c:\documents and settings\fredrich\desktop\lokala nätverk\ovning1\ovning1\ovning.c(22): error C2143: syntax error : missing ';' 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\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\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\lokala nätverk\ovning1\ovning1\ovning.c(23): error C2065: 'message' : undeclared identifier
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\lokala nätverk\ovning1\ovning1\ovning.c(23): warning C4024: 'strlen' : different types for formal and actual parameter 1
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Anyone who knows wy these error occurs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你没有包含stdio.h吗,printf声明在该文件中
Haven't you included stdio.h, printf declration is in that file
如果这是 C 而不是 C++,则不能在块中间声明
char * message
。所有变量声明必须位于块的开头。 (就在{
之后)。If this is C and not C++, you can't declare
char * message
in the middle of a block. All variable declarations must be at the start of a block. (right after the{
).应该在平台SDK中
http://msdn.microsoft.com/en-us/library/ms740673.aspx
编辑:虽然正如其他答案所说 - 这不是问题
Should be in the platform SDK
http://msdn.microsoft.com/en-us/library/ms740673.aspx
edit: although as the other answers says - this isn't the problem
底部显示 0 成功,1 失败。这仅意味着一个文件无法构建。向下扫描右侧,可以看到来自 ovning.c 的错误和警告。首先查看每个错误并修复它。
第一个错误是 printf 未定义。显然你有一个错字、一个缺少的功能或者一个缺少的包含。正如 Tanuj 所说,您可能想在 stdio.h 中使用 printf,因此添加 include 语句。
接下来您有两条关于未声明标识符的消息。同样,可能缺少包含内容。
At the bottom it says 0 succeeded, 1 failed. That just means that one file failed to build. Scanning down the right hand side, there are errors and warnings from ovning.c. Start by looking at each error, and fixing it.
First error is that printf is undefined. 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.
Next you have two messages about an undeclared identifier. Again, there are probably missing includes.