#include <windows.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib ")
#define uchar unsigned char
#define DEFAULT_PROTO SOCK_STREAM // TCP
int main(int argc, char* argv[])
{
char server_name[256]= "www.163.com ";
int socket_type = DEFAULT_PROTO;
WSADATA wsaData;
struct sockaddr_insrv_addr;
if (WSAStartup(0x202,&wsaData) == SOCKET_ERROR) {
WSACleanup();
return -1;
}
SOCKET cli_sock=socket(PF_INET,SOCK_STREAM,0);
if (cli_sock==INVALID_SOCKET){
return -1;
}
// connect to server
unsigned short netshort;
if(WSAHtons(cli_sock, DEFAULT_PORT, &netshort))
{
return -1;
}
srv_addr.sin_family = AF_INET;
LPHOSTENT lphost = gethostbyname(server_name);
if (lphost != NULL)
srv_addr.sin_addr.s_addr = ((LPIN_ADDR)lphost-> h_addr)-> s_addr;
srv_addr.sin_port= netshort;
if (connect(cli_sock,(LPSOCKADDR)&srv_addr,sizeof(srv_addr)) ==SOCKET_ERROR){
return -1;
}
char sz[]= "GET / HTTP/1.1\r\nHost:www.163.com\r\n\r\n ";
int retval = send(cli_sock,sz,sizeof(sz)-1,0);
if( SOCKET_ERROR == retval )
return 0;
char buffer[1024]={0};
while( SOCKET_ERROR != retval ){
retval = recv(cli_sock ,buffer,1000,0);
printf( "%s\n ",buffer);
}
closesocket(cli_sock);
return 0;
}
一个很短的socket程序, 在vc++ 2008中编译
但是出现了很多类似于
1>c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(384) : error C2143: 语法错误 : 缺少“}”(在“常量”的前面)
1>c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(132) : error C2011: “fd_set”: “struct”类型重定义
等等问题, 求解.
谢谢