我是做socket的新手,最近做了一個(gè)socket客戶端程序,連接server的時(shí)候,如果server存在,并且允許連接的話,程序無(wú)錯(cuò),正常執(zhí)行;但是如果server不存在,或者拒絕連接,程序就會(huì)卡住,此時(shí)不提示出錯(cuò)。開始我以為是沒有catch異常,但是檢查了程序,異常情況都catch掉了,程序還是卡。
請(qǐng)各位大蝦幫忙指正!謝謝,以下是我這個(gè)模塊的代碼!
using system;
using system.collections;
using system.componentmodel;
using system.net;
using system.net.sockets;
using system.threading;
using system.text;
namespace 測(cè)試程序
{
/// <summary>
/// classclient 的摘要說(shuō)明。
/// </summary>
public class classclient
{
//方法
public classclient()
{
//
// todo: 在此處添加構(gòu)造函數(shù)邏輯
//
}
//函數(shù)
#region socket通信機(jī)連接函數(shù)
/// <summary>
/// socket通信機(jī)連接函數(shù)
/// </summary>
/// <param name="remoteep">遠(yuǎn)程終端</param>
/// <param name="client">建立客戶端</param>
public byte socketconnect(endpoint remoteep, socket client)
{
//調(diào)用系統(tǒng)連接函數(shù)
client.beginconnect(remoteep,new asynccallback(connectcallback), client );
connectdone.waitone();
return(1);
}
#endregion
#region socket連接返回函數(shù)
/// <summary>
/// socket連接返回函數(shù)
/// </summary>
/// <param name="ar">表示異步操作的狀態(tài)</param>
private static void connectcallback(iasyncresult ar)
{
try
{
// 獲取socket連接實(shí)例
socket client = (socket) ar.asyncstate;
// 完成連接過程.
client.endconnect(ar);
// 置位連接完成標(biāo)志
connectdone.set();
// 得到連接成功信息
connectinfo="連接成功!";
}
catch (exception e)
{
// 得到連接失敗信息
connectinfo=e.tostring ();
// 結(jié)束連接
connectdone.reset ();
}
}
#endregion
#region socket通信機(jī)關(guān)閉函數(shù)
/// <summary>
/// socket通信機(jī)關(guān)閉函數(shù)
/// </summary>
/// <param name="client">需關(guān)閉的socket通信實(shí)例</param>
public byte socketclose(socket client)
{
try
{
if (client!=null)
{
//如果仍然產(chǎn)生通信信息,則禁用
if (client.connected)
{
client.shutdown(socketshutdown.both);
}
//關(guān)閉socket通信
client.close();
//獲得關(guān)閉成功信息
closeinfo = "通信機(jī)已關(guān)閉!";
}
return(1);
}
catch (exception e)
{
//獲得關(guān)閉通信失敗信息
closeinfo = e.tostring();
return(0);
}
}
#endregion
#region 數(shù)據(jù)發(fā)送函數(shù)
/// <summary>
/// 數(shù)據(jù)發(fā)送函數(shù)
/// </summary>
/// <param name="client">已連接的socket通信機(jī)實(shí)例(客戶端)</param>
/// <param name="messagesend">需發(fā)送的信息</param>
/// <returns>
/// 返回發(fā)送是否成功值
/// </returns>
public bool socketsend(socket client, string messagesend)
{
//將數(shù)據(jù)轉(zhuǎn)換成byte型ascii碼。
byte[] bytedata = encoding.ascii.getbytes(messagesend);
// 向遠(yuǎn)程設(shè)備(server)發(fā)送數(shù)據(jù).
client.beginsend(bytedata, 0, bytedata.length, socketflags.none,new asynccallback(sendcallback), client);
//發(fā)送標(biāo)志事件等待
senddone.waitone();
//返回真
return(true);
}
#endregion
#region 數(shù)據(jù)發(fā)送返回函數(shù)
/// <summary>
/// 數(shù)據(jù)發(fā)送返回函數(shù)
/// </summary>
/// <param name="ar">表示異步操作的狀態(tài)</param>
private static void sendcallback(iasyncresult ar)
{
try
{
// 獲取socket連接實(shí)例
socket client = (socket) ar.asyncstate;
// 對(duì)遠(yuǎn)程設(shè)備發(fā)送數(shù)據(jù)完成
int bytessent = client.endsend(ar);
//置位發(fā)送完成標(biāo)志
senddone.set();
// 獲得發(fā)送完成信息
sendinfo="發(fā)送已完成!";
}
catch (exception e)
{
//得到發(fā)送失敗信息
sendinfo=e.tostring();
//結(jié)束發(fā)送標(biāo)志
senddone.reset();
}
}
#endregion
#region 數(shù)據(jù)接收函數(shù)
/// <summary>
/// 數(shù)據(jù)接收函數(shù)
/// </summary>
/// <param name="client">已連接的socket通信機(jī)實(shí)例(客戶端)</param>
/// <returns>
/// 返回接收數(shù)據(jù)
/// </returns>
public string socketreceive(socket client)
{
try
{
int i;
// 創(chuàng)建實(shí)例.
stateobject state = new stateobject();
state.worksocket = client;
// 開始從遠(yuǎn)程設(shè)備接收數(shù)據(jù)
client.beginreceive( state.buffer, 0, stateobject.buffersize, 0,new asynccallback(receivecallback), state);
//將接收的byte數(shù)據(jù)轉(zhuǎn)化為字符串
for (i=0;i<state.buffer .length ;i++)
{
revdata += state.buffer[i].tostring ();
}
//返回接收到的數(shù)據(jù)
return(revdata);
}
catch (exception e)
{
//獲得接收失敗信息
revinfo = e.tostring();
//返回空字符串
return("");
}
}
#endregion
#region 數(shù)據(jù)接收返回函數(shù)
/// <summary>
/// 數(shù)據(jù)接收返回函數(shù)
/// </summary>
/// <param name="ar">表示異步操作的狀態(tài)</param>
private static void receivecallback( iasyncresult ar )
{
try
{
// 創(chuàng)建實(shí)例
stateobject state = (stateobject) ar.asyncstate;
socket client = state.worksocket;
// 從遠(yuǎn)程設(shè)備讀取數(shù)據(jù)
int bytesread = client.endreceive(ar);
if (bytesread > 0)
{
// 可能有過多的數(shù)據(jù),先存儲(chǔ)緩沖區(qū)內(nèi)的字符串
revtempstring = encoding.ascii.getstring(state.buffer,0,bytesread);
state.sb.append(revtempstring);
// 接收剩余數(shù)據(jù)
client.beginreceive(state.buffer,0,stateobject.buffersize,0,new asynccallback(receivecallback), state);
}
else
{
// 所有數(shù)據(jù)都已經(jīng)接收
if (state.sb.length > 1)
{
string response = state.sb.tostring();
}
// 置位數(shù)據(jù)已接收標(biāo)志位
receivedone.set();
}
}
catch (exception e)
{
// 獲得接收失敗信息
revinfo=e.tostring();
}
}
#endregion
#region 字段
private static manualresetevent senddone = new manualresetevent(false);
private static manualresetevent connectdone = new manualresetevent(false);
private static manualresetevent receivedone = new manualresetevent(false);
public static string connectinfo = "";
public static string closeinfo = "";
public static string sendinfo = "";
public static string revinfo = "";
public static string revdata = "";
public static string revtempstring = "";
public class stateobject
{
// client socket.
public socket worksocket = null;
// size of receive buffer.
public const int buffersize = 2048;
// receive buffer.
public byte[] buffer = new byte[buffersize];
// received data string.
public stringbuilder sb = new stringbuilder();
}
#endregion
}
}
商業(yè)源碼熱門下載www.html.org.cn
新聞熱點(diǎn)
疑難解答
圖片精選