P
pbz
Unregistered / Unconfirmed
GUEST, unregistred user!
用socket api 做了一个客户端程序,用的select模型。在局域网连接,收发收据正常。放在电信的局域网,连接失败,代码跟踪,在connect(m_Socket,(struct sockaddr*)&To,sizeof(struct sockaddr))时候,返回10060,timeout为6000ms。
但用delphi的clientsocket,可以正常连接。
请问这两种方式主要区别是什么?为什么用控件可以连接,api却不可以?
==================================
代码
==================================
char szTmp[256];
int nSize=0;
UINT m_Port;
AnsiString addr;
int m_Protocol;
AnsiString sMessage;
bRec = true;
sMessage = "CReate S. [pre state=" + IntToStr(iState) + "]";
iState = 0;
SKClose();
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
addr = sHost.Trim();
if(addr.IsEmpty())
{
iState = -1;
//::MessageBox(0,"Please enter the server IP!","Error",MB_OK+MB_ICONERROR);
return;
}
unsigned long nAddr = inet_addr(addr.c_str());
if(nAddr==INADDR_NONE)
{
iState = -2;
//::MessageBox(0,"Bad Internet IP!","Error",MB_OK+MB_ICONERROR);
return;}
m_Port = iPort;
m_Protocol = 0;
//Initialize Winsocket
WSAData wsaData;
::ZeroMemory(&wsaData, sizeof(WSAData));
WORD version=MAKEWORD(2, 0);
if:WSAStartup(version,&wsaData))
{
iState = -3;
//ShowMessage(AnsiString(szTmp) + "Failed to initial winsock enviroment!,error no:%d" + ::WSAGetLastError());
return;
}
//Obtain the active connection
char ComputerName[255];
gethostname(ComputerName,255);
struct hostent* he=gethostbyname(ComputerName);
if(!he)
{
iState = -4;
//ShowMessage(AnsiString(szTmp) + "Failed to get information to host!"
::WSACleanup();
return;
}
//create new socket
m_Socket=INVALID_SOCKET;
m_Socket=socket(AF_INET,SOCK_STREAM,0);
if(m_Socket==INVALID_SOCKET)
{
iState = -5;
sMessage = (AnsiString(szTmp) + "Failed to create a new socket!,error no:%d" + ::WSAGetLastError());
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
//::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
::WSACleanup();
return;
}
int TimeOut=6000; //设置发送超时6秒
if:setsockopt(m_Socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&TimeOut, sizeof(TimeOut))==SOCKET_ERROR)
{
iState = -56;
sMessage = ("setsockopt Failed to setsockopt!,error num.:" + ::WSAGetLastError());
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
//::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
::WSACleanup();
return;
}
//bind socket
struct sockaddr_in client;
unsigned long nClient;
memcpy(&nClient,he->h_addr_list[0],sizeof(int));
if(nClient==INADDR_NONE)
{
iState = -6;
sMessage = (AnsiString(szTmp) + "Failed to obtain the local machine's IP!"
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
closesocket(m_Socket);
::WSACleanup();
return;
}
client.sin_family = AF_INET;
client.sin_port = 0;
client.sin_addr.S_un.S_addr = (int)nClient;
if(bind(m_Socket,(struct sockaddr*)&client,sizeof(struct sockaddr)))
{
iState = -7;
sMessage = (AnsiString(szTmp) + "Failed to bind socket!"
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
closesocket(m_Socket);
::WSACleanup();
return;
}
//connect socket
struct sockaddr_in To;
To.sin_family=AF_INET;
To.sin_port=htons(m_Port);
To.sin_addr.S_un.S_addr=(int)nAddr;
fd_set FDSET;
FD_ZERO(&FDSET);
FD_SET(m_Socket, &FDSET);
if(connect(m_Socket,(struct sockaddr*)&To,sizeof(struct sockaddr)))
{
iState = -8;
sMessage = ("Failed to connect the object!,error num.:" +
IntToStr:WSAGetLastError()) + "[ip:" + addr + ", Port:" + IntToStr(iPort) + "]"
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
closesocket(m_Socket);
::WSACleanup();
return;
}
int nError=select(1,0,&FDSET,0,0);
if(nError<=0)
{
iState = -9;
sMessage = (AnsiString(szTmp) + "Failed to select socket!,error no:%d" + WSAGetLastError());
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
::WSACleanup();
return;
}
iState = 1;
Sleep(100);
SendMessage(FormHandle, WM_CONNECTOK, (WPARAM)sMessage.c_str(), 0);
//pClientSocketRead();
但用delphi的clientsocket,可以正常连接。
请问这两种方式主要区别是什么?为什么用控件可以连接,api却不可以?
==================================
代码
==================================
char szTmp[256];
int nSize=0;
UINT m_Port;
AnsiString addr;
int m_Protocol;
AnsiString sMessage;
bRec = true;
sMessage = "CReate S. [pre state=" + IntToStr(iState) + "]";
iState = 0;
SKClose();
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
addr = sHost.Trim();
if(addr.IsEmpty())
{
iState = -1;
//::MessageBox(0,"Please enter the server IP!","Error",MB_OK+MB_ICONERROR);
return;
}
unsigned long nAddr = inet_addr(addr.c_str());
if(nAddr==INADDR_NONE)
{
iState = -2;
//::MessageBox(0,"Bad Internet IP!","Error",MB_OK+MB_ICONERROR);
return;}
m_Port = iPort;
m_Protocol = 0;
//Initialize Winsocket
WSAData wsaData;
::ZeroMemory(&wsaData, sizeof(WSAData));
WORD version=MAKEWORD(2, 0);
if:WSAStartup(version,&wsaData))
{
iState = -3;
//ShowMessage(AnsiString(szTmp) + "Failed to initial winsock enviroment!,error no:%d" + ::WSAGetLastError());
return;
}
//Obtain the active connection
char ComputerName[255];
gethostname(ComputerName,255);
struct hostent* he=gethostbyname(ComputerName);
if(!he)
{
iState = -4;
//ShowMessage(AnsiString(szTmp) + "Failed to get information to host!"
::WSACleanup();
return;
}
//create new socket
m_Socket=INVALID_SOCKET;
m_Socket=socket(AF_INET,SOCK_STREAM,0);
if(m_Socket==INVALID_SOCKET)
{
iState = -5;
sMessage = (AnsiString(szTmp) + "Failed to create a new socket!,error no:%d" + ::WSAGetLastError());
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
//::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
::WSACleanup();
return;
}
int TimeOut=6000; //设置发送超时6秒
if:setsockopt(m_Socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&TimeOut, sizeof(TimeOut))==SOCKET_ERROR)
{
iState = -56;
sMessage = ("setsockopt Failed to setsockopt!,error num.:" + ::WSAGetLastError());
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
//::MessageBox(0,szTmp,"Error",MB_OK+MB_ICONERROR);
::WSACleanup();
return;
}
//bind socket
struct sockaddr_in client;
unsigned long nClient;
memcpy(&nClient,he->h_addr_list[0],sizeof(int));
if(nClient==INADDR_NONE)
{
iState = -6;
sMessage = (AnsiString(szTmp) + "Failed to obtain the local machine's IP!"
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
closesocket(m_Socket);
::WSACleanup();
return;
}
client.sin_family = AF_INET;
client.sin_port = 0;
client.sin_addr.S_un.S_addr = (int)nClient;
if(bind(m_Socket,(struct sockaddr*)&client,sizeof(struct sockaddr)))
{
iState = -7;
sMessage = (AnsiString(szTmp) + "Failed to bind socket!"
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
closesocket(m_Socket);
::WSACleanup();
return;
}
//connect socket
struct sockaddr_in To;
To.sin_family=AF_INET;
To.sin_port=htons(m_Port);
To.sin_addr.S_un.S_addr=(int)nAddr;
fd_set FDSET;
FD_ZERO(&FDSET);
FD_SET(m_Socket, &FDSET);
if(connect(m_Socket,(struct sockaddr*)&To,sizeof(struct sockaddr)))
{
iState = -8;
sMessage = ("Failed to connect the object!,error num.:" +
IntToStr:WSAGetLastError()) + "[ip:" + addr + ", Port:" + IntToStr(iPort) + "]"
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
closesocket(m_Socket);
::WSACleanup();
return;
}
int nError=select(1,0,&FDSET,0,0);
if(nError<=0)
{
iState = -9;
sMessage = (AnsiString(szTmp) + "Failed to select socket!,error no:%d" + WSAGetLastError());
SendMessage(FormHandle, WM_CONNECTERROR, (WPARAM)sMessage.c_str(), 0);
::WSACleanup();
return;
}
iState = 1;
Sleep(100);
SendMessage(FormHandle, WM_CONNECTOK, (WPARAM)sMessage.c_str(), 0);
//pClientSocketRead();