TcpClient写入错误 [英] TcpClient write error

查看:170
本文介绍了TcpClient写入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在关注Code Project中的文章。我在客户端尝试将数据发送到服务器(write()方法)时收到错误。这是服务器(TcpListener)代码。我将端口号从作者使用的8001更改为其他一些数字
(44300),因为8001不起作用。监听器启动后显示

Hi I am following article in Code Project. I am getting error at the point where client tries to send data to server (write() method). this is the server (TcpListener) code. I made change in port number from 8001 the author is using to some other number (44300) because 8001 did not work. When the Listener is started it shows

服务器在端口8001运行...

本地端点是  :192.168.56.1:44300

等待连接.....

The server is running at port 8001...
The local End point is  :192.168.56.1:44300
Waiting for a connection.....

当客户端运行时,它会在几分钟后显示错误:

When the client is run it shows error after a couple of minutes:

连接.....

已连接

输入要传输的字符串:hellow server

传输.....

错误.....   在System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32 offs

et,Int32 size)

    at cnt中的clnt.Main():\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b $ b按任意键继续。 。 。

Connecting.....
Connected
Enter the string to be transmitted : hellow server
Transmitting.....
Error.....    at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offs
et, Int32 size)
   at clnt.Main() in c:\users\user\Source\Repos\ConsoleApp2\ConsoleApp2\Program.
cs:line 34
Press any key to continue . . .

我需要帮助:找到错误原因的任何调试思路?我试图关闭防火墙,但它没有帮助。

I need help: any debugging ideas to find the cause of error? I tried to turn off Firewall but it did not help.

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

public class serv
{
    public static void Main()
    {
        try
        {
            IPAddress ipAd = IPAddress.Parse("192.168.56.1"); //use local m/c IP address, and use the same in the client

            /* Initializes the Listener */
            TcpListener myList = new TcpListener(ipAd, 44300);

            /* Start Listeneting at the specified port */
            myList.Start();

            Console.WriteLine("The server is running at port 8001...");
            Console.WriteLine("The local End point is  :" + myList.LocalEndpoint);
            Console.WriteLine("Waiting for a connection.....");

            Socket s = myList.AcceptSocket();
            Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

            byte[] b = new byte[100];
            int k = s.Receive(b);
            Console.WriteLine("Recieved...");
            for (int i = 0; i < k; i++)
                Console.Write(Convert.ToChar(b[i]));

            ASCIIEncoding asen = new ASCIIEncoding();
            s.Send(asen.GetBytes("The string was recieved by the server."));
            Console.WriteLine("\nSent Acknowledgement");
            /* clean up */
            s.Close();
            myList.Stop();

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

}


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


public class clnt
{

    public static void Main()
    {

        try
        {
            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine("Connecting.....");

            tcpclnt.Connect("192.168.56.1", 8001); // use the ipaddress as in the server program

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

            String str = Console.ReadLine();
           NetworkStream stm = tcpclnt.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]));

            tcpclnt.Close();
        }

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

推荐答案

在两个程序中使用相同的有效地址和端口。

对于本地测试,您可以使用
IPAddress.Loopback 地址。

For local tests, you can use the IPAddress.Loopback address.

改善错误报告:

。 。 。

catch(例外e)

{

   Console.WriteLine(" Error:" + e);


}

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

显示错误消息。


这篇关于TcpClient写入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆