开发者

Windows sockets problem!

开发者 https://www.devze.com 2023-02-08 21:12 出处:网络
I\'ve started on a school project, and i got some problem when i started to program! This is my code (far from finish):

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?


Haven't you included stdio.h, printf declration is in that file


error C2143: syntax error : missing ';' before 'type'

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 {).


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号