C# Sockets TCP client/serve
I have a client and a server that I'm trying to get to speak to eachother over my local network (127.0.0.1).
First, I start the 'server' application:
public override void StartListening(int port, IPAddress address) {
listenClient = new TcpListener(address, port);
listenClient.Start();
listenDelimiter = new IPEndPoint(address, port);
if (listenThread != null) listenThread.Abort();
listenThread = new Thread(new ParameterizedThreadStart(ListenThread));
listenThread.IsBackground = true;
listenThread.Start(port);
}
public void ListenThread(object o) {
while (true) {
try {
Socket incomingConnection = listenClient.AcceptSocket();
NetworkStream conStream = new NetworkStream(incomingConnection);
NetworkConnection nc = new NetworkConnection((incomingConnection.RemoteEndPoint as IPEndPoint).Port, (incomingConnection.RemoteEndPoint as IPEndPoint).Address);
Clients.Add(nc);
nc.MonitorTCPStream(conStream, this);
}
catch (Exception e) {
Console.WriteLine("Server has crashed. Networking error: " + e.Message);
Console.WriteLine("Will restart in 60 seconds...");
Thread.Sleep(60000);
StartListening(WORLDMAP_PORT, IPAddress.Any);
}
}
}
Then the 'client':
try {
if (worldMapConnection != null) worldMapConnection.Close();
worldMapConnection = new TcpClient(address.ToString(), port);
worldMapConnection.Connect(WORLDMAP_ADDRESS, WORLDMAP_PORT);
if (worldMapThread != null) worldMapThread.Abort();
worldMapThread = new Thread(new ParameterizedThreadStart(WorldMapListener));
worldMapThread.IsBackground = true;
worldMapThread.Start();
if (Engine.Renderer.GetTopMenu().GetType() == typeof(MenuContainerConnecting)) Engine.Renderer.CloseMenu();
}
catch (System.Net.Sockets.SocketException e) {
if (Engine.Renderer.GetTopMenu(开发者_Go百科).GetType() == typeof(MenuContainerConnecting)) Engine.Renderer.CloseMenu();
switch (e.SocketErrorCode) {
case SocketError.AddressAlreadyInUse: {
Engine.Renderer.OpenMenu(new MenuContainerNotice("Connection Error", "You already have a copy of the game connected to the game server.", "Okay"));
} break;
case SocketError.ConnectionRefused: {
Engine.Renderer.OpenMenu(new MenuContainerNotice("Connection Error", "The connection was refused by the game server.", "Okay"));
} break;
case SocketError.HostUnreachable: {
Engine.Renderer.OpenMenu(new MenuContainerNotice("Connection Error", "Could not connect to the game server. Check your internet connection.", "Okay"));
} break;
default: {
Engine.Renderer.OpenMenu(new MenuContainerNotice("Connection Error", "Could not connect to the game server. The server may be down for maintenance.", "Okay"));
} break;
}
}
catch (Exception) {
if (Engine.Renderer.GetTopMenu().GetType() == typeof(MenuContainerConnecting)) Engine.Renderer.CloseMenu();
Engine.Renderer.OpenMenu(new MenuContainerNotice("Connection Error", "Could not connect to the game server. An unknown error has occoured.", "Okay"));
}
WORLDMAP_ADDRESS is 127.0.0.1. The port used is the same on both applications (8269).
However, when I run the client, it catches a System.Net.Sockets.SocketException, whose message in the debugger is:
"A connect request was made on an already connected socket 127.0.0.1:8269"
I don't understand this, because my server is using a TcpListener, and the client a TcpClient. Surely this shouldn't be an issue?
These two lines in the client are redundant:
worldMapConnection = new TcpClient(address.ToString(), port);
worldMapConnection.Connect(WORLDMAP_ADDRESS, WORLDMAP_PORT);
Constructing the TcpClient
object with an address and port is sufficient to get it to connect.
精彩评论