zoukankan      html  css  js  c++  java
  • TcpListener、TcpClient

    1.TcpClient

    using System;
    using System.Text;
    using System.Net.Sockets;
    
    namespace tcpclient
    {
        class tcpclient
        {
            static void Main(string[] args)
            {
                try
                {
                    //建立客户端套接字
                    TcpClient tcpclnt = new TcpClient();
                    Console.WriteLine("正在连接服务器...");
    
                    //连接服务器
                    tcpclnt.Connect("127.0.0.1", 8081);
    
                    //得到客户端的流
                    NetworkStream stm = tcpclnt.GetStream();
                    while (true)
                    {
                        Console.Write("客户端说:");
                        string str = Console.ReadLine();//输入说话内容
                        //发送字符串
                        System.Text.UTF8Encoding utf8 = new UTF8Encoding(); //可以处理中文
                        byte[] ba = utf8.GetBytes(str);
                        stm.Write(ba, 0, ba.Length);
    
                        //接收从服务器返回的信息
                        byte[] bb = new byte[2048];
                        int k = stm.Read(bb, 0, 100);
                        //输出服务器端返回的信息
                        Console.WriteLine("服务器说:" + utf8.GetString(bb, 0, k));
                    }
                    tcpclnt.Close();
    
                }
                catch (Exception e)
                {
                    Console.WriteLine("错误..." + e.StackTrace);
                }
            }
    
        }
    }

    2.TcpListener

    using System;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    
    namespace tcpchater
    {
        class tcpserver
        {
            static void Main(string[] args)
            {
                try
                {
                    //初始化监听,端口为8001
                    TcpListener myList = new TcpListener(IPAddress.Parse("127.0.0.1"), 8081);
                    //开始监听服务器端口
                    myList.Start();
    
                    Console.WriteLine("启动服务器端,端口服务...");
                    Console.WriteLine("本地节点为:" + myList.LocalEndpoint);//LocalEndpoint属性 标识正用于侦听传入客户端连接请求的本地网络接口和端口号
                    Console.WriteLine("等待客户端连接...");
    
                    //等待处理接入连接请求
                    Socket s = myList.AcceptSocket();
    
                    //新建立的连接用套接字s表示
                    Console.WriteLine("客户端连接来自 " + s.RemoteEndPoint + " 已上线.");
                    while (true)
                    {
                        System.Text.UTF8Encoding utf8 = new UTF8Encoding(); //可以处理中文
                        //接收客户信息
                        byte[] b = new byte[2048];
                        int k = s.Receive(b);
                        Console.Write("客户端说:" + utf8.GetString(b, 0, k));
                        Console.WriteLine();
    
                        Console.Write("服务器端说:");
                        //处理客户端请求,给客户端回应
                        string str = Console.ReadLine();
                        s.Send(utf8.GetBytes(str));
                    }
                    //释放资源,结束监听
                    s.Close();
                    myList.Stop();
    
                }
                catch (Exception e)
                {
                    Console.WriteLine("错误..." + e.StackTrace);
                }
            }
        }
    }
  • 相关阅读:
    为什么每天都在学习,生活还是没有任何改善?
    MySql基础汇总
    BeanUtils.copyProperties(待复制对象, 待更新对象) || PropertyUtils.copyProperties(待更新对象, 待复制对象)
    ThreadLocal
    synchronized 锁
    STS报could not find tools.jar in the active JRE
    SpringBoot 定时任务 || cron表达式
    lombok注解
    cron表达式
    Thymeleaf 模板引擎
  • 原文地址:https://www.cnblogs.com/valor-xh/p/6040782.html
Copyright © 2011-2022 走看看