在 Compact Framework 2.0 中接收超时
Receive Timeout in Compact Framework 2.0
我的程序中的套接字有问题。我正在开发一个嵌入到另一个程序中的 FTP 服务器,为了做到这一点,我创建了一个套接字和一个 TCPListener。我的问题发生在这种情况下:
- 我第一次打开连接,如果没有人连接,我关闭它,一切都很好;否则,如果有人连接到我的 FTP 服务器也一切正常;
- 如果在第一次关闭连接并在之前没有任何人连接的情况下再次打开它,我得到一个 "socket/ip address/port" 的 SocketException 已经在使用中并且我无法再次实例化它。
我的功能代码是:
public void Start()
{
ServerStarted = true;
if (this.authHandler == null)
this.authHandler = new DefaultAuthHandler();
if (this.fsHandler == null)
this.fsHandler = new DefaultFileSystemHandler();
Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
TcpListener listenerServer = new TcpListener(endpoint);
listenerServer.Start(); // The exception is here at the second time
// I open the connection without anybody connected before
try
{
while (ServerStarted)
{
socketServer = listenerServer.AcceptSocket();
try
{
IPEndPoint socketPort = (IPEndPoint)socketServer.RemoteEndPoint;
Session session = new Session(socketServer, bufferSize, this.authHandler.Clone(socketPort), this.fsHandler.Clone(socketPort));
session.Start();
sessions.Add(session);
p_HostIPAddress = socketPort.Address;
if (p_HostIPAddress != null)
HostConnected = true;
// Purge old sessions
for (int i = sessions.Count - 1; i >= 0; --i)
{
if (!sessions[i].IsOpen)
{
sessions.RemoveAt(i);
--i;
}
}
}
catch(Exception ex)
{
}
}
}
catch (SocketException socketEx)
{
}
finally
{
foreach (Session s in sessions)
{
s.Stop();
HostConnected = false;
}
TcpSocketListener = null;
}
}
我知道在 Compact 框架中没有 Timeout 调用,所以我该怎么做才能避免我的套接字仍然在后台打开。
哦,对了,还有一个额外的信息:
我的 ftp 服务器实际上使用 OpenNETCF FTP 库
好的,我已经使用 SetSocketOption 解决了问题,并且稍微更改了代码。现在是:
public void Start()
{
ServerStarted = true;
if (this.authHandler == null)
this.authHandler = new DefaultAuthHandler();
if (this.fsHandler == null)
this.fsHandler = new DefaultFileSystemHandler();
socketServer = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
/* Here is the change. In this way I can reuse multiple times the
* same address without getting the SocketException.*/
socketServer.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress,
1);
Socket handler = null;
try
{
socketServer.Bind(endpoint);
socketServer.Listen(10);
while (ServerStarted)
{
handler = socketServer.Accept();
try
{
IPEndPoint socketPort = (IPEndPoint)handler.RemoteEndPoint;
Session session = new Session(handler,bufferSize,
this.authHandler.Clone(socketPort),
this.fsHandler.Clone(socketPort));
session.Start();
sessions.Add(session);
p_HostIPAddress = socketPort.Address;
if (p_HostIPAddress != null)
HostConnected = true;
// Purge old sessions
for (int i = sessions.Count - 1; i >= 0; --i)
{
if (!sessions[i].IsOpen)
{
sessions.RemoveAt(i);
--i;
}
}
}
catch(Exception ex)
{
}
}
}
catch (SocketException socketEx)
{
}
finally
{
handler.Close();
foreach (Session s in sessions)
{
s.Stop();
HostConnected = false;
}
}
}
我的程序中的套接字有问题。我正在开发一个嵌入到另一个程序中的 FTP 服务器,为了做到这一点,我创建了一个套接字和一个 TCPListener。我的问题发生在这种情况下:
- 我第一次打开连接,如果没有人连接,我关闭它,一切都很好;否则,如果有人连接到我的 FTP 服务器也一切正常;
- 如果在第一次关闭连接并在之前没有任何人连接的情况下再次打开它,我得到一个 "socket/ip address/port" 的 SocketException 已经在使用中并且我无法再次实例化它。
我的功能代码是:
public void Start()
{
ServerStarted = true;
if (this.authHandler == null)
this.authHandler = new DefaultAuthHandler();
if (this.fsHandler == null)
this.fsHandler = new DefaultFileSystemHandler();
Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
TcpListener listenerServer = new TcpListener(endpoint);
listenerServer.Start(); // The exception is here at the second time
// I open the connection without anybody connected before
try
{
while (ServerStarted)
{
socketServer = listenerServer.AcceptSocket();
try
{
IPEndPoint socketPort = (IPEndPoint)socketServer.RemoteEndPoint;
Session session = new Session(socketServer, bufferSize, this.authHandler.Clone(socketPort), this.fsHandler.Clone(socketPort));
session.Start();
sessions.Add(session);
p_HostIPAddress = socketPort.Address;
if (p_HostIPAddress != null)
HostConnected = true;
// Purge old sessions
for (int i = sessions.Count - 1; i >= 0; --i)
{
if (!sessions[i].IsOpen)
{
sessions.RemoveAt(i);
--i;
}
}
}
catch(Exception ex)
{
}
}
}
catch (SocketException socketEx)
{
}
finally
{
foreach (Session s in sessions)
{
s.Stop();
HostConnected = false;
}
TcpSocketListener = null;
}
}
我知道在 Compact 框架中没有 Timeout 调用,所以我该怎么做才能避免我的套接字仍然在后台打开。
哦,对了,还有一个额外的信息:
我的 ftp 服务器实际上使用 OpenNETCF FTP 库
好的,我已经使用 SetSocketOption 解决了问题,并且稍微更改了代码。现在是:
public void Start()
{
ServerStarted = true;
if (this.authHandler == null)
this.authHandler = new DefaultAuthHandler();
if (this.fsHandler == null)
this.fsHandler = new DefaultFileSystemHandler();
socketServer = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
/* Here is the change. In this way I can reuse multiple times the
* same address without getting the SocketException.*/
socketServer.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress,
1);
Socket handler = null;
try
{
socketServer.Bind(endpoint);
socketServer.Listen(10);
while (ServerStarted)
{
handler = socketServer.Accept();
try
{
IPEndPoint socketPort = (IPEndPoint)handler.RemoteEndPoint;
Session session = new Session(handler,bufferSize,
this.authHandler.Clone(socketPort),
this.fsHandler.Clone(socketPort));
session.Start();
sessions.Add(session);
p_HostIPAddress = socketPort.Address;
if (p_HostIPAddress != null)
HostConnected = true;
// Purge old sessions
for (int i = sessions.Count - 1; i >= 0; --i)
{
if (!sessions[i].IsOpen)
{
sessions.RemoveAt(i);
--i;
}
}
}
catch(Exception ex)
{
}
}
}
catch (SocketException socketEx)
{
}
finally
{
handler.Close();
foreach (Session s in sessions)
{
s.Stop();
HostConnected = false;
}
}
}