二 C# Socket通信模式
2009-11-04 17:54
381 查看
Socket通信有两种模式:阻塞模式和非阻塞模式。
1:阻塞模式
所谓阻塞模式,就是开启一个线程一直保持侦听状态(通过while(true)循环),这样该线程将一直在这个循环里运行,不会退出,因此该线程将被该循环所阻塞,是为阻塞模式。使用该模式进行通信时,必须开启一个新线程,不能将其置于主线程中,否则主线程什么事都干不了。
阻塞模式通信中又分为两种方式——重连接和持续连接。重连接就是发送端每次发送信息时,重新与接收端进行连接;而持续连接则是发送端初始化时便与接收端进行连接,并且此后一直保持连接。这两种连接方式在编程上的区别主要体现在接收端——对于前者,接收端的Socket accept = listener.Accept()必须写在while(true)循环里面;而对于后者,则必须写在循环外面。这一点对于新手很重要,我开始接触套接字编程时,由于不知道有这么一个区别存在,所以总是不知道错误到底出现在哪里!
下面举例说明阻塞模式下的这两种编程方式。
1.1 阻塞模式之重连接方式
发送端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace DelTest
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace DelTest_Rec
1.2 阻塞模式之重连接方式
这种模式需要注意的是:接收端应用程序必须先开启,然后才能运行发送端程序。
发送端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace DelTest
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace DelTest_Rec
在实际项目中,一般采用持续连接,这是因为这里的套接字所采用的传输协议是TCP/IP协议,当与远程建立连接时,需要经过三次握手,一旦出现异常,通常都会在30秒以后才能确定有没有与远程连接上,因此一般都是一次连接多次使用,而不是使用一次连接一次。一般而言,发送端需要开启一个线程专门与远程保持连接;接收端也开启一个线程专门侦听远程套接字。
2:非阻塞模式
所谓非阻塞模式,就是接收端不使用while循环来一直保持侦听,此时,接收端必须明确知道发送端在什么时间发送套接字,这种模式一般不会使用。下面举一例说明:
发送端:
private void Form1_Load(object sender, EventArgs e)
接收端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace DelTest_Rec
3:Socket编程中注意点
(1)发送端和接收端所使用的编码必须一致;
(2)注意阻塞模式中还存在如本例所说的两种套接字通信方式;
(3)在发送端与远程建立套接字连接之前,必须运行接收端进行侦听,否则将报错;
(4)在项目中需要引入3个空间:using System.Net;
using System.Net.Sockets;
using System.Threading;
1:阻塞模式
所谓阻塞模式,就是开启一个线程一直保持侦听状态(通过while(true)循环),这样该线程将一直在这个循环里运行,不会退出,因此该线程将被该循环所阻塞,是为阻塞模式。使用该模式进行通信时,必须开启一个新线程,不能将其置于主线程中,否则主线程什么事都干不了。
阻塞模式通信中又分为两种方式——重连接和持续连接。重连接就是发送端每次发送信息时,重新与接收端进行连接;而持续连接则是发送端初始化时便与接收端进行连接,并且此后一直保持连接。这两种连接方式在编程上的区别主要体现在接收端——对于前者,接收端的Socket accept = listener.Accept()必须写在while(true)循环里面;而对于后者,则必须写在循环外面。这一点对于新手很重要,我开始接触套接字编程时,由于不知道有这么一个区别存在,所以总是不知道错误到底出现在哪里!
下面举例说明阻塞模式下的这两种编程方式。
1.1 阻塞模式之重连接方式
发送端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace DelTest
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace DelTest_Rec
1.2 阻塞模式之重连接方式
这种模式需要注意的是:接收端应用程序必须先开启,然后才能运行发送端程序。
发送端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace DelTest
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace DelTest_Rec
在实际项目中,一般采用持续连接,这是因为这里的套接字所采用的传输协议是TCP/IP协议,当与远程建立连接时,需要经过三次握手,一旦出现异常,通常都会在30秒以后才能确定有没有与远程连接上,因此一般都是一次连接多次使用,而不是使用一次连接一次。一般而言,发送端需要开启一个线程专门与远程保持连接;接收端也开启一个线程专门侦听远程套接字。
2:非阻塞模式
所谓非阻塞模式,就是接收端不使用while循环来一直保持侦听,此时,接收端必须明确知道发送端在什么时间发送套接字,这种模式一般不会使用。下面举一例说明:
发送端:
private void Form1_Load(object sender, EventArgs e)
接收端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace DelTest_Rec
3:Socket编程中注意点
(1)发送端和接收端所使用的编码必须一致;
(2)注意阻塞模式中还存在如本例所说的两种套接字通信方式;
(3)在发送端与远程建立套接字连接之前,必须运行接收端进行侦听,否则将报错;
(4)在项目中需要引入3个空间:using System.Net;
using System.Net.Sockets;
using System.Threading;
相关文章推荐
- C#和C++结构体Socket通信
- C#基于Socket的CS模式的完整例子
- Unity(C#.net)网络通信问题解决(服务器开启失败,Socket下的“由于目标机器积极拒绝,无法连接”异常)
- C#中Socket编程解决应用程序直接的通信
- C#和C++结构体Socket通信
- SOCKET通信—如何设置成非阻塞模式、该模式下判断connect成功(失败)、判断recv/recvfrom成功(失败)、判断send/sendto成功(失败)
- SOCKET Communication writen in C# (4) Socket 同步模式,异步模式操作
- SOCKET Communication writen in C# (5) Socket 异步操作 模式
- C#中异步Socket通信编程代码实例
- C# 通过socket实现UDP 通信
- C# Socket简单例子(服务器与客户端通信)
- 关于c# TCP SOCKET 通信
- DirectX编程:C#中利用Socket实现网络语音通信[初级版本]
- .net平台下C#socket通信(中)
- 利用C#进行Socket通信编程
- C# Socket简单例子(服务器与客户端通信)
- Unity3D —— Socket通信(C#)
- C#SocketAsyncEventArgs实现高效能多并发TCPSocket通信 (客户端实现)
- socket实现C#和Java之间的通信
- C#的Socket实现UDP协议通信示例代码