请问为什么不能运行呢?
#include <stdio.h>#include <stdlib.h>
#include <windows.h>
#include <string.h>
#define HOST_PORT 10000
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
int clientfd = socket(AF_INET, SOCK_STREAM, 0);
if(clientfd < 0)
{
printf("client create socket failed.\n");
return 0;
}
int on = 1;
if(setsockopt(clientfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)) < 0)
{
printf("set socket option failed.\n");
return 0;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(HOST_PORT);
servaddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.56");
char buf[BUFSIZ];
memset(buf,0,BUFSIZ);
strncpy(buf,"shawn's client",14);
//buf[BUFSIZ] = '\0';
if(connect(clientfd,(const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("%d\n",errno);
return 0;
}
int index = 0;
for(index = 0; index < 5; index++)
{
send(clientfd,buf,strlen(buf) + 1, 0);
_sleep(10);
}
strcpy(buf,"end");
send(clientfd,buf,strlen(buf) + 1, 0);
closesocket(clientfd);
getchar();
return 0;
}