我是做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
}
}
商业源码热门下载www.html.org.cn
新闻热点
疑难解答