socket为什么连接失败?请看代码
//程序是一个客户端
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <conio.h>
#include <iostream>
using namespace std;
string Str;
//*********************
void SendMsg()
{
char ch;
ch = getchar();
if (ch == '\n')
{
cout<<"RT!"<<endl;
Str = "RT!";
}
while (ch != '\n')
{
Str = Str + ch;
ch = getchar();
}
}
int main(int argc,char** argv)
{
WSADATA wsaData;
SOCKET sClient;
static char* szBuffer;
int ret;
struct sockaddr_in Server;
struct hostent *host = NULL;
string IP;
char ch;
bool bSendOnly = false;
int SocketErrorCode = 0;
szBuffer = new char(0);
if (WSAStartup(MAKEWORD(2,0),&wsaData) != 0)
{
SocketErrorCode=1;
cout<<"Create Socket Error!"<<endl;
return 1;
}
sClient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (sClient == INVALID_SOCKET)
{
cout<<"Create Socket Error!"<<endl;
}
cout<<"Please Input Host IP:"<<endl;
cin>>IP;
Server.sin_family = AF_INET;
Server.sin_port = 5150; //此处以前写成的
Server.sin_addr.s_addr = inet_addr(IP.c_str());
if (connect(sClient,(struct sockaddr*)&Server,sizeof(Server)) == SOCKET_ERROR)
{
cout<<"Connect Server Failed!"<<endl;
//进来了,到此出错 为什么?
}
////以下省略***