代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
Socket连接监听发送
作者:
dezai
/ 发布于
2014/7/31
/
740
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; using System.Threading; namespace Imps.Services.WebSocketServices { public class ps { static ManualResetEvent clientDone = new ManualResetEvent(false); public static void Demo() { SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs(); IPEndPoint hostEntry = new IPEndPoint(System.Net.IPAddress.Parse("192.168.0.102"), 5001); // Create a socket and connect to the server Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.Bind(hostEntry); sock.Listen(80); socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed); socketEventArg.RemoteEndPoint = hostEntry; socketEventArg.UserToken = sock; sock.AcceptAsync(socketEventArg); //clientDone.WaitOne(); } // A single callback is used for all socket operations. // This method forwards execution on to the correct handler // based on the type of completed operation static void SocketEventArg_Completed(object sender, SocketAsyncEventArgs e) { switch (e.LastOperation) { case SocketAsyncOperation.Connect: ProcessConnect(e); break; case SocketAsyncOperation.Receive: ProcessReceive(e); break; case SocketAsyncOperation.Send: ProcessSend(e); break; default: throw new Exception("Invalid operation completed"); } } // Called when a ConnectAsync operation completes private static void ProcessConnect(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { // Successfully connected to the server // Send 'Hello World' to the server byte[] buffer = Encoding.UTF8.GetBytes("Hello World"); e.SetBuffer(buffer, 0, buffer.Length); Socket sock = e.UserToken as Socket; bool willRaiseEvent = sock.SendAsync(e); if (!willRaiseEvent) { ProcessSend(e); } } else { throw new SocketException((int)e.SocketError); } } // Called when a ReceiveAsync operation completes // </summary> private static void ProcessReceive(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { // Received data from server // Data has now been sent and received from the server. // Disconnect from the server Socket sock = e.UserToken as Socket; sock.Shutdown(SocketShutdown.Send); sock.Close(); clientDone.Set(); } else { throw new SocketException((int)e.SocketError); } } // Called when a SendAsync operation completes private static void ProcessSend(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { // Sent "Hello World" to the server successfully //Read data sent from the server Socket sock = e.UserToken as Socket; bool willRaiseEvent = sock.ReceiveAsync(e); if (!willRaiseEvent) { ProcessReceive(e); } } else { throw new SocketException((int)e.SocketError); } } } }
试试其它关键字
Socket
监听发送
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3