Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Net.Sockets.Tcpclient.Connect处出错<;字符串主机名,Int32端口>;_C#_Sockets_Tcp_Tcpclient - Fatal编程技术网

C# System.Net.Sockets.Tcpclient.Connect处出错<;字符串主机名,Int32端口>;

C# System.Net.Sockets.Tcpclient.Connect处出错<;字符串主机名,Int32端口>;,c#,sockets,tcp,tcpclient,C#,Sockets,Tcp,Tcpclient,我正在尝试在我的控制台应用程序和PIC微控制器之间建立TCP连接。这里的行为很奇怪。有时我能联系上,有时却不能。这种行为完全是随机的。下面给出了代码以及我得到的错误快照 using System; using System.IO; using System.Net; using System.Text; using System.Net.Sockets; namespace ConsoleApplication1 { class Program { static

我正在尝试在我的控制台应用程序和PIC微控制器之间建立
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);
                }
            }

        }
    }
}
使用系统;
使用System.IO;
Net系统;
使用系统文本;
使用System.Net.Sockets;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
while(true)
{
尝试
{
TcpClient tcpclnt1=新的TcpClient();
Console.WriteLine(“连接…”);
tcpclnt1.连接(“10.112.90.246”,13002);
//使用服务器程序中的IP地址
控制台。写入线(“连接”);
Console.Write(“输入要传输的字符串:”);
String str=Console.ReadLine();
Stream stm=tcpclnt1.GetStream();
ascienceoding asen=新的ascienceoding();
byte[]ba=asen.GetBytes(str);
Console.WriteLine(“传输…”);
stm.Write(ba,0,ba.Length);
//字节[]bb=新字节[100];
//intk=stm.Read(bb,0100);
//for(int i=0;i
错误


消息错误

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

public void Connect(string hostname, int port)
主机名

类型:System.String

要连接到的远程主机的DNS名称 你打算连接

使用主机名或将IP作为参数的isntead
请参见

打印电子邮件输出或仅打印整个异常,基本上。。。这将自动包括任何可能相关的嵌套异常。您是否进行过任何以太网日志记录(例如wireshark)以检查您的PIC是否正确响应您的TCP连接尝试?