• TCP编程(6):客户端接收程序TcpClient


    /*--===------------------------------------------===---
    客户端接收程序:
        TcpClient, NetworkStream

                许明会    2007年12月9日 22:57:13
    --===------------------------------------------===---
    */
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace NetCommunication
    {
        
    class tcpClient
        {
            
    private const int port = 7788;
            
    private const string hostname = "127.0.0.1";

            
    static void Main()
            {
                
    try
                {
                    Console.WriteLine(
    "尝试连接服务器{0}:{1}\t{2}", hostname, port, DateTime.Now.ToString());
                    System.Net.Sockets.TcpClient tc 
    = new System.Net.Sockets.TcpClient(hostname, port);
                    System.Net.Sockets.NetworkStream ns 
    = tc.GetStream();
                    
    byte[] bytes = new byte[1024];
                    
    int nCount = ns.Read(bytes, 0, bytes.Length);
                    
    if(nCount>0)
                        Console.WriteLine(System.Text.Encoding.ASCII.GetString(bytes, 
    0, nCount));
                    tc.Close();
                    Console.ReadLine();
                }
                
    catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
  • 相关阅读:
    c语言关键字-static
    c语言关键字-const
    c语言32个关键字
    宏定义#define详解
    c程序启动终止过程及exit(),_exit(),atexit()函数区别
    c语言编译过程和头文件<>与""的区别
    职业经理人-以柔克刚的柔性领导力
    职业经理人-如何管理好你的老板
    职业经理人-带人要带心
    职业经理人-怎样能批评了下属还让他很高兴
  • 原文地址:https://www.cnblogs.com/flaaash/p/988757.html
Copyright © 2020-2023  润新知