c# socket網(wǎng)絡(luò)編程接收發(fā)送數(shù)據(jù)示例代碼
代碼分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();
}
}
![]() |
相關(guān)文章
深入多線程之:Reader與Write Locks(讀寫鎖)的使用詳解
本篇文章是對(duì)Reader與Write Locks(讀寫鎖)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C# Access數(shù)據(jù)庫增刪查改的簡(jiǎn)單方法
這篇文章主要介紹了C# Access數(shù)據(jù)庫增刪查改的簡(jiǎn)單方法,有需要的朋友可以參考一下2014-01-01關(guān)于C#繼承的簡(jiǎn)單應(yīng)用代碼分析
在本篇文章里小編給大家整理了一篇關(guān)于C#繼承的簡(jiǎn)單應(yīng)用代碼分析內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-05-05c# 實(shí)現(xiàn)文件上傳下載功能的實(shí)例代碼
這篇文章主要介紹了如何用c# 實(shí)現(xiàn)文件上傳下載功能,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07C#微信公眾平臺(tái)開發(fā)之高級(jí)群發(fā)接口
這篇文章主要為大家詳細(xì)介紹了C#微信公眾平臺(tái)開發(fā)之高級(jí)群發(fā)接口的相關(guān)資料,需要的朋友可以參考下2016-03-03詳談C# 圖片與byte[]之間以及byte[]與string之間的轉(zhuǎn)換
下面小編就為大家?guī)硪黄斦凜# 圖片與byte[]之間以及byte[]與string之間的轉(zhuǎn)換。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02C#實(shí)現(xiàn)多文件打包壓縮(.Net?Core)
本文詳細(xì)講解了.Net?Core框架下C#實(shí)現(xiàn)多文件打包壓縮的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12