zoukankan      html  css  js  c++  java
  • TCP编程(5):服务器端 TcpListener

    /*--===------------------------------------------===---
    服务器端
        TcpListener: Start(), AcceptTcpClient(), 
        NetworkStream
                许明会    2007年12月9日 22:57:43
    --===------------------------------------------===---
    */
    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Net;
    using System.Net.Sockets;

    namespace NetCommunication
    {
        
    class tcpServer
        {
            
    private const int port = 7788;

            
    static void Main()//_server
            {
              
                
    try
                {
                    
    bool done = false;
                    System.Net.IPAddress local 
    = System.Net.IPAddress.Parse("127.0.0.1");
                    TcpListener listener 
    = new TcpListener(local,port);
                    listener.Start();
    //开始监听
                    while (!done)
                    {
                        Console.Write(
    "Waiting for connection");
                        TcpClient tc 
    = listener.AcceptTcpClient();
                        Console.WriteLine(
    "Connection Accepted:{0}",tc.Client.LocalEndPoint.ToString());
                        NetworkStream ns 
    = tc.GetStream();

                        
    try
                        {    
    //将字符串转换为数组
                            byte[] bytTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());
                            ns.Write(bytTime, 
    0, bytTime.Length);
                            ns.Close();
                            tc.Close();
                        }
                        
    catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }

                }
                
    catch (Exception ex)
                {
                    Console.WriteLine(ex.Message); ;
                }
            }
        }
    }
  • 相关阅读:
    〖編程·C++〗回溯算法:排列树 N皇后问题
    〖網頁·PHP〗Windows 7下进行PHP环境搭建
    〖編程·C++〗回溯算法:排列树 工作分配问题
    〖編程·C++〗回溯算法:完全N叉树 最佳调度问题 以及相关思考
    pod install 初始化失败 RuntimeError [Xcodeproj] Unknown object version.
    iOS 版本判定
    Windows部署多版本Mysql服务
    注意IE的Cookie个数限制问题
    检索 COM 类工厂中的组件时失败,原因是出现以下错误: 8007007e
    一次曲折的单点集成之旅
  • 原文地址:https://www.cnblogs.com/flaaash/p/988754.html
Copyright © 2011-2022 走看看