当前位置: 首页 > article >正文

TcpClientTest

ClientTest:

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



class TcpClientTest
{
    static void Main(string[] args)
    {
        try
        {
            // 创建一个TcpClient实例并连接到服务器  
            TcpClient client = new TcpClient("1vg5062570.51mypc.cn", 43319);
            //1vg5062570.51mypc.cn:43319
            // 获取一个NetworkStream对象以进行读写  
            NetworkStream stream = client.GetStream();

            // 将消息转换为字节数组  
            string message = "Hello from the client!";
            byte[] data = Encoding.ASCII.GetBytes(message);

            // 发送消息到服务器  
            stream.Write(data, 0, data.Length);

            // 读取服务器的响应  
            byte[] buffer = new byte[4096];
            int bytesRead = stream.Read(buffer, 0, buffer.Length);

            // 将接收到的字节转换为字符串  
            string responseData = Encoding.ASCII.GetString(buffer, 0, bytesRead);
            Console.WriteLine("Received from server: " + responseData);

            // 关闭连接  
            client.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
        }

        // 等待用户按键,以便在控制台中查看结果  
        Console.WriteLine("Press Enter to continue...");
        Console.ReadLine();
    }
}

ClientTestV3:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;

public class IPport
{
    public string IP { get; set; }
    public int Port { get; set; }
}

namespace ClientTestV3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string jsonFilePath = "ipport.json"; // 替换为你的JSON文件路径  

                // 读取文件内容  
                string jsonContent = File.ReadAllText(jsonFilePath);

                // 反序列化JSON字符串到对象  
                IPport ipaddr = JsonConvert.DeserializeObject<IPport>(jsonContent);

                // 输出结果  
                Console.WriteLine($"{ipaddr.IP}:{ipaddr.Port}");

                // 创建一个TcpClient实例并连接到服务器  
                TcpClient client = new TcpClient($"{ipaddr.IP}", ipaddr.Port);

                // 获取一个NetworkStream对象以进行读写  
                NetworkStream stream = client.GetStream();


                // 将消息转换为字节数组  
                string message = "Hello from the client!";
                Random rand = new Random();
                message += rand.Next().ToString();
                byte[] data = Encoding.ASCII.GetBytes(message);

                // 发送消息到服务器  
                stream.Write(data, 0, data.Length);

                // 读取服务器的响应  
                byte[] buffer = new byte[4096];
                int bytesRead = stream.Read(buffer, 0, buffer.Length);

                // 将接收到的字节转换为字符串  
                string responseData = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                Console.WriteLine("Received from server: " + responseData);
                Console.WriteLine("ReceivedBytes:{0}", bytesRead);
                // 关闭连接  
                client.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }

            // 等待用户按键,以便在控制台中查看结果  
            Console.WriteLine("Press Enter to continue...");
            Console.ReadLine();
        }
    }
}


http://www.kler.cn/a/536038.html

相关文章:

  • GPT-4使用次数有上限吗?一文了解使用规则
  • 复制粘贴小工具——Ditto
  • C基础寒假练习(6)
  • 2.Mkdocs配置说明(mkdocs.yml)【最新版】
  • docker安装es及分词器ik
  • Python爬虫-如何正确解决起点中文网的无限debugger
  • Python中 logging.basicConfig
  • 最新阿里高级Java面试题(首发,70道,带详细答案)
  • 支持向量机(一)
  • VERA: 基于视觉-语言模型的解释性视频异常检测框架
  • 大模型的微调方式
  • 【软件测试入门】Linux操作系统初级命令大全
  • 大模型蒸馏(Model Distillation)的原理及过程
  • 【Git】tortoisegit使用配置
  • 解锁高效 Web 开发新姿势:Open WebUI 安装指南
  • Java 的try-with-resources语句,不需要显式调用close()
  • autMan奥特曼机器人-对接deepseek教程
  • 【鸿蒙HarmonyOS Next实战开发】实现ArkTS/JS和C/C++的交互-Node-API
  • Qt —— 加载百度离线地图、及简单绘图(附源码)
  • 备战蓝桥杯:二维前缀和之激光炸弹
  • Java面试题-Java基础
  • 基础入门-算法解密散列对称非对称字典碰撞前后端逆向MD5AESDESRSA
  • C++:代码常见规范1
  • 七。自定义数据集 使用tensorflow框架实现逻辑回归并保存模型,然后保存模型后再加载模型进行预测
  • Mac: docker安装以后报错Command not found: docker
  • ctf网络安全大赛python ctf网络安全大赛