c# socket網絡編程接收發(fā)送數據示例代碼
代碼分2塊,server端:
class Program
{
static void Main(string[] args)
{
TcpListener lsner = new TcpListener(9000);
lsner.Start();
Console.WriteLine("started in port: 9000");
while (true)
{
TcpClient client=lsner.AcceptTcpClient();
Console.WriteLine("new client received. hashcode: {0}", client.GetHashCode());
ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessTcpClient), client);
}
Console.ReadKey();
}
private static void ProcessTcpClient(object state)
{
TcpClient client=state as TcpClient;
if(client==null)
Console.WriteLine("client is null");
NetworkStream ns=client.GetStream();
StreamWriter sw = new StreamWriter(ns);
sw.WriteLine("Welcome.");
sw.Flush();
sw.Close();
client.Close();
}
client端:
class Program
{
static void Main(string[] args)
{
IPAddress address = IPAddress.Parse("127.0.0.1");
IPEndPoint ep=new IPEndPoint(address, 9000);
TcpClient client = new TcpClient();
client.Connect(ep);
NetworkStream ns=client.GetStream();
StreamReader sr = new StreamReader(ns);
Console.WriteLine(sr.ReadToEnd());
sr.Close();
sr.Dispose();
ns.Close();
ns.Dispose();
client.Close();
Console.ReadKey();
}
}
![]() |
相關文章
C#中IList<T>與List<T>的區(qū)別深入解析
本篇文章主要是對C#中IList<T>與List<T>的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
C#實現(xiàn)數據導出任一Word圖表的通用呈現(xiàn)方法
應人才測評產品的需求,導出測評報告是其中一個重要的環(huán)節(jié),報告的文件類型也多種多樣,其中WORD輸出也扮演了一個重要的角色,本文給大家介紹了C#實現(xiàn)數據導出任一Word圖表的通用呈現(xiàn)方法及一些體會,需要的朋友可以參考下2023-10-10


