惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
P
Privacy & Cybersecurity Law Blog
I
Intezer
Y
Y Combinator Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
Netflix TechBlog - Medium
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Stack Overflow Blog
Stack Overflow Blog
Hacker News: Ask HN
Hacker News: Ask HN
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
T
Tor Project blog
C
Cybersecurity and Infrastructure Security Agency CISA
云风的 BLOG
云风的 BLOG
博客园_首页
V2EX - 技术
V2EX - 技术
T
Threat Research - Cisco Blogs
腾讯CDC
宝玉的分享
宝玉的分享
博客园 - 叶小钗
罗磊的独立博客
S
Securelist
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
Scott Helme
Scott Helme
博客园 - 司徒正美
W
WeLiveSecurity
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Secure Thoughts
NISL@THU
NISL@THU
N
News and Events Feed by Topic
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
K
Kaspersky official blog
IT之家
IT之家

博客园 - 随风逝去(叶进)

MySQL分表(Partition)学习研究报告 用WPF+MongoDB开发房产信息收集器(4)——房产信息采集器总体介绍附程序下载 用WPF+MongoDB开发房产信息收集器(3)——MongoDB入门 用WPF+MongoDB开发房产信息收集器(2)——后台线程 用WPF+MongoDB开发房产信息收集器(1) 解决FireFox不能Debug Silverlight程序的问题 - 随风逝去(叶进) - 博客园 终于可以继续往下了! 回首2008 另类的二级域名实现方法 【更新 2008.10.16】触发C#Button的双击事件 触发C#Button的双击事件 到底要不要去面试? 【C#食谱】【面食】菜单7:用默认值初始化泛型变量 类和对象 C#Winform下用正则表达式限制TextBox只能输入数字 【C#食谱】【杭帮菜】菜单1:写一个TCP服务端 【C#食谱】【川菜】菜单1:列出被引用的程序集 实现需求工程的成功方法 实现需求工程的成功方法——难度:高;影响:低
【C#食谱】【杭帮菜】菜单2:写一个TCP客户端
随风逝去(叶进) · 2008-05-11 · via 博客园 - 随风逝去(叶进)

问题:
你想连接基于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服务端