【C#食谱】【杭帮菜】菜单2:写一个TCP客户端
生活随笔
收集整理的這篇文章主要介紹了
【C#食谱】【杭帮菜】菜单2:写一个TCP客户端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題:
你想連接基于TCP的服務端。
解決方法:
使用System.Net.TcpClient類,通過給服務端傳遞地址和端口來和服務端建立連接和會話。下面這個例子將和上一菜單中的服務端進行會話。
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);
????????????// 獲取將要發生的消息
????????????byte[]?bytes?=?Encoding.ASCII.GetBytes(msg);
????????????// 獲取和服務端會話的流
????????????using?(NetworkStream?ns?=?_client.GetStream())
????????????{
????????????????// 發送消息
????????????????Trace.WriteLine("Sending?message?to?server:?"?+?msg);
????????????????ns.Write(bytes,?0,?bytes.Length);
????????????????// 獲得響應
????????????????// 存儲響應內容的緩存
????????????????bytes?=?new?byte[1024];
????????????????// 顯示響應內容
????????????????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
????????{
????????????// 關閉所有的東西
????????????if(_client?!=?null)?
????????????????_client.Close();
????????}
????}
} 要使用這個類,你可以簡單的創建一個它的實例,然后通過調用ConnectToServer來發送請求。在這個程序中,你首先呼叫了服務端三次,來測試基本的機制。接著,你進入一個循環來真正的“重擊”服務端,使得請求數超過服務端默認的線程池容量。這就可以證明服務端在處理多個請求的機制是可靠的。
????static?void?Main(string[]?args)
????{
????????MakeClientCallToServer("Just?wanted?to?say?hi");
????????MakeClientCallToServer("Just?wanted?to?say?hi?again");
????????MakeClientCallToServer("Are?you?ignoring?me?");
????????// 現在發送一堆消息
????????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是被設計用來發送一條消息,獲得響應,顯示響應的內容,然后關閉連接。為了完成這些,它創建一個System.Net.TcpClient并通過調用TcpClient.Connect連接到服務端。Connect通過IPEndPoint定位到服務端,IPEndPoint是根據你傳遞給MyTcpClient的構造函數的參數(IP地址和端口)確定的。
MyTcpClient.ConnectToServer然后用Encoding.ASCII.GetBytes從string中取得bytes。一旦有bytes要發送,它通過調用System.Net.TcpClient的GetStream方法來獲取NetworkStream,然后通過TcpClient.Write方法來發送消息。
為了中服務端收到響應,阻塞函數TcpClient.Read被調用。然后連接被關閉,客戶端結束。
寫一個TCP服務端
你想連接基于TCP的服務端。
解決方法:
使用System.Net.TcpClient類,通過給服務端傳遞地址和端口來和服務端建立連接和會話。下面這個例子將和上一菜單中的服務端進行會話。
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);
????????????// 獲取將要發生的消息
????????????byte[]?bytes?=?Encoding.ASCII.GetBytes(msg);
????????????// 獲取和服務端會話的流
????????????using?(NetworkStream?ns?=?_client.GetStream())
????????????{
????????????????// 發送消息
????????????????Trace.WriteLine("Sending?message?to?server:?"?+?msg);
????????????????ns.Write(bytes,?0,?bytes.Length);
????????????????// 獲得響應
????????????????// 存儲響應內容的緩存
????????????????bytes?=?new?byte[1024];
????????????????// 顯示響應內容
????????????????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
????????{
????????????// 關閉所有的東西
????????????if(_client?!=?null)?
????????????????_client.Close();
????????}
????}
} 要使用這個類,你可以簡單的創建一個它的實例,然后通過調用ConnectToServer來發送請求。在這個程序中,你首先呼叫了服務端三次,來測試基本的機制。接著,你進入一個循環來真正的“重擊”服務端,使得請求數超過服務端默認的線程池容量。這就可以證明服務端在處理多個請求的機制是可靠的。
????static?void?Main(string[]?args)
????{
????????MakeClientCallToServer("Just?wanted?to?say?hi");
????????MakeClientCallToServer("Just?wanted?to?say?hi?again");
????????MakeClientCallToServer("Are?you?ignoring?me?");
????????// 現在發送一堆消息
????????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是被設計用來發送一條消息,獲得響應,顯示響應的內容,然后關閉連接。為了完成這些,它創建一個System.Net.TcpClient并通過調用TcpClient.Connect連接到服務端。Connect通過IPEndPoint定位到服務端,IPEndPoint是根據你傳遞給MyTcpClient的構造函數的參數(IP地址和端口)確定的。
MyTcpClient.ConnectToServer然后用Encoding.ASCII.GetBytes從string中取得bytes。一旦有bytes要發送,它通過調用System.Net.TcpClient的GetStream方法來獲取NetworkStream,然后通過TcpClient.Write方法來發送消息。
為了中服務端收到響應,阻塞函數TcpClient.Read被調用。然后連接被關閉,客戶端結束。
寫一個TCP服務端
總結
以上是生活随笔為你收集整理的【C#食谱】【杭帮菜】菜单2:写一个TCP客户端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Outlook通过RPC或RPC ove
- 下一篇: 李开复给中国学生的第七封信:21世纪最需