Socket监听
来源:互联网 发布:会计从业资格考试软件 编辑:程序博客网 时间:2024/06/05 13:24
bool isSend = false;
Socket sendSocket;
while (!isSend)
{
sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sendSocket.Connect(SocketIP, SocketPort);
}
catch
{
Error err = new Error();
err.WriteError(0, "/Data QClient /Id " + Num + " /QueueID " + FrmMain.strGueueNo + " /Subscribe " + strType + " ");
isSend = true;
sendSocket.Shutdown(SocketShutdown.Both);
sendSocket.Close();
FrmMsg msg = new FrmMsg("请确认网络是否正常或重新取号!");
msg.ShowDialog();
msg.Close();
msg.Dispose();
GoToLogin();
return;
}
if (sendSocket.Connected)
{
#region 发送数据
Encoding gb2312 = Encoding.GetEncoding("gb2312");
//byte[] buffer = gb2312.GetBytes("/Data QClient /Id Y0004 /QueueID " + FrmMain.strProjectNo + " /Subscribe True ");
byte[] buffer = gb2312.GetBytes("/Data QClient /Id " + Num + " /QueueID " + FrmMain.strGueueNo + " /Subscribe " + strType + " ");
int retc = sendSocket.Send(buffer);
if (retc == -1)
{
Error err = new Error();
err.WriteError(2, "/Data QClient /Id " + Num + " /QueueID " + FrmMain.strGueueNo + " /Subscribe " + strType + " ");
isSend = true;
sendSocket.Shutdown(SocketShutdown.Both);
sendSocket.Close();
FrmMsg msg = new FrmMsg("服务器忙!请重新取号");
msg.ShowDialog();
msg.Close();
msg.Dispose();
GoToLogin();
return;
}
#endregion
#region 获取数据
byte[] inOptionValues = new byte[4 * 3];
BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
BitConverter.GetBytes((uint)1000).CopyTo(inOptionValues, 4);
BitConverter.GetBytes((uint)500).CopyTo(inOptionValues, 8);
sendSocket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
byte[] buffer_Call = new byte[sendSocket.ReceiveBufferSize];
sendSocket.ReceiveTimeout = 10000; //设置超时时间
try
{
int m_read = sendSocket.Receive(buffer_Call, SocketFlags.Partial);
System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");
string str_Read = chs.GetString(buffer_Call, 0, m_read);
string[] str_split = str_Read.Split(',');
if (str_split[0] == "完成" && str_split[1] == strQueueNum) //评价
{
isSend = true;
sendSocket.Shutdown(SocketShutdown.Both);
sendSocket.Close();
}
else
{
Error err = new Error();
err.WriteError(1, "/Data QClient /Id " + Num + " /QueueID " + FrmMain.strGueueNo + " /Subscribe " + strType + " ");
isSend = true;
sendSocket.Shutdown(SocketShutdown.Both);
sendSocket.Close();
FrmMsg msg = new FrmMsg("服务器繁忙!请重新取号");
msg.ShowDialog();
msg.Close();
msg.Dispose();
GoToLogin();
return;
}
}
catch (Exception e) //捕捉超时这个事件g
{
Error err = new Error();
err.WriteError(1, "/Data QClient /Id " + Num + " /QueueID " + FrmMain.strGueueNo + " /Subscribe " + strType + " ");
isSend = true;
sendSocket.Shutdown(SocketShutdown.Both);
sendSocket.Close();
FrmMsg msg = new FrmMsg("请确认网络是否正常或重新取号");
msg.ShowDialog();
msg.Close();
msg.Dispose();
GoToLogin();
return;
}
#endregion
}
0 0
- Socket监听
- 监听socket
- socket监听
- Socket监听 /TcpListener监听/HttpListener 监听
- PHP监听Socket
- 监听socket初始化
- C# socket监听
- android 端监听socket
- Myeclipse+tomcat+socket启动socket监听方法
- socket连接监听udp包
- nginx 监听socket的继承
- http监听socket的初始化
- C# Socket 监听程序:IllusiveServer
- Socket编程之Select()监听
- 【C#-Socket监听消息处理】
- C#-Socket监听消息处理
- C#-Socket监听消息处理
- Socket监听本机端口
- smali文件语法参考
- JavaScript作用域
- storyboard 页面传值跳转
- 在C#开发中学习苦中之乐
- 别名在 update 中的应用
- Socket监听
- Windows核心编程——》第十三章 Windows内存架构(Windows Memory Architecture)
- 域名怎么绑定网站
- Linux开机自动启动svn服务
- 玩转Nodejs日志管理log4js
- 1!+2!+3!+···+n!
- JS获取关闭浏览器事件
- 用importdata() 读取文档
- ubuntu下SVN服务器安装配置