急请教socket连接超时应该如何设置
急急,请问socket连接超时应该怎么设置
我看了http://study.99net.net/study/program/vc/1085476685.html后感觉还是有几个不明白。
运行我的程序后如果IP地址错误,就会有大约20秒的假死状态,请问我该怎么设置延迟。该怎么改代码。
以下是我的代码:
连接类
CClientConnection::CClientConnection()
{
}
CClientConnection::~CClientConnection()
{
shutdown(m_sock, SD_BOTH);
closesocket(m_sock);
m_sock = INVALID_SOCKET;
}
int CClientConnection::run()
{
BOOL value;
if (m_port == -1)
GetConnectDetails();
// Connect if we 're not already connected
if (m_sock == INVALID_SOCKET)
value = Connect();
if (value )
value = SetSocketOptions();
else
return 1;
if (value )
value = NegotiateProtocalVersion();
else
return 2;
if (value )
value = Authenticate();
else
return 3;
if (value )
return 0;
else
return 4;
}
void CClientConnection::init(LPTSTR host, LPTSTR pw)
{
m_sock = INVALID_SOCKET;
_tcsncpy(m_host, host, MAX_HOST_NAME_LEN);
m_port = 5900;
_tcsncpy(m_pswd,pw,20);
}
void CClientConnection::GetConnectDetails()
{
_tcsncpy(m_host, "192.168.3.49 ", MAX_HOST_NAME_LEN);
m_port = 5900;
}
BOOL CClientConnection::Connect()
{
struct sockaddr_in thataddr;
int res;
m_sock = socket(PF_INET, SOCK_STREAM, 0);
if (m_sock == INVALID_SOCKET) throw WarningException(_T( "无法连接 "));
thataddr.sin_addr.s_addr = inet_addr(m_host);
if (thataddr.sin_addr.s_addr == INADDR_NONE) {
LPHOSTENT lphost;
lphost = gethostbyname(m_host);
if (lphost == NULL) {
throw WarningException( "无法连接 ");
};
thataddr.sin_addr.s_addr = ((LPIN_ADDR) lphost-> h_addr)-> s_addr;
};
thataddr.sin_family = AF_INET;
thataddr.sin_port = htons(m_port);
res = connect(m_sock, (LPSOCKADDR) &thataddr, sizeof(thataddr));//设置超时有问题
if (res == SOCKET_ERROR) throw WarningException( "无法与服务端连接! ");
return true;
}
BOOL CClientConnection::SetSocketOptions()
{
BOOL nodelayval = TRUE;
if (setsockopt(m_sock, IPPROTO_TCP, TCP_NODELAY, (const char *) &nodelayval, sizeof(BOOL)))
throw WarningException( "无法连接 ");
return true;
}
//////////////////////////////
主程序调用:
CClientConnection *Client = new CClientConnection();
try{
Client-> init(ip,pw) ;
connected = Client-> run();
switch(connected){
case 0: GetDlgItem(IDC_EDIT2)-> SetWindowText ( "登陆成功 ");break;
case 1: GetDlgItem(IDC_EDIT2)-> SetWindowText ( "连接错误 ");break;
case 2: GetDlgItem(IDC_EDIT2)-> SetWindowText ( "连接错误 ");break;
我看了http://study.99net.net/study/program/vc/1085476685.html后感觉还是有几个不明白。
运行我的程序后如果IP地址错误,就会有大约20秒的假死状态,请问我该怎么设置延迟。该怎么改代码。
以下是我的代码:
连接类
CClientConnection::CClientConnection()
{
}
CClientConnection::~CClientConnection()
{
shutdown(m_sock, SD_BOTH);
closesocket(m_sock);
m_sock = INVALID_SOCKET;
}
int CClientConnection::run()
{
BOOL value;
if (m_port == -1)
GetConnectDetails();
// Connect if we 're not already connected
if (m_sock == INVALID_SOCKET)
value = Connect();
if (value )
value = SetSocketOptions();
else
return 1;
if (value )
value = NegotiateProtocalVersion();
else
return 2;
if (value )
value = Authenticate();
else
return 3;
if (value )
return 0;
else
return 4;
}
void CClientConnection::init(LPTSTR host, LPTSTR pw)
{
m_sock = INVALID_SOCKET;
_tcsncpy(m_host, host, MAX_HOST_NAME_LEN);
m_port = 5900;
_tcsncpy(m_pswd,pw,20);
}
void CClientConnection::GetConnectDetails()
{
_tcsncpy(m_host, "192.168.3.49 ", MAX_HOST_NAME_LEN);
m_port = 5900;
}
BOOL CClientConnection::Connect()
{
struct sockaddr_in thataddr;
int res;
m_sock = socket(PF_INET, SOCK_STREAM, 0);
if (m_sock == INVALID_SOCKET) throw WarningException(_T( "无法连接 "));
thataddr.sin_addr.s_addr = inet_addr(m_host);
if (thataddr.sin_addr.s_addr == INADDR_NONE) {
LPHOSTENT lphost;
lphost = gethostbyname(m_host);
if (lphost == NULL) {
throw WarningException( "无法连接 ");
};
thataddr.sin_addr.s_addr = ((LPIN_ADDR) lphost-> h_addr)-> s_addr;
};
thataddr.sin_family = AF_INET;
thataddr.sin_port = htons(m_port);
res = connect(m_sock, (LPSOCKADDR) &thataddr, sizeof(thataddr));//设置超时有问题
if (res == SOCKET_ERROR) throw WarningException( "无法与服务端连接! ");
return true;
}
BOOL CClientConnection::SetSocketOptions()
{
BOOL nodelayval = TRUE;
if (setsockopt(m_sock, IPPROTO_TCP, TCP_NODELAY, (const char *) &nodelayval, sizeof(BOOL)))
throw WarningException( "无法连接 ");
return true;
}
//////////////////////////////
主程序调用:
CClientConnection *Client = new CClientConnection();
try{
Client-> init(ip,pw) ;
connected = Client-> run();
switch(connected){
case 0: GetDlgItem(IDC_EDIT2)-> SetWindowText ( "登陆成功 ");break;
case 1: GetDlgItem(IDC_EDIT2)-> SetWindowText ( "连接错误 ");break;
case 2: GetDlgItem(IDC_EDIT2)-> SetWindowText ( "连接错误 ");break;