gpt4 book ai didi

c# - System.Net.Sockets.Tcpclient.Connect 错误

转载 作者:可可西里 更新时间:2023-11-01 02:43:43 25 4
gpt4 key购买 nike

我正在尝试在我的控制台应用程序和 PIC 微 Controller 之间建立 TCP 连接。这里的行为很奇怪。有时我能够建立联系,有时却不能。行为是完全随机的。下面给出了代码以及我收到的错误的快照。

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
while (true)
{
try
{
TcpClient tcpclnt1 = new TcpClient();
Console.WriteLine("Connecting.....");

tcpclnt1.Connect("10.112.90.246", 13002);
// use the ipaddress as in the server program

Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");

String str = Console.ReadLine();
Stream stm = tcpclnt1.GetStream();

ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
Console.WriteLine("Transmitting.....");

stm.Write(ba, 0, ba.Length);

//byte[] bb = new byte[100];
//int k = stm.Read(bb, 0, 100);

//for (int i = 0; i < k; i++)
// Console.Write(Convert.ToChar(bb[i]));

tcpclnt1.Close();
}

catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}

}
}
}

错误

enter image description here

enter image description here

enter image description here

消息错误

最佳答案

您使用的是 IP 地址而不是主机名,将字符串作为参数的连接方法需要 DNS 主机名而不是 IP 地址:

public void Connect(string hostname, int port)

hostname

Type: System.String

The DNS name of the remote host to which you intend to connect.

使用主机名或 method它以 IP 作为参数 istead参见 Documentation

关于c# - System.Net.Sockets.Tcpclient.Connect 错误 <String hostname, Int32 port>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22525529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com