请问Socket用TcpClient发送完消息后,如何释放本地端口?

frankfuse 2008-10-07 09:38:28
是这样的:
我需要做一个程序,要求客户端以固定端口向服务端发送消息。用了TcpClient之后,确实实现了固定IP,只是端口没有及时释放,等到下次连接的时候,他就说端口被占用。我用netstat -a发现该端口的状态是Time_wait,要很久才能自动释放.请问我写的程序有什么问题吗?还是有什么方法可以立即释放端口?



Client:
public static void TcpSend(string mes)
{
IPAddress SeverAddr = IPAddress.Parse(ServerIP);
IPAddress LocalAddr = IPAddress.Parse(ClientIP);

Int32 port = 2113;
int pro = Form1.Pro;
IPEndPoint ipe = new IPEndPoint(LocalAddr, 2125);
TcpClient tcp = new TcpClient(ipe);
Socket sock = tcp.Client;
//Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sock.Connect(SeverAddr, port);
if (sock.Connected)
{
//Console.WriteLine("Sending Client a simple Message");
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(mes);
sock.Send(bytes);
int nNumBytes;
string Response = null;
do
{
bytes = new byte[1024];
nNumBytes = sock.Receive(bytes);
Response += System.Text.Encoding.UTF8.GetString(bytes);
//Console.WriteLine("Received {0} bytes", nNumBytes);

} while (nNumBytes == 1024);
//Console.WriteLine(Response);
sock.Close();
tcp.Close();

Result re = new Result();
re.Message = Response;
re.ShowDialog();

}
}
catch (Exception e)
{
sock.Close();
tcp.Close();
MessageBox.Show(e.Message);
}
}

Server:
static void Main(string[] args)
{
IPAddress localAddr = IPAddress.Any;
Int32 port = 2113;
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//connect it to the server in this case it is ur own hostname, the localhost at port 80
IPEndPoint ipEnd = new IPEndPoint(localAddr, port);
sock.Bind(ipEnd);
sock.Listen(25);
while (true)
{
try
{
//TCPMessageSocket ms = new TCPMessageSocket(parse);
Socket Reso = sock.Accept();
MessageHandler me = new MessageHandler(Reso);
Thread th = new Thread(new ThreadStart(me.HandleMessage));
th.Start();

}

catch (Exception e)
{
Console.WriteLine("Thread error {0}", e.Message);
}

}
}
...全文
946 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
HackerJLY 2011-01-05
  • 打赏
  • 举报
回复
How to release the port of TCP Client immediately when the connection is disconnect with the TCP server. - TCP 客户端与 TCP 服务器断开连接后,如何立刻释放端口

http://blog.csdn.net/HackerJLY/archive/2011/01/05/6116857.aspx
frankfuse 2008-10-07
  • 打赏
  • 举报
回复
大哥你怎么不早说。。。
zbking 2008-10-07
  • 打赏
  • 举报
回复
帮顶 JF
wangsaokui 2008-10-07
  • 打赏
  • 举报
回复
对啊,我说的也是客户端端口,发送前你先绑定,然后再连接,发送,接收
frankfuse 2008-10-07
  • 打赏
  • 举报
回复
我说的是客户端的端口,就是你每次发往服务端用的都必须是同一个端口。接受端也是自然也是同一个端口。老板这么要求的。。
wangsaokui 2008-10-07
  • 打赏
  • 举报
回复
谁说的Socket没有办法实现端口固定?

socket.Bind(new IPEndPoint("IPAddress"),port));
然后再Connect, Send, Receive

frankfuse 2008-10-07
  • 打赏
  • 举报
回复
Tcp那招确实很古怪,但是没办法。用Socket的话,就没办法实现端口固定了。为了达到要求,才用这么蹩脚的方法的。
另外,没次发送的都是1024字节。这个连接只是负责想服务器发送消息,然后接受一个反馈消息而已。
wangsaokui 2008-10-07
  • 打赏
  • 举报
回复
你这个程序做得古怪,本来TcpClient有自己的方法GetStream来获取接收到的数据,你却去用Socket.Receive(),与其这样,为什么不用Socket来做呢?
TcpClient需要关闭字节流,比如receiveStream.Close(),然后调用TcpClient.Close(),这样才能关闭连接。

do
{
bytes = new byte[1024];
nNumBytes = sock.Receive(bytes);
Response += System.Text.Encoding.UTF8.GetString(bytes);
//Console.WriteLine("Received {0} bytes", nNumBytes);

} while (nNumBytes == 1024);
//Console.WriteLine(Response);
sock.Close();
tcp.Close();
另外你的程序调试下,是否运行到Close()这步,你是必须满1024个字节才退出接收的,一般不这样做,取而代之的是用一个特殊的结束字符,难道你每次服务器端发送的都是1024个字节吗?

Thread th = new Thread(new ThreadStart(me.HandleMessage));
th.Start();

谁知道你服务器端接收到数据后都往客户端传了些啥。
frankfuse 2008-10-07
  • 打赏
  • 举报
回复
楼上的方法用来,还算不行哦
wangsaokui 2008-10-07
  • 打赏
  • 举报
回复
sock.Close();
tcp.Close();

===》


sock.Shutdown(SocketShutdown.Both(;
sock.Close();
tcp.Close();
消失的尘芥 2008-10-07
  • 打赏
  • 举报
回复
学习,帮顶
gogogo 2008-10-07
  • 打赏
  • 举报
回复
要求比较奇怪,不过可能特殊情况会碰到,关注下,估计要研究下操作系统方面的信息。
医手 2008-10-07
  • 打赏
  • 举报
回复
关注....
如同楼主般地期待....
ericzhangbo1982111 2008-10-07
  • 打赏
  • 举报
回复
socket.close();

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧