zoukankan
html css js c++ java
自己动手,丰衣足食 之Socket开发
话说“主机已经强制性关闭一个链接”这个问题一直拖延了很久,现在解决了,贴出来希望能购有所参考
using System; using System.Collections; using System.Collections.Specialized; using System.Text; using System.Threading; using System.Net.Sockets; using System.Net; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using System.Data; using System.Windows.Forms; using System.Configuration; using Microsoft.Win32; using System.Diagnostics; using System.Timers; namespace WSGPSGateway { public partial class TcpServer : Form { public TcpServer() { InitializeComponent(); } #region 自定义字段 public static ManualResetEvent allDone = new ManualResetEvent(false); /// <summary> /// 监听控件开启状态 /// </summary> private bool State = true; /// <summary> /// 声明一个线程实例 /// </summary> private Thread mythread; /// <summary> /// 服务器端Ip /// </summary> private int _port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]); /// <summary> /// 保存客户端所有回话的哈希表 /// </summary> private Hashtable _transmit_tb = new Hashtable(); /// <summary> /// 用于接受消息的线程 /// </summary> private Thread _receviccethread = null; public struct TCPParameter { public string Package; public string IpAddress; } #endregion #region 监听代码块 //窗体运行 private void TcpServer_Load(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; this.Opacity = 0; // 窗体透明度 Form.CheckForIllegalCrossThreadCalls = false; InitializeComponent(); mythread = new Thread(Listen); mythread.Start(); System.Timers.Timer atimer = new System.Timers.Timer(); atimer.Elapsed += new System.Timers.ElapsedEventHandler(TimeEvent); atimer.Interval = 1000; atimer.Enabled = true; GC.KeepAlive(atimer); } private object threadlock = new object(); //启动监听 private void BtnStart_Click(object sender, EventArgs e) { //多线程 } //启动监听,轮询监听客户机请求并将客户端套接字存入转发表 private void Listen() { try { IPAddress _ip = IPAddress.Any; Socket newsoc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); newsoc.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); IPEndPoint locaEp = new IPEndPoint(IPAddress.Any, _port);//建立连接 newsoc.Bind(locaEp); newsoc.Listen(100); allDone.Reset(); newsoc.BeginAccept(new AsyncCallback(onCall), newsoc);//继续接受其他客户端的连接 allDone.WaitOne(); } catch (Exception ex) { // } } //监听回调 private void onCall(IAsyncResult ar) { allDone.Set(); Socket serverSoc = (Socket)ar.AsyncState; Socket clent = serverSoc.EndAccept(ar); try { if (serverSoc != null) { byte[] comes = new byte[1024]; EndPoint enp = clent.RemoteEndPoint; serverSoc.BeginAccept(new AsyncCallback(onCall), serverSoc); while (true) { int re = clent.Receive(comes, comes.Length, 0); clent.Send(Encoding.ASCII.GetBytes("8")); TCPParameter parm = new TCPParameter(); parm.Package = Encoding.UTF8.GetString(comes, 0, re).ToString().Trim(); parm.IpAddress = clent.RemoteEndPoint.ToString(); if (parm.Package.Length != 0) { Receive(parm.Package, parm.IpAddress); } } } } catch (SocketException ex) { // } } //处理解析数据 private void Receive(string msg, string ip) { // } #endregion #region 关闭与退出 //窗体关闭 private void TcpServer_FormClosing(object sender, FormClosingEventArgs e) { if (mythread != null) { mythread.Interrupt(); mythread.Abort(); GC.Collect(); } } #endregion } }
查看全文
相关阅读:
Git学习手记(一)
微信开发手记(二)
mysql命令行打开中文乱码
在linux上centos6安装pdo_mysql扩展
在linux上安装php5.6版本提示GD2版本要高于2.1.0解决
在linux上安装apache是出现error1错误
在linux上搭建amp环境安装jpeg时出现libtool command not found,error127解决
在linux中搭建amp环境(apache2.4.7+mysql5.5.23+php5.6.19)
mysql忘记root密码解决办法
移动端网页设计经验与心得
原文地址:https://www.cnblogs.com/javawebsoa/p/2458155.html
最新文章
爬虫使用中间代理人 fiddl...,charles,mitmproxy 设置
关于adsl vps 拨号ip服务器
nohub 将程序永久运行下去
flask 部署外部访问
redis 出现(error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details
京东全站爬取,简单笔记,不涉及代码
scrapy中间件中使用selenium切换ip
scrapy 在爬取过程中抓取下载图片
python 关于函数递归调用自己
关于爬虫个人认为难度很高的两点
热门文章
定义宏
unity3d 加载优化建议 总结 from 侑虎科技
同上两篇 这篇是关于shader的
同上篇 这篇是针对mesh的
转youhu科技的文章 勿怪 感激 感激
转csdn某位同学的 感谢bmfont
cocostudio 1.6
转 unity 优化
Tutorial
这个网站不错
Copyright © 2011-2022 走看看