【问题标题】:Problem with simple tcp\ip client-server简单 tcp\ip 客户端-服务器的问题
【发布时间】:2011-11-16 20:45:54
【问题描述】:

我正在尝试编写简单的 tcp\ip 客户端-服务器。

这是服务器代码:

 internal class Program
    {
        private const int _localPort = 7777;

        private static void Main(string[] args)
        {
            TcpListener Listener;
            Socket ClientSock; 
            string data;
            byte[] cldata = new byte[1024]; 

            Listener = new TcpListener(_localPort);
            Listener.Start(); 
            Console.WriteLine("Waiting connections [" + Convert.ToString(_localPort) + "]...");
            try
            {
                ClientSock = Listener.AcceptSocket(); 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
            int i = 0;

            if (ClientSock.Connected)
            {
                while (true)
                {
                    try
                    {
                        i = ClientSock.Receive(cldata); 
                    }
                    catch
                    {
                    }

                    try
                    {
                        if (i > 0)
                        {

                            data = Encoding.ASCII.GetString(cldata).Trim();

                            ClientSock.Send(cldata);
                        }
                    }
                    catch
                    {
                        ClientSock.Close(); 
                        Listener.Stop();
                        Console.WriteLine(
                            "Server closing. Reason: client offline. Type EXIT to quit the application.");
                    }
                }
            }
        }
    }

这是客户端代码:

void Main()
{
         string data; // Юзерская дата
            byte[] remdata ={ };
            TcpClient Client = new TcpClient();

            string ip = "127.0.0.1";
            int port = 7777;

            Console.WriteLine("\r\nConnecting to server...");
            try
            {
                Client.Connect(ip, port);
            }
            catch
            {
                Console.WriteLine("Cannot connect to remote host!");
                return;
            }
            Console.Write("done\r\nTo end, type 'END'");
            Socket Sock = Client.Client; 

            while (true) 
            {
                Console.Write("\r\n>");
                data = Console.ReadLine();
                if (data == "END")
                    break;
                Sock.Send(Encoding.ASCII.GetBytes(data)); 
                Sock.Receive(remdata);
                Console.Write("\r\n<" + Encoding.ASCII.GetString(remdata));
            }

            Sock.Close();
            Client.Close();
}

当我发送到我的服务器时,我无法接收数据回复。 Sock.Receive(remdata) 什么也不返回!为什么?

【问题讨论】:

  • 如果您尝试隐藏或屏蔽生成的异常,您将无法获得任何异常。在 catch 中,抛出异常。不要蒙面。然后你就会知道问题出在哪里。或者您是否看到正在打印任何控制台消息?请提供更多调试信息
  • 仅在某个地方屏蔽了异常。但它工作正常。客户端的问题。

标签: c# .net networking tcp ip


【解决方案1】:

您正在尝试接收一个空缓冲区。您应该以合理的大小分配缓冲区,然后记下接收到的数据量:

byte[] buffer = new byte[1024];

...

int bytesReceived = socket.Receive(buffer);
string text = Encoding.ASCII.GetString(buffer, 0, bytesReceived);

(顺便说一句,将 PascalCase 用于局部变量有点不合常规。我还敦促您不要只是盲目地捕获异常,不要在不记录异常的情况下吞下异常。)

【讨论】:

  • 谢谢。这么快就能看出问题出在哪里真是太好了!
猜你喜欢
  • 2017-08-04
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 2015-12-29
  • 2016-03-09
  • 1970-01-01
  • 2010-10-30
  • 2022-01-22
相关资源
最近更新 更多