我的问题是我有一个使用NetSockets连接到C#服务器的Silverlight C#客户端。问题是当我们有客户端在代理服务器后面时,连接到服务器时出现问题。我们得到入站连接,然后我们似乎无法再进行通信。已在代理服务器和客户端计算机上检查防火墙。我们已经确认他们的港口是开放的。
以下是我的服务器代码的摘录:
public void StartServer()
{
//Silverlight port range: 4502-4532
_socketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 4530);
_socketListener.Bind(ipLocal);
_socketListener.Listen(4);
while (true)
{
// Set the event to nonsignaled state.
_allDone.Reset();
Console.WriteLine("Begin Accept Connections");
// create the call back for any client connections...
_socketListener.BeginAccept(new AsyncCallback(HandleClientConnectionRequest), null);
Console.WriteLine("Accept Done");
// Wait until a connection is made before continuing.
_allDone.WaitOne();
Console.WriteLine("Waiting for connection.....");
}
}
这是连接回调:
public void HandleClientConnectionRequest(IAsyncResult asyn)
{
try
{
Console.WriteLine("Request For Connection.");
// Signal the main thread to continue.
_allDone.Set();
//Create a packet to hold the client state
CSockPack clientSocket = new CSockPack();
clientSocket.cSocket = _socketListener.EndAccept(asyn); ;
//Wait for incoming data
WaitForData(clientSocket);
Console.WriteLine("New client connected.");
// SendData("New client connected");
clientSocket.cSocket.Send(System.Text.Encoding.ASCII.GetBytes("Authenticate<end>"));
}
catch (ObjectDisposedException)
{
Console.WriteLine("OnClientConnection: Socket has been closed\n");
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}
}
在Silverlight方面:
public Socket _socket;
public DnsEndPoint _endPoint;
public SocketAsyncEventArgs _sendEventArgs;
这是在Silverlight中的Application Start:
_endPoint = new DnsEndPoint(Application.Current.Host.Source.DnsSafeHost, 4530);
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.UserToken = _socket;
args.RemoteEndPoint = _endPoint;
args.Completed += new EventHandler<SocketAsyncEventArgs>(OnSocketConnectCompleted);
_socket.ConnectAsync(args);
有什么想法吗?