国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > 綜合 > 正文

請(qǐng)教如何改善C#中socket通信機(jī)客戶端程序的健壯性

2024-07-21 02:17:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
 

我是做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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 莱阳市| 遵义市| 长子县| 临颍县| 田东县| 达孜县| 江永县| 公安县| 水富县| 增城市| 宿松县| 北辰区| 株洲市| 大化| 廉江市| 中江县| 崇仁县| 丽江市| 伊通| 休宁县| 阳原县| 乌什县| 荃湾区| 宽城| 张家川| 正镶白旗| 泰来县| 中阳县| 甘肃省| 香格里拉县| 黄石市| 扎赉特旗| 健康| 蓬莱市| 林口县| 信宜市| 樟树市| 南安市| 五常市| 保山市| 大丰市|