【C#食谱】【杭帮菜】菜单2:写一个TCP客户端
生活随笔
收集整理的這篇文章主要介紹了
【C#食谱】【杭帮菜】菜单2:写一个TCP客户端
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
問題:
你想連接基于TCP的服務(wù)端。
解決方法:
使用System.Net.TcpClient類,通過給服務(wù)端傳遞地址和端口來和服務(wù)端建立連接和會話。下面這個例子將和上一菜單中的服務(wù)端進(jìn)行會話。
class?MyTcpClient
{
????private?TcpClient?_client?=?null;
????private?IPAddress?_address;
????private?int?_port;
????private?IPEndPoint?_endPoint?=?null;
????public?MyTcpClient(IPAddress?address,?int?port)
????{
????????_address?=?address;
????????_port?=?port;
????????_endPoint?=?new?IPEndPoint(_address,?_port);
????}
????public?void?ConnectToServer(string?msg)
????{
????????try
????????{
????????????_client?=?new?TcpClient();
????????????_client.Connect(_endPoint);
????????????// 獲取將要發(fā)生的消息
????????????byte[]?bytes?=?Encoding.ASCII.GetBytes(msg);
????????????// 獲取和服務(wù)端會話的流
????????????using?(NetworkStream?ns?=?_client.GetStream())
????????????{
????????????????// 發(fā)送消息
????????????????Trace.WriteLine("Sending?message?to?server:?"?+?msg);
????????????????ns.Write(bytes,?0,?bytes.Length);
????????????????// 獲得響應(yīng)
????????????????// 存儲響應(yīng)內(nèi)容的緩存
????????????????bytes?=?new?byte[1024];
????????????????// 顯示響應(yīng)內(nèi)容
????????????????int?bytesRead?=?ns.Read(bytes,?0,?bytes.Length);
????????????????string?serverResponse?=?Encoding.ASCII.GetString(bytes,?0,?bytesRead);
????????????????Trace.WriteLine("Server?said:?"?+?serverResponse);
????????????}
????????}
????????catch?(SocketException?se)
????????{
????????????Trace.WriteLine("There?was?an?error?talking?to?the?server:?"?+
????????????????se.ToString());
????????}
????????finally
????????{
????????????// 關(guān)閉所有的東西
????????????if(_client?!=?null)?
????????????????_client.Close();
????????}
????}
} 要使用這個類,你可以簡單的創(chuàng)建一個它的實例,然后通過調(diào)用ConnectToServer來發(fā)送請求。在這個程序中,你首先呼叫了服務(wù)端三次,來測試基本的機制。接著,你進(jìn)入一個循環(huán)來真正的“重?fù)簟狈?wù)端,使得請求數(shù)超過服務(wù)端默認(rèn)的線程池容量。這就可以證明服務(wù)端在處理多個請求的機制是可靠的。
????static?void?Main(string[]?args)
????{
????????MakeClientCallToServer("Just?wanted?to?say?hi");
????????MakeClientCallToServer("Just?wanted?to?say?hi?again");
????????MakeClientCallToServer("Are?you?ignoring?me?");
????????// 現(xiàn)在發(fā)送一堆消息
????????string?msg;
????????for?(int?i?=?0;?i?<?100;?i++)
????????{
????????????msg?=?string.Format("I'll?not?be?ignored!?(round?{0})",?i);?
????????????ThreadPool.QueueUserWorkItem(new?WaitCallback(MakeClientCallToServer),?msg);
????????}
????????Console.WriteLine("\n?Press?any?key?to?continue…?(if?you?can?find?it…)");
????????Console.Read();?
????}
????static?void?MakeClientCallToServer(object?objMsg)
????{
????????string?msg?=?(string)objMsg;
????????MyTcpClient?client?=?new?MyTcpClient(IPAddress.Loopback,55555);
????????client.ConnectToServer(msg);
????} 在客戶端的輸出是這樣的:
Sending?message?to?server:?Just?wanted?to?say?hi
Server?said:?Thanks?call?again!
Sending?message?to?server:?Just?wanted?to?say?hi?again
Server?said:?Thanks?call?again!
Sending?message?to?server:?Are?you?ignoring?me?
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?0)
Sending?message?to?server:?I'll?not?be?ignored!?(round?1)
Sending?message?to?server:?I'll?not?be?ignored!?(round?2)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?3)
Sending?message?to?server:?I'll?not?be?ignored!?(round?4)
Sending?message?to?server:?I'll?not?be?ignored!?(round?5)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?6)
Sending?message?to?server:?I'll?not?be?ignored!?(round?7)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?8)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?9)
Sending?message?to?server:?I'll?not?be?ignored!?(round?10)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?11)
Sending?message?to?server:?I'll?not?be?ignored!?(round?12)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?13)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?14)
Sending?message?to?server:?I'll?not?be?ignored!?(round?15)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?16)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?17)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?18)
Sending?message?to?server:?I'll?not?be?ignored!?(round?19)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?20)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?21)
Sending?message?to?server:?I'll?not?be?ignored!?(round?22)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?23)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?24)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?25)
Sending?message?to?server:?I'll?not?be?ignored!?(round?26)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?27)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?28)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?29)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?30)
Sending?message?to?server:?I'll?not?be?ignored!?(round?31)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?32)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?33)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?34)
Sending?message?to?server:?I'll?not?be?ignored!?(round?35)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?36)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?37)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?38)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?39)
Sending?message?to?server:?I'll?not?be?ignored!?(round?40)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?41)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?42)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?43)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?44)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?45)
Sending?message?to?server:?I'll?not?be?ignored!?(round?46)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?47)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?48)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?49)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?50)
Sending?message?to?server:?I'll?not?be?ignored!?(round?51)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?52)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?53)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?54)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?55)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?56)
Sending?message?to?server:?I'll?not?be?ignored!?(round?57)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?58)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?59)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?60)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?61)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?62)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?63)
Sending?message?to?server:?I'll?not?be?ignored!?(round?64)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?65)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?66)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?67)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?68)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?69)
Sending?message?to?server:?I'll?not?be?ignored!?(round?70)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?71)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?72)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?73)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?74)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?75)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?76)
Sending?message?to?server:?I'll?not?be?ignored!?(round?77)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?78)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?79)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?80)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?81)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?82)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?83)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?84)
Sending?message?to?server:?I'll?not?be?ignored!?(round?85)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?86)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?87)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?88)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?89)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?90)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?91)
Sending?message?to?server:?I'll?not?be?ignored!?(round?92)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?93)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?94)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?95)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?96)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?97)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?98)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?99)
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
線程?0xb10?已退出,返回值為?0?(0x0)。
線程?'<無名稱>'?(0x344)?已退出,返回值為?0?(0x0)。
線程?0xe90?已退出,返回值為?0?(0x0)。
線程?'<無名稱>'?(0xb64)?已退出,返回值為?0?(0x0)。
線程?0x73c?已退出,返回值為?0?(0x0)。
線程?0xd9c?已退出,返回值為?0?(0x0)。
線程?'<無名稱>'?(0xdf8)?已退出,返回值為?0?(0x0)。
線程?0xe8c?已退出,返回值為?0?(0x0)。
線程?0x204?已退出,返回值為?0?(0x0)。
線程?0xc54?已退出,返回值為?0?(0x0)。
線程?0xad0?已退出,返回值為?0?(0x0)。
線程?'<無名稱>'?(0xd34)?已退出,返回值為?0?(0x0)。
線程?0xce8?已退出,返回值為?0?(0x0)。
線程?0xe88?已退出,返回值為?0?(0x0)。
線程?0xb6c?已退出,返回值為?0?(0x0)。
線程?0xaa8?已退出,返回值為?0?(0x0)。
線程?0xf4?已退出,返回值為?0?(0x0)。
線程?0xae4?已退出,返回值為?0?(0x0)。
線程?0x6f8?已退出,返回值為?0?(0x0)。
線程?'<無名稱>'?(0x6c4)?已退出,返回值為?0?(0x0)。
線程?0xcfc?已退出,返回值為?0?(0x0)。
線程?0x504?已退出,返回值為?0?(0x0)。 討論:
MyTcpClient.ConnectToServer是被設(shè)計用來發(fā)送一條消息,獲得響應(yīng),顯示響應(yīng)的內(nèi)容,然后關(guān)閉連接。為了完成這些,它創(chuàng)建一個System.Net.TcpClient并通過調(diào)用TcpClient.Connect連接到服務(wù)端。Connect通過IPEndPoint定位到服務(wù)端,IPEndPoint是根據(jù)你傳遞給MyTcpClient的構(gòu)造函數(shù)的參數(shù)(IP地址和端口)確定的。
MyTcpClient.ConnectToServer然后用Encoding.ASCII.GetBytes從string中取得bytes。一旦有bytes要發(fā)送,它通過調(diào)用System.Net.TcpClient的GetStream方法來獲取NetworkStream,然后通過TcpClient.Write方法來發(fā)送消息。
為了中服務(wù)端收到響應(yīng),阻塞函數(shù)TcpClient.Read被調(diào)用。然后連接被關(guān)閉,客戶端結(jié)束。
寫一個TCP服務(wù)端
你想連接基于TCP的服務(wù)端。
解決方法:
使用System.Net.TcpClient類,通過給服務(wù)端傳遞地址和端口來和服務(wù)端建立連接和會話。下面這個例子將和上一菜單中的服務(wù)端進(jìn)行會話。
class?MyTcpClient
{
????private?TcpClient?_client?=?null;
????private?IPAddress?_address;
????private?int?_port;
????private?IPEndPoint?_endPoint?=?null;
????public?MyTcpClient(IPAddress?address,?int?port)
????{
????????_address?=?address;
????????_port?=?port;
????????_endPoint?=?new?IPEndPoint(_address,?_port);
????}
????public?void?ConnectToServer(string?msg)
????{
????????try
????????{
????????????_client?=?new?TcpClient();
????????????_client.Connect(_endPoint);
????????????// 獲取將要發(fā)生的消息
????????????byte[]?bytes?=?Encoding.ASCII.GetBytes(msg);
????????????// 獲取和服務(wù)端會話的流
????????????using?(NetworkStream?ns?=?_client.GetStream())
????????????{
????????????????// 發(fā)送消息
????????????????Trace.WriteLine("Sending?message?to?server:?"?+?msg);
????????????????ns.Write(bytes,?0,?bytes.Length);
????????????????// 獲得響應(yīng)
????????????????// 存儲響應(yīng)內(nèi)容的緩存
????????????????bytes?=?new?byte[1024];
????????????????// 顯示響應(yīng)內(nèi)容
????????????????int?bytesRead?=?ns.Read(bytes,?0,?bytes.Length);
????????????????string?serverResponse?=?Encoding.ASCII.GetString(bytes,?0,?bytesRead);
????????????????Trace.WriteLine("Server?said:?"?+?serverResponse);
????????????}
????????}
????????catch?(SocketException?se)
????????{
????????????Trace.WriteLine("There?was?an?error?talking?to?the?server:?"?+
????????????????se.ToString());
????????}
????????finally
????????{
????????????// 關(guān)閉所有的東西
????????????if(_client?!=?null)?
????????????????_client.Close();
????????}
????}
} 要使用這個類,你可以簡單的創(chuàng)建一個它的實例,然后通過調(diào)用ConnectToServer來發(fā)送請求。在這個程序中,你首先呼叫了服務(wù)端三次,來測試基本的機制。接著,你進(jìn)入一個循環(huán)來真正的“重?fù)簟狈?wù)端,使得請求數(shù)超過服務(wù)端默認(rèn)的線程池容量。這就可以證明服務(wù)端在處理多個請求的機制是可靠的。
????static?void?Main(string[]?args)
????{
????????MakeClientCallToServer("Just?wanted?to?say?hi");
????????MakeClientCallToServer("Just?wanted?to?say?hi?again");
????????MakeClientCallToServer("Are?you?ignoring?me?");
????????// 現(xiàn)在發(fā)送一堆消息
????????string?msg;
????????for?(int?i?=?0;?i?<?100;?i++)
????????{
????????????msg?=?string.Format("I'll?not?be?ignored!?(round?{0})",?i);?
????????????ThreadPool.QueueUserWorkItem(new?WaitCallback(MakeClientCallToServer),?msg);
????????}
????????Console.WriteLine("\n?Press?any?key?to?continue…?(if?you?can?find?it…)");
????????Console.Read();?
????}
????static?void?MakeClientCallToServer(object?objMsg)
????{
????????string?msg?=?(string)objMsg;
????????MyTcpClient?client?=?new?MyTcpClient(IPAddress.Loopback,55555);
????????client.ConnectToServer(msg);
????} 在客戶端的輸出是這樣的:
Sending?message?to?server:?Just?wanted?to?say?hi
Server?said:?Thanks?call?again!
Sending?message?to?server:?Just?wanted?to?say?hi?again
Server?said:?Thanks?call?again!
Sending?message?to?server:?Are?you?ignoring?me?
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?0)
Sending?message?to?server:?I'll?not?be?ignored!?(round?1)
Sending?message?to?server:?I'll?not?be?ignored!?(round?2)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?3)
Sending?message?to?server:?I'll?not?be?ignored!?(round?4)
Sending?message?to?server:?I'll?not?be?ignored!?(round?5)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?6)
Sending?message?to?server:?I'll?not?be?ignored!?(round?7)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?8)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?9)
Sending?message?to?server:?I'll?not?be?ignored!?(round?10)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?11)
Sending?message?to?server:?I'll?not?be?ignored!?(round?12)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?13)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?14)
Sending?message?to?server:?I'll?not?be?ignored!?(round?15)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?16)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?17)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?18)
Sending?message?to?server:?I'll?not?be?ignored!?(round?19)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?20)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?21)
Sending?message?to?server:?I'll?not?be?ignored!?(round?22)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?23)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?24)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?25)
Sending?message?to?server:?I'll?not?be?ignored!?(round?26)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?27)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?28)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?29)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?30)
Sending?message?to?server:?I'll?not?be?ignored!?(round?31)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?32)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?33)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?34)
Sending?message?to?server:?I'll?not?be?ignored!?(round?35)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?36)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?37)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?38)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?39)
Sending?message?to?server:?I'll?not?be?ignored!?(round?40)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?41)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?42)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?43)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?44)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?45)
Sending?message?to?server:?I'll?not?be?ignored!?(round?46)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?47)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?48)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?49)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?50)
Sending?message?to?server:?I'll?not?be?ignored!?(round?51)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?52)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?53)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?54)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?55)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?56)
Sending?message?to?server:?I'll?not?be?ignored!?(round?57)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?58)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?59)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?60)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?61)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?62)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?63)
Sending?message?to?server:?I'll?not?be?ignored!?(round?64)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?65)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?66)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?67)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?68)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?69)
Sending?message?to?server:?I'll?not?be?ignored!?(round?70)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?71)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?72)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?73)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?74)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?75)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?76)
Sending?message?to?server:?I'll?not?be?ignored!?(round?77)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?78)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?79)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?80)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?81)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?82)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?83)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?84)
Sending?message?to?server:?I'll?not?be?ignored!?(round?85)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?86)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?87)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?88)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?89)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?90)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?91)
Sending?message?to?server:?I'll?not?be?ignored!?(round?92)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?93)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?94)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?95)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?96)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?97)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?98)
Server?said:?Thanks?call?again!
Sending?message?to?server:?I'll?not?be?ignored!?(round?99)
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
Server?said:?Thanks?call?again!
線程?0xb10?已退出,返回值為?0?(0x0)。
線程?'<無名稱>'?(0x344)?已退出,返回值為?0?(0x0)。
線程?0xe90?已退出,返回值為?0?(0x0)。
線程?'<無名稱>'?(0xb64)?已退出,返回值為?0?(0x0)。
線程?0x73c?已退出,返回值為?0?(0x0)。
線程?0xd9c?已退出,返回值為?0?(0x0)。
線程?'<無名稱>'?(0xdf8)?已退出,返回值為?0?(0x0)。
線程?0xe8c?已退出,返回值為?0?(0x0)。
線程?0x204?已退出,返回值為?0?(0x0)。
線程?0xc54?已退出,返回值為?0?(0x0)。
線程?0xad0?已退出,返回值為?0?(0x0)。
線程?'<無名稱>'?(0xd34)?已退出,返回值為?0?(0x0)。
線程?0xce8?已退出,返回值為?0?(0x0)。
線程?0xe88?已退出,返回值為?0?(0x0)。
線程?0xb6c?已退出,返回值為?0?(0x0)。
線程?0xaa8?已退出,返回值為?0?(0x0)。
線程?0xf4?已退出,返回值為?0?(0x0)。
線程?0xae4?已退出,返回值為?0?(0x0)。
線程?0x6f8?已退出,返回值為?0?(0x0)。
線程?'<無名稱>'?(0x6c4)?已退出,返回值為?0?(0x0)。
線程?0xcfc?已退出,返回值為?0?(0x0)。
線程?0x504?已退出,返回值為?0?(0x0)。 討論:
MyTcpClient.ConnectToServer是被設(shè)計用來發(fā)送一條消息,獲得響應(yīng),顯示響應(yīng)的內(nèi)容,然后關(guān)閉連接。為了完成這些,它創(chuàng)建一個System.Net.TcpClient并通過調(diào)用TcpClient.Connect連接到服務(wù)端。Connect通過IPEndPoint定位到服務(wù)端,IPEndPoint是根據(jù)你傳遞給MyTcpClient的構(gòu)造函數(shù)的參數(shù)(IP地址和端口)確定的。
MyTcpClient.ConnectToServer然后用Encoding.ASCII.GetBytes從string中取得bytes。一旦有bytes要發(fā)送,它通過調(diào)用System.Net.TcpClient的GetStream方法來獲取NetworkStream,然后通過TcpClient.Write方法來發(fā)送消息。
為了中服務(wù)端收到響應(yīng),阻塞函數(shù)TcpClient.Read被調(diào)用。然后連接被關(guān)閉,客戶端結(jié)束。
寫一個TCP服務(wù)端
總結(jié)
以上是生活随笔為你收集整理的【C#食谱】【杭帮菜】菜单2:写一个TCP客户端的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Outlook通过RPC或RPC ove
- 下一篇: 李开复给中国学生的第七封信:21世纪最需