选择显示字体大小

改善c#中socket通信机客户端程序的健壮性

    我是做socket的新手,最近做了一个socket客户端程序,连接server的时候,如果server存在,并且允许连接的话,程序无错,正常执行;但是如果server不存在,或者拒绝连接,程序就会卡住,此时不提示出错。开始我以为是没有catch异常,但是检查了程序,异常情况都catch掉了,程序还是卡。

请各位大虾帮忙指正!谢谢,以下是我这个模块的代码!

using system;
using system.collections;
using system.componentmodel;
using system.net;
using system.net.sockets;
using system.threading;
using system.text;


namespace 测试程序
{
 /// <summary>
 /// classclient 的摘要说明。
 /// </summary>
 public class classclient
 {
  //方法
  public classclient()
  {
   //
   // todo: 在此处添加构造函数逻辑
   //
  }

  //函数

  #region socket通信机连接函数
  /// <summary>
  /// socket通信机连接函数
  /// </summary>
  /// <param name="remoteep">远程终端</param>
  /// <param name="client">建立客户端</param>
  public  byte socketconnect(endpoint remoteep, socket client)
  {
   //调用系统连接函数
   client.beginconnect(remoteep,new asynccallback(connectcallback), client );
           
   connectdone.waitone();
   
   return(1);

   
  }
  #endregion

  #region socket连接返回函数
  /// <summary>
  /// socket连接返回函数
  /// </summary>
  /// <param name="ar">表示异步操作的状态</param>
  private static void connectcallback(iasyncresult ar)
  {
   try
   {
    // 获取socket连接实例
    socket client = (socket) ar.asyncstate;

    // 完成连接过程.
    client.endconnect(ar);

    // 置位连接完成标志
    connectdone.set();
    
    // 得到连接成功信息
    connectinfo="连接成功!";
   }
   catch (exception e)
   {
    // 得到连接失败信息
    connectinfo=e.tostring ();

    // 结束连接
    connectdone.reset ();
   }
  }
  #endregion

  #region socket通信机关闭函数
  /// <summary>
  /// socket通信机关闭函数
  /// </summary>
  /// <param name="client">需关闭的socket通信实例</param>
  public byte socketclose(socket client)
  {
   try
   {
    if (client!=null)
    {
     //如果仍然产生通信信息,则禁用
     if (client.connected) 
     {
      client.shutdown(socketshutdown.both);
     }
     //关闭socket通信
     client.close();

     //获得关闭成功信息
     closeinfo = "通信机已关闭!";
    }
    return(1);
   }
   catch (exception e)
   {
    //获得关闭通信失败信息
    closeinfo = e.tostring();

    return(0);
   }
  }
  #endregion

  #region 数据发送函数
  /// <summary>
  /// 数据发送函数
  /// </summary>
  /// <param name="client">已连接的socket通信机实例(客户端)</param>
  /// <param name="messagesend">需发送的信息</param>
  /// <returns>
  /// 返回发送是否成功值
  /// </returns>
  public bool socketsend(socket client, string messagesend)
  {
   //将数据转换成byte型ascii码。
   byte[] bytedata = encoding.ascii.getbytes(messagesend);

   // 向远程设备(server)发送数据.
   client.beginsend(bytedata, 0, bytedata.length, socketflags.none,new asynccallback(sendcallback), client);
           
   //发送标志事件等待
   senddone.waitone();
           
   //返回真
   return(true);
  }
  #endregion



  #region 数据发送返回函数
  /// <summary>
  /// 数据发送返回函数
  /// </summary>
  /// <param name="ar">表示异步操作的状态</param>
  private static void sendcallback(iasyncresult ar)
  {
   try
   {
    // 获取socket连接实例
    socket client = (socket) ar.asyncstate;

    // 对远程设备发送数据完成
    int bytessent = client.endsend(ar);

    //置位发送完成标志
    senddone.set();

    // 获得发送完成信息
    sendinfo="发送已完成!";

   }
   catch (exception e)
   {
    //得到发送失败信息
    sendinfo=e.tostring();

    //结束发送标志
    senddone.reset();
   }
  }

  #endregion

  #region 数据接收函数
  /// <summary>
  /// 数据接收函数
  /// </summary>
  /// <param name="client">已连接的socket通信机实例(客户端)</param>
  /// <returns>
  /// 返回接收数据
  /// </returns>
  public string socketreceive(socket client)
  { 
   try
   {
    int i;

    // 创建实例.
    stateobject state = new stateobject();
    state.worksocket = client;
    

    // 开始从远程设备接收数据
    client.beginreceive( state.buffer, 0, stateobject.buffersize, 0,new asynccallback(receivecallback), state);

    //将接收的byte数据转化为字符串
    for (i=0;i<state.buffer .length ;i++)
    {
     revdata += state.buffer[i].tostring ();
    }
    
    //返回接收到的数据
    return(revdata);
   }
   catch (exception e)
   {
    //获得接收失败信息
    revinfo = e.tostring();

    //返回空字符串
    return("");
   }
  }
  #endregion

  #region 数据接收返回函数
  /// <summary>
  /// 数据接收返回函数
  /// </summary>
  /// <param name="ar">表示异步操作的状态</param>
  private static void receivecallback( iasyncresult ar )
  {
   try
   {
    // 创建实例
    stateobject state = (stateobject) ar.asyncstate;
    socket client = state.worksocket;

    // 从远程设备读取数据
    int bytesread = client.endreceive(ar);

    if (bytesread > 0)
    {
     // 可能有过多的数据,先存储缓冲区内的字符串
     revtempstring = encoding.ascii.getstring(state.buffer,0,bytesread);
     state.sb.append(revtempstring);

     // 接收剩余数据
     client.beginreceive(state.buffer,0,stateobject.buffersize,0,new asynccallback(receivecallback), state);
    }
    else
    {
     // 所有数据都已经接收
     if (state.sb.length > 1)
     {
      string response = state.sb.tostring();
     }

     // 置位数据已接收标志位
     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
  
 }
}



  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons