为什么我用new TcpClient("127.0.0.1",80)发出本机连接请求,用TcpListener去监听,总是报 错:
《通常每个套接字地址(协议/网络地址/端口)只允许使用一次。》
我的程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;namespace ConsoleApplication1
{
class Program
{
private const int portNum = 83;//服务器端口,可以随意修改
private const string hostName = "127.0.0.1";//服务器地址,127.0.0.1指本机 static void Main(string[] args)
{
try
{
Console.Write("Try to connect to " + hostName + ":" + portNum.ToString() + "\r\n");
TcpClient client = new TcpClient(hostName, portNum); TcpListener listener = new TcpListener(IPAddress.Parse(hostName), portNum);
bool done = false;
listener.Start();
while (!done)
{
Console.Write("Waiting for connection…");
TcpClient client2 = listener.AcceptTcpClient(); Console.WriteLine("Connection accepted.");
NetworkStream ns2 = client2.GetStream(); byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString()); try
{
ns2.Write(byteTime, 0, byteTime.Length);
ns2.Close();
client2.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
listener.Stop(); NetworkStream ns = client.GetStream();
byte[] bytes = new byte[1024];
int bytesRead = ns.Read(bytes, 0, bytes.Length);
Console.WriteLine(Encoding.ASCII.GetString(bytes, 0, bytesRead));
client.Close();
Console.ReadLine();//由于是控制台程序,故为了清楚的看到结果,可以加上这句
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}程序一运行到 listener.Start(); 处就报上面的错:《通常每个套接字地址(协议/网络地址/端口)只允许使用一次。》
《通常每个套接字地址(协议/网络地址/端口)只允许使用一次。》
我的程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;namespace ConsoleApplication1
{
class Program
{
private const int portNum = 83;//服务器端口,可以随意修改
private const string hostName = "127.0.0.1";//服务器地址,127.0.0.1指本机 static void Main(string[] args)
{
try
{
Console.Write("Try to connect to " + hostName + ":" + portNum.ToString() + "\r\n");
TcpClient client = new TcpClient(hostName, portNum); TcpListener listener = new TcpListener(IPAddress.Parse(hostName), portNum);
bool done = false;
listener.Start();
while (!done)
{
Console.Write("Waiting for connection…");
TcpClient client2 = listener.AcceptTcpClient(); Console.WriteLine("Connection accepted.");
NetworkStream ns2 = client2.GetStream(); byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString()); try
{
ns2.Write(byteTime, 0, byteTime.Length);
ns2.Close();
client2.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
listener.Stop(); NetworkStream ns = client.GetStream();
byte[] bytes = new byte[1024];
int bytesRead = ns.Read(bytes, 0, bytes.Length);
Console.WriteLine(Encoding.ASCII.GetString(bytes, 0, bytesRead));
client.Close();
Console.ReadLine();//由于是控制台程序,故为了清楚的看到结果,可以加上这句
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}程序一运行到 listener.Start(); 处就报上面的错:《通常每个套接字地址(协议/网络地址/端口)只允许使用一次。》
上面应该就已经开始工作了然后你又执行了start
listener.Start();
注释掉后报错 《由于目标机器积极拒绝,无法连接。 127.0.0.1:83>了啊
向高手请教 急待解决!!!!
注意要将其销毁!否则可能产生端口不能释放的问题。
也就是说我在程序刚运行的时候去连接,如果连接成功的话知道程序结束为止,都不用去请求连接了吗?
listener.AcceptTcpClient();就是返回TcpClient,你却在之前创建一个client:
TcpClient client = new TcpClient(hostName, portNum);//把这个去掉
希望大家可以踊跃参加,积极讨论