我的T CP客户端未连接到我的TCP服务器

问题描述 投票:0回答:1

这些是我在C#中通过TCP进行客户端-服务器通信的第一步。在“等待客户端...”消息后,服务器被卡住。客户端显示“错误:未连接到服务器”。怎么了?

服务器代码:

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

namespace MyTCPServer
{
    public class MyTCPServerClass
    {
        public static void Main(string[] args)
        {
            TcpListener listener = null;
            int servPort = 55437;

            try 
            {
                listener = new TcpListener(IPAddress.Any, servPort);
                listener.Start();
            } 
            catch (SocketException se)
            {
                Console.WriteLine(se.ErrorCode + ": " + se.Message);
                Environment.Exit(se.ErrorCode);
            }

            TcpClient client = null;
            NetworkStream netStream = null;

            try
            {
                Console.WriteLine("Waiting for Client...");
                client = listener.AcceptTcpClient();
                Console.WriteLine("Get Stream...");
                netStream = client.GetStream();
                Console.Write("Handling client - ");

                int bytesRcvd;
                int totalBytesEchoed = 0;
                byte[] rcvBuffer = new byte[10000];

                while ((bytesRcvd = netStream.Read(rcvBuffer, 0, rcvBuffer.Length)) > 0) 
                {
                    netStream.Write(rcvBuffer, 0, bytesRcvd);
                    totalBytesEchoed += bytesRcvd;
                }
                Console.WriteLine("echoed {0} bytes.", totalBytesEchoed);

                netStream.Close();
                client.Close();

            } 
            catch (Exception e) 
            {
                Console.WriteLine(e.Message);
                netStream.Close();
            }
        }
    }
}

客户代码:

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

namespace MyTCPClient
{
    /// <summary>
    /// Demonstration eines synchron agierenden TcpClienten.
    /// </summary>
    public class MyTCPClientClass
    {
        static void Main(string[] args)
        {
            byte[] byteBuffer = Encoding.ASCII.GetBytes("Hello World");

            //IPAddress ipAddress = IPAddress.Parse("192.168.1.16");
            IPAddress ipAddress = IPAddress.Loopback;

            int servPort = 55437;

            TcpClient client = null;
            NetworkStream netStream = null;

            try {
                client = new TcpClient(new IPEndPoint(ipAddress, servPort));

                if (!client.Connected) 
                {
                    Console.WriteLine("Error: Not Connected to server");
                    throw new Exception();
                }
                else Console.WriteLine("Connected to server... sending echo string");

                netStream = client.GetStream();

                netStream.Write(byteBuffer, 0, byteBuffer.Length);

                Console.WriteLine("Sent {0} bytes to server...", byteBuffer.Length);

                int totalBytesRcvd = 0;
                int bytesRcvd = 0;

                while (totalBytesRcvd < byteBuffer.Length) {
                    if ((bytesRcvd = netStream.Read(byteBuffer, totalBytesRcvd,
                            byteBuffer.Length - totalBytesRcvd)) == 0) {
                        Console.WriteLine("Connection closed prematurely.");
                        break;
                    }
                    totalBytesRcvd += bytesRcvd;
                }

                Console.WriteLine("Received {0} bytes from server: {1}", totalBytesRcvd,
                                  Encoding.ASCII.GetString(byteBuffer, 0, totalBytesRcvd));

            } 
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            } 
            finally {
                if (netStream != null) netStream.Close();
                if (client != null) client.Close();
            }

            Console.ReadKey(true);
        }
    }
}
c# tcpclient tcpserver
1个回答
1
投票
client = new TcpClient();
client.Connect(new IPEndPoint(ipAddress, servPort));
© www.soinside.com 2019 - 2024. All rights reserved.