您好有人可以帮我解决这个问题吗?我正在尝试创建客户端和服务器应用程序反向连接。当我尝试打开客户端和服务器时,反之亦然。场景是我执行客户端和服务器,但是当我关闭服务器并再次打开时,它不接受任何连接,连接丢失。
这是代码:
客户代码:
namespace client1
{
class Program
{
public static bool isConnected { get; set; }
public static NetworkStream Writer { get; set; }
public static NetworkStream Reciever { get; set; }
static void Main(string[] args)
{
Connect_To_Server();
}
public static void Connect_To_Server()
{
TcpClient Connecting = new TcpClient();
string IP = "178.121.1.2";
while (isConnected == false)
{
try
{
Connecting.Connect(IP, 2000);
isConnected = true;
Writer = Connecting.GetStream();
Reciever = Connecting.GetStream();
Console.WriteLine("Connected: " + IP);
}
catch
{
Console.WriteLine("Error Connection... .");
Console.Clear();
}
}
Console.ReadKey();
}
}
}
服务器代码:
namespace server1
{
class Program
{
public static bool isConnected { get; set; }
public static NetworkStream Writer { get; set; }
static void Main(string[] args)
{
Listen_To_Client();
}
public static void Listen_To_Client()
{
TcpListener Listen = new TcpListener(2000);
while (true)
{
Listen.Start();
if (Listen.Pending())
{
TcpClient Connect = Listen.AcceptTcpClient();
isConnected = true;
Console.WriteLine("Connection Accepted");
Writer = Connect.GetStream();
Console.ReadKey();
}
}
}
}
}