扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:程序人生 来源:博客园 2007年11月4日
关键字:
|
1using System; 2 3using System.Net; 4 5using System.Net.Sockets; 6 7 8 9namespace SocketLibrary 10 11{ 12 13 14 15 public class Client 16 17 { 18 19 public const int CONNECTTIMEOUT = 10; 20 21 public Connection _connection; 22 23 public Client() 24 25 { 26 27 28 29 } 30 31 public static Connection StartClient(IPAddress ipaddress,int port) { 32 33 TcpClient client = new TcpClient(); 34 35 client.SendTimeout = CONNECTTIMEOUT; 36 37 client.ReceiveTimeout = CONNECTTIMEOUT; 38 39 40 41 client.Connect(ipaddress,port); 42 43 } 44 45 } 46 47} 48 |
1public Connection StartClient(IPAddress ip,int port) 2 3{ 4 5 return Client.StartClient(ip,port); 6 7} 8 |
|
public static System.Text.Encoding DefaultEncoding = System.Text.Encoding.GetEncoding("GB2312"); public static void SendMessage(string message,Connection connection) { byte[] buffer = DefaultEncoding.GetBytes(message); connection.NetworkStream.Write(buffer,0,buffer.Length); } |
|
public void Listenning() { while(true) { System.Threading.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(SocketFactory.DefaultEncoding.GetString(buffer,0,count)); } } } } 1public void StartListen() { 2 3 _listenningthread = new System.Threading.Thread(new System.Threading.ThreadStart(Listenning)); 4 5 _listenningthread.Start(); 6 7} 8 9private System.Threading.Thread _listenningthread; 10 |
|
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者