⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 用TCPListener模拟socket编程的一问一答聊天 基于控制台
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace TCPClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TcpClient tcpc = new TcpClient("127.0.0.1", 10000);
                NetworkStream ns = tcpc.GetStream();


                while (true)
                {
                    string input = Console.ReadLine();
                    byte[] data = Encoding.ASCII.GetBytes(input);
                    ns.Write(data, 0, data.Length);

                    byte[] inbyte = new byte[1024];
                    int recv = ns.Read(inbyte, 0, inbyte.Length);
                    Console.WriteLine(Encoding.ASCII.GetString(inbyte, 0, recv) + "    from" + tcpc.Client.RemoteEndPoint.ToString());
                }
                ns.Close();
                tcpc.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("先开服务端!");
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -