主要模块: 1:客户端,2:服务端,3:SocketFactory,4:Connection连接对象,5:ConnectionCollection连接管理对象
测试:
/// 服务端 SocketLibrary.SocketFactory factory = new SocketLibrary.SocketFactory(); factory.StartServer(ip1, port1); /// 客户端 SocketLibrary.Connection conn = factory.StartClient(IPAddress.Parse(ip1), port1); SocketLibrary.SocketFactory.SendMessage("我的测试信息", conn);
1:客户端
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; namespace SocketLibrary { /// <summary> /// Socket 客户端 /// </summary> public class Client { // 超时时间,毫秒 public const int CONNECT_TIMEOUT = 10; public Client() { } /// <summary> /// 启动Socket客户端 /// </summary> /// <param name="ipaddress"></param> /// <param name="port"></param> /// <returns></returns> public static Connection StartClient(IPAddress ipaddress, int port) { TcpClient client = new TcpClient(); client.SendTimeout = CONNECT_TIMEOUT; client.ReceiveTimeout = CONNECT_TIMEOUT; client.Connect(ipaddress, port); Connection connection = new Connection(client.GetStream()); return connection; } } }
2:服务端
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Threading; namespace SocketLibrary { /// <summary> /// Socket 服务端 /// </summary> public class Server { private ConnectionCollection connections; public ConnectionCollection Connections { get { return connections; } set { connections = value; } } private TcpListener listener; private Thread listenningthread; public Server(TcpListener listener) { this.connections = new ConnectionCollection(); this.listener = listener; } public void Start() { while (true) { if (listener.Pending()) { TcpClient client = listener.AcceptTcpClient(); NetworkStream stream = client.GetStream(); this.connections.Add(new Connection(stream)); } } } /// <summary> /// 服务器端侦听消息 /// </summary> public void Listenning() { while (true) { Thread.Sleep(200); foreach (Connection connection in this.connections) { if (connection.NetworkStream.CanRead && connection.NetworkStream.DataAvailable) { byte[] buffer = new byte[1024]; int count = connection.NetworkStream.Read(buffer, 0, buffer.Length); Console.Write("================Server 服务器接受到的信息==================" + SocketFactory.encoding.GetString(buffer, 0, count)); } } } } /// <summary> /// 启动服务器监听 /// </summary> public void StartListen() { listenningthread = new Thread(new ThreadStart(Listenning)); listenningthread.Start(); } } }
3:SocketFactory
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Net.Sockets; using System.Net; namespace SocketLibrary { public class SocketFactory { private Thread serverListenThread; public static Encoding encoding = Encoding.GetEncoding("utf-8"); public void StartServer(string ip, int port) { IPAddress ipa = IPAddress.Parse(ip); TcpListener listener = new TcpListener(ipa, port); listener.Start(); Server server = new Server(listener); serverListenThread = new Thread(new ThreadStart(server.Start)); serverListenThread.Start(); server.StartListen(); } public Connection StartClient(IPAddress ip, int port) { return Client.StartClient(ip, port); } /// <summary> /// 发消息给服务器端 /// </summary> /// <param name="message"></param> /// <param name="connection"></param> public static void SendMessage(string message, Connection connection) { byte[] buffer = encoding.GetBytes(message); connection.NetworkStream.Write(buffer, 0, buffer.Length); } /// <summary> /// 获取服务器端返回的消息 /// </summary> /// <param name="message"></param> /// <param name="connection"></param> public static void GetMessage(Connection connection) { // connection.NetworkStream.Length; byte[] buffer = new byte[1024]; connection.NetworkStream.Write(buffer, 0, buffer.Length); } } }
4:Connection连接对象
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; namespace SocketLibrary { public class Connection { private NetworkStream networkStream; public NetworkStream NetworkStream { get { return networkStream; } set { networkStream = value; } } private string connectionName; public string ConnectionName { get { return connectionName; } set { connectionName = value; } } public Connection(NetworkStream networkStream, string connectionName) { this.networkStream = networkStream; this.connectionName = connectionName; } public Connection(NetworkStream networkStream):this(networkStream, string.Empty) { } } }
5:ConnectionCollection连接管理对象
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace SocketLibrary { public class ConnectionCollection : CollectionBase { public ConnectionCollection() { } public void Add(Connection conn) { List.Add(conn); } public Connection this[int index] { get { return List[index] as Connection; } set{ List[index] = value; } } public Connection this[string connectionName] { get { foreach(Connection connection in List) { if(connection.ConnectionName == connectionName) return connection; } return null; } } } }
相关推荐
HP用SOCKET实现(客户端---服务端)通信.doc
例子主要包括SocketAsyncEventArgs通讯封装、服务端实现日志查看、SCOKET列表、上传、下载、远程文件流、吞吐量协议,用于测试SocketAsyncEventArgs的性能和压力,最大连接数支持65535个长连接,最高命令交互速度...
在.NET里面使用Socket通信,该例子书写了一个服务,将服务端所在的地址作为客户端需要进行连接的路径,默认不断的监听相应的端口号,以方便为客户端提供相应的通信服务。 多客户端进行聊天通讯 类似目前所使用的简易...
两者之间使用基于TCP的Socket通信方式。可是,做了一个测试小例子,结果从客户端传过来的都是乱码。于是上网查,希望可以找到解决方法,可是,网上有好多的答案,经过了很多的实验,都不能很好的解决。没办法只能靠...
这个例子只是简单实现了如何使用 Socket 类实现面向连接的通信。 注意:此例子的目的只是为了说明用套接字写程序的大概思路,而不是实际项目中的使用程序。在这个例子中,实际上还有很多问题没有解决,如消息边界...
* Socket网络编程的好例子。 * 分别利用该客户端和服务器,能够监控目前成熟的http产品的通信(当然有其他http嗅探工具)。 注: * 控制台项目,用Visual Studio 2010打开工程(.net 4.0 client profile) * 内有...
答:封装的是HPSocket通信组件,效率和稳定性没得说。 ======================================================================= 6、连接池除了Mysql,其他数据库能用吗? 答:常见的ACCESS、SQLITE3都是单机...
一个开源的高性能IOCP(完成端口)网络通信封装框架《HP-Socket》的使用实例,官方的DEMO只有MFC对话框的版本,对于刚接触这个框架的新手不太容易把代码移植到自己的项目。 此DEMO使用标准C++控制台来实现,对于刚...
socket通信例子。 Matlab作为服务端代码 % CLIENT connect to a server and read a message % Usage - message = client(host, port, number_of_retries) function message = client(host, port, number_of_...
10.2 异步Socket通信——实现MSN机器人 390 10.2.1 机器人服务端 390 10.2.2 客户端实现步骤 395 10.3 基于TCP协议的客户端和服务端 398 10.3.1 TcpListener 实现网络服务端 398 10.3.2 TcpClient实现网络客户端 399...
此例由客户端和服务器端组成,由客户端发送消息,服务器端侦听到马上以弹出窗体形式呈现,代码简单明了 可以实现BS-》CS||CS->CS之间的通信, 可1对1,1对多聊天(待扩展)
10.2 异步Socket通信——实现MSN机器人 390 10.2.1 机器人服务端 390 10.2.2 客户端实现步骤 395 10.3 基于TCP协议的客户端和服务端 398 10.3.1 TcpListener 实现网络服务端 398 10.3.2 TcpClient实现网络客户端 399...
在上面这张图中,你可以理解为程序A和程序B是2个SOCKET程序,服务器端程序A设置端口为81,已接受到3个客户端的连接,计算机C开了2个程序,分别连接到E和D,而他的端口是计算机自动分配的,连接到E的端口为789,连接...
参考MSDN上的Socket例程,以Form为框架,写的关于Socket通信的一个例子,有SocketServer(服务器端)和SocketClient(客户端)两部分,对理解多线程和Socket基本编程有一定的帮助,其中仍有一些地方有待改进。
c/s 聊天例子目的:实现了一个简单的c/s分布式聊天例子,演示了c#客户端和pomelo服务器的交互,使用原生socket通信方式。本来是想用unity3d和pomelo通信,但是unity3d里面处理异步比较麻烦,为了使例子简洁,这里用...
// 在这里得到一个于客户端通信的Session // 注册用于处理收到的消息的handler // 用Session::Push将数据写到发送buffer,最终数据传给client } }; 实现一个App, class App : public Herm::App { ...
Socket是指在一个特定编程模型下,进程间通信链路的端点。因为这个特定编程模型的流行,Socket这个名字在其他领域得到了复用,包括Java叫技术。 如果要建立连接,一台机器必须运行一个进程来等待连接,而另一台机器...
Socket是指在一个特定编程模型下,进程间通信链路的端点。因为这个特定编程模型的流行,Socket这个名字在其他领域得到了复用,包括Java叫技术。 如果要建立连接,一台机器必须运行一个进程来等待连接,而另一台...
java Socket通信实现.rar Java Telnet客户端实例源码 Java zip压缩包查看程序源码.rar java 一个消息发布网站.rar Java 数字签名、数字证书生成源码.rar Java 版的酒店系统,貌似完整.rar Java 电梯模拟程序 v2.0....