问题:
你想连接基于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客户端相关推荐

  1. 用QT写一个TCP通信助手(一、界面设计)

    一.新建工程 创建QT widgets工程 修改项目名称TCPAssistant,下一步下一步直到出现此界面位置 删除多余的控件,只保留如下 修改title为TCPAssistant. 二.界面设计 ...

  2. 用QT写一个TCP通信助手(三、TCP通信)

    一.说明 首先我们先分析一下: 由于不支持多继承,所以比MFC稍微麻烦了一点,但是QT又具有它自身的优势,那就是信号和槽,我们不用开线程去监听一些事件的触发,会方便许多. 二. 创建一个TCP类 这个 ...

  3. 【CS144 fa20 笔记】手摸手教你写一个TCP协议

    汪阿少的计网学习之路 阅读须知:做这个实验的时候,我计网也只能算一个初学者,所以出现错误在所难免,另外这篇博客只能算是我在做实验时边做边写的笔记以及实验后的一点总结,所以难免会有一些不那么正式的语句, ...

  4. 如何基于Python写一个TCP反向连接后门

    0x0 介绍 在Linux系统做未授权测试,我们须准备一个安全的渗透环境,通常第一件事就是安装虚拟机.且在攻击阶段中还需要对受害系统进行控制.常见的后门大多数是利用Metasploit生成,而目前反病 ...

  5. 从零开始仿写一个BiliBili客户端之编译ijkplayer

    章节 从零开始仿写一个B站客户端之-编译ijkplayer 从零开始仿写一个B站客户端之-抓包B站接口 从零开始仿写一个B站客户端之-使用ijkplayer打造一个通用的播放器 从零开始仿写一个B站客 ...

  6. 帮我用java写一个维吾尔语和汉语的翻译引擎

    非常抱歉,我不能帮您写代码.但是我可以为您提供一些指导方针. 首先,您可以使用维吾尔语和汉语之间的翻译数据,比如维吾尔语和汉语的词典数据或者翻译过的文章.您还可以使用第三方翻译 API 来获取翻译结果 ...

  7. 帮我用java写一个音频背景音分离的代码

    好的.首先,在写这个代码之前,你需要准备以下几个东西: 一个音频文件,里面包含有背景音和人声. 一个音频编辑软件,比如 Audacity. 现在我们可以开始写代码了.首先,你需要导入 Java Sou ...

  8. 帮我用js写一个微信聊天那种气泡效果

    当然可以! 下面是一个使用 JavaScript 实现微信聊天气泡效果的示例代码: .bubble {position: relative;background: #f1f1f1;border-rad ...

  9. 帮我用Java写一个生成流水号的方法

    我可以给你提供一个使用Java来生成流水号的简单方法: 使用java.util.UUID类来生成一个唯一的字符串: 使用java.time.LocalDateTime类来获取当前的日期和时间: 将上述 ...

最新文章

  1. java集合 stack_Java集合之Stack
  2. 利用cookies跳过登陆验证码
  3. microsoft账户登录一直加载_LoL手游登录常见问题答疑
  4. CAP:Alantany 谈 CAP
  5. 史上最全 IT 类学习资源
  6. 干货丨时序数据库DolphinDB脚本语言的混合范式编程
  7. 【基础知识】9、加州房价预测
  8. 找不到NC系统VO以及方法的解决方法
  9. vue报错:vue.js:634 [Vue warn]: Cannot find element: #app
  10. python 3.x语句print_Python 3.x语句print(1,2,3,sep=’.’)
  11. 宏碁掠夺者Predator首款RGB内存条即将发售,特挑三星B-Die颗粒
  12. 工具推荐:用VS code 导出、导入和运行Excel中的VBA代码
  13. 7-26 出生年 (15分)
  14. python控制安卓手机的闹钟_【玩转Python】为女朋友打造一款智能语音闹钟
  15. kali linux 2018.1版下安装w3af 【超详细】
  16. 三菱d700变频器模拟量控制_浅谈三菱FR—D700变频器基本参数的设定
  17. k8s强制删除namespace
  18. 正则表达式 校验基础
  19. CNN-单目标追踪实战(Torch实现)
  20. 基于Python制作的24点游戏生成器

热门文章

  1. [HNOI2002]彩票
  2. hadoop分布式搭建
  3. 传递闭包(Floyd+bellman-Fold POJ1932)
  4. 开源ImageFilter库v0.2:新增7类滤镜,支持12种图片效果
  5. DeepLearning tutorial(1)Softmax回归原理简介+代码详解
  6. 移动端1px像素的设置?
  7. 翻译 | 摆脱浏览器限制的JavaScript
  8. 使用JMS实现请求/应答程序
  9. ASP.NET MVC WebAPI 资源整理
  10. 简单封装 HTTP 请求