採用Socket方式,測試伺服器連線

2021-08-19 19:12:17 字數 2976 閱讀 6338

///

/// 資料庫連線操作,可替換為你自己的程式 

///

/// 連線字串

///

private static bool testconnection(string connectionstring)

catch (exception ex)

return result;

}#region 採用socket方式,測試伺服器連線

///

/// 採用socket方式,測試伺服器連線 

///

/// 伺服器主機名或ip

/// 埠號

///

public static bool testconnection(string host, int port, int millisecondstimeout)

catch (exception e)

finally

}#endregion

private void btnsocket_click(object sender, eventargs e) 

else

messagebox.show("socket link failed","連線伺服器"); 

}data source=(description=(address=(protocol=tcp)(host=)(port=))(connect_data=(service_name=)));user id=;password=

using system;

using system.collections.generic;

using system.data.sqlclient;

using system.linq;

using system.net.sockets;

using system.text;

namespace zt_whitelist_guizhoushifan

catch (exception e)

finally

}///

/// 根據資料庫連線字串判斷連線資料庫是否正確

///

/// 資料庫連線操作

///

public static bool testsqlconnextion(string connectionstring)

catch (exception ex)

return result;

}///

/// 使用config中sql連線判斷是否連線到伺服器並且成功連線資料庫

///

///

///

public static object testsqlserverstart(string sqlserverconnectionsting)

;string serveriplist = sqlserverconnectionsting.split(';');

string tempstringhost = string.empty;

string tempstringport = string.empty;

foreach (var item in serveriplist)

else}}

if (testconnect.testconnection(tempstringhost, int.parse(tempstringport), 60))

;if (testconnect.testsqlconnextion(sqlserverconnectionsting))

returnlist = new object ;

else

returnlist = new object ;

}else;}

return returnlist;

}///

/// 使用config中orca連線判斷是否連線到伺服器並且成功連線資料庫

///

///

///

public static object testorcastart(string orcaconnectionsting)

;orcaconnectionsting = orcaconnectionsting.toupper();

int pos_host = orcaconnectionsting.indexof("host", 0);

int pos_hostend = orcaconnectionsting.indexof(")", pos_host);

string host = orcaconnectionsting.substring(pos_host + 1, pos_hostend - pos_host - 1);

host = host.split('=')[1];

int pos_port = orcaconnectionsting.indexof("port", 0);

int pos_portend = orcaconnectionsting.indexof(")", pos_port);

string port = orcaconnectionsting.substring(pos_port, pos_portend - pos_port - 1);

port = port.split('=')[1];

if (testconnect.testconnection(host, int.parse(port), 60))

;if (testconnect.testsqlconnextion(orcaconnectionsting))

returnlist = new object ;

else

returnlist = new object ;

}else;}

return returnlist;}}

}參考:

20180601增加:

增加異常判斷:

catch (exception ex)

else

}這樣處理起來更舒服點

郵件伺服器採用的DNS伺服器

郵件伺服器採用的dns伺服器字型大小 10pt 香港 ns1.vigator.205.252.144.228 澳門 vassun2.macau.ctm.202.175.3.8 深圳 ns.shenzhen.gd.202.96.134.133 202.96.154.8 202.96.154.15 北京...

伺服器模型 socket

伺服器模型 一 迴圈伺服器 迴圈伺服器在同一時刻只可以相應乙個客戶端請求 二 併發伺服器 併發伺服器在同一時刻可以相應多個客戶端的請求.迴圈伺服器 1.udp伺服器 udp迴圈伺服器的實現非常簡單 udp伺服器每次從套接字上讀取乙個客戶端的請求,處理,然後將結果返回給客戶機.可以用下面的演算法來實現...

socket伺服器端

伺服器 include winsock2.h include string.h include stdio.h include time.h include stdarg.h include stdlib.h pragma comment lib,ws2 32 void errexit const ...