Socket 通信 .net 例子 客户端、本地服务
?
主要模块: 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; } } } }
?
?
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。