Fixed Server Application bug where server tried to compare client and server IDs.

This commit is contained in:
AquaClouds 2016-03-28 22:39:50 -10:00
commit 45cea7214c
21 changed files with 167 additions and 177 deletions

View File

@ -14,21 +14,12 @@ namespace Server_application
class MainServer : TCPServer
{
//dictionary of connected clients by clientID
private Dictionary<uint, TcpClient> clientList;
private bool running;
private Dictionary<uint, TcpClient> clientList = new Dictionary<uint, TcpClient>();
private bool running = true;
private static readonly object serverCounterLock = new Object();
private uint serverCounter;
private uint serverCounter = 0;
//main server port
private readonly int port;
public MainServer()
{
clientList = new Dictionary<uint, TcpClient>();
running = true;
serverCounter = 0;
port = 20112;
}
private readonly int port = 20112;
public override void startServer()
{
@ -53,8 +44,23 @@ namespace Server_application
//connects to client
//currently connects to any client, possibly add security later
TcpClient client = listener.AcceptTcpClient();
Thread t = new Thread(transmitter);
t.Start(client);
NetworkStream stream = client.GetStream();
BinaryReader reader = new BinaryReader(stream);
byte type = reader.ReadByte();
//client type sends requests to server
if (type == 0)
{
Thread t = new Thread(transmitter);
t.Start(client);
}
//client type waits for requests from server
else if (type == 1)
{
uint thisClientID = reader.ReadUInt32();
//adds client to a dictionary for use
clientList.Add(thisClientID, client);
}
}
}
}
@ -67,178 +73,171 @@ namespace Server_application
NetworkStream stream = thisClient.GetStream();
BinaryReader reader = new BinaryReader(stream);
BinaryWriter writer = new BinaryWriter(stream);
byte type = reader.ReadByte();
uint thisClientID = reader.ReadUInt32();
//client type sends requests to server
if (type == 0)
{
//control while client connected and server running
while (thisClient.Connected && running)
{
//checks if data is available on the clients stream
if (stream.DataAvailable)
{
//gets the type of request sent by the client
byte reqType = reader.ReadByte();
switch (reqType)
{
//request for chat server
case 1:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
{
//increments server counter
serverCounter++;
//creates chat server with serverCounter as its ID in new thread
Thread tC = new Thread(startChatServer);
tC.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
}
break;
//request for resource server
case 2:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
{
//increments server counter
serverCounter++;
//creates resource server with serverCounter as its ID in new thread
Thread tR = new Thread(startResourceServer);
tR.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
}
break;
//request for real time collaboration server
case 3:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
{
//increments server counter
serverCounter++;
//creates real time collaboration server with serverCounter as its ID in new thread
Thread tRTC = new Thread(startRTCServer);
tRTC.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
}
break;
//request to connect a client to a server
case 4:
TcpClient connect;
//reads connection info from stream
byte serType = reader.ReadByte();
uint serverID = reader.ReadUInt32();
uint clientID = reader.ReadUInt32();
//checks if specified client connected, and gets corresponding TcpClient
if (clientList.TryGetValue(clientID, out connect))
{
//creates reader and writer on requested clients stream
NetworkStream cStream = connect.GetStream();
BinaryWriter cWriter = new BinaryWriter(stream);
//checks the type of server connection request for
switch (serType)
{
//connection request for chat server
case 1:
//locks to ensure ChatServer's currentID is not changed
lock (ChatServer.IDLock)
{
//tells chat servers listener that incoming request is for server with specified ID
ChatServer.setCurrentID(serverID);
//tells chat server that a connection is expected
ChatServer.setConnectExpected();
//tells client to connect
cWriter.Write(serType);
cWriter.Write(serverID);
//waits for client to connect to unlock, connectExpected set to false when it does
while (ChatServer.getConnectExpected() && connect.Connected) { }
}
break;
//connection request for resource server
case 2:
//structure same as chat server, see comments for case 1
lock (ResourceServer.IDLock)
{
ResourceServer.setCurrentID(serverID);
ResourceServer.setConnectExpected();
cWriter.Write(serType);
cWriter.Write(serverID);
while (ResourceServer.getConnectExpected() && connect.Connected) { }
}
break;
//connection request for real time collaboration server
case 3:
//structure same as chat server, see comments for case 1
lock (RTCServer.IDLock)
{
RTCServer.setCurrentID(serverID);
RTCServer.setConnectExpected();
cWriter.Write(serType);
cWriter.Write(serverID);
while (RTCServer.getConnectExpected() && connect.Connected) { }
}
break;
}
//checks if requested client still connected
if (connect.Connected)
{
//tells requester client successfully connected if it is
writer.Write(true);
}
else
{
//else indicates connection unsuccessful
writer.Write(false);
}
}
//requested client not connected
else
{
//tells client connection request was unsuccessful
writer.Write(false);
}
break;
//request for status of indicated client
case 5:
TcpClient temp;
//checks if indicated client is in list of connected clients
if (clientList.TryGetValue(reader.ReadUInt32(), out temp))
uint thisClientID = reader.ReadUInt32();
//control while client connected and server running
while (thisClient.Connected && running)
{
//checks if data is available on the clients stream
if (stream.DataAvailable)
{
//gets the type of request sent by the client
byte reqType = reader.ReadByte();
switch (reqType)
{
//request for chat server
case 1:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
{
//increments server counter
serverCounter++;
//creates chat server with serverCounter as its ID in new thread
Thread tC = new Thread(startChatServer);
tC.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
}
break;
//request for resource server
case 2:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
{
//increments server counter
serverCounter++;
//creates resource server with serverCounter as its ID in new thread
Thread tR = new Thread(startResourceServer);
tR.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
}
break;
//request for real time collaboration server
case 3:
//locks to ensure server counter not modified by another thread
lock (serverCounterLock)
{
//increments server counter
serverCounter++;
//creates real time collaboration server with serverCounter as its ID in new thread
Thread tRTC = new Thread(startRTCServer);
tRTC.Start(serverCounter);
//writes the assigned serverID to client
writer.Write(serverCounter);
}
break;
//request to connect a client to a server
case 4:
TcpClient connect;
//reads connection info from stream
byte serType = reader.ReadByte();
uint serverID = reader.ReadUInt32();
uint clientID = reader.ReadUInt32();
//checks if specified client connected, and gets corresponding TcpClient
if (clientList.TryGetValue(clientID, out connect))
{
//creates reader and writer on requested clients stream
NetworkStream cStream = connect.GetStream();
BinaryWriter cWriter = new BinaryWriter(stream);
//checks the type of server connection request for
switch (serType)
{
//tells requester specified client is connected
//connection request for chat server
case 1:
//locks to ensure ChatServer's currentID is not changed
lock (ChatServer.IDLock)
{
//tells chat servers listener that incoming request is for server with specified ID
ChatServer.setCurrentID(serverID);
//tells chat server that a connection is expected
ChatServer.setConnectExpected();
//tells client to connect
cWriter.Write(serType);
cWriter.Write(serverID);
//waits for client to connect to unlock, connectExpected set to false when it does
while (ChatServer.getConnectExpected() && connect.Connected) { }
}
break;
//connection request for resource server
case 2:
//structure same as chat server, see comments for case 1
lock (ResourceServer.IDLock)
{
ResourceServer.setCurrentID(serverID);
ResourceServer.setConnectExpected();
cWriter.Write(serType);
cWriter.Write(serverID);
while (ResourceServer.getConnectExpected() && connect.Connected) { }
}
break;
//connection request for real time collaboration server
case 3:
//structure same as chat server, see comments for case 1
lock (RTCServer.IDLock)
{
RTCServer.setCurrentID(serverID);
RTCServer.setConnectExpected();
cWriter.Write(serType);
cWriter.Write(serverID);
while (RTCServer.getConnectExpected() && connect.Connected) { }
}
break;
}
//checks if requested client still connected
if (connect.Connected)
{
//tells requester client successfully connected if it is
writer.Write(true);
}
else
{
//tells requester specified client is not connected
//else indicates connection unsuccessful
writer.Write(false);
}
break;
}
}
//requested client not connected
else
{
//tells client connection request was unsuccessful
writer.Write(false);
}
break;
//request for status of indicated client
case 5:
TcpClient temp;
//checks if indicated client is in list of connected clients
if (clientList.TryGetValue(reader.ReadUInt32(), out temp))
{
//tells requester specified client is connected
writer.Write(true);
}
else
{
//tells requester specified client is not connected
writer.Write(false);
}
break;
}
}
}
//cleanup
if (writer != null) { writer.Close(); }
if (reader != null) { reader.Close(); }
//removes client from list of connected clients
lock (clientList)
{
clientList.Remove(thisClientID);
}
//client type waits for requests from server
if (type == 1)
{
//adds client to a dictionary for use
if (clientList != null)
TcpClient close;
if (clientList.TryGetValue(thisClientID, out close))
{
clientList.Add(thisClientID, thisClient);
close.GetStream().Close();
close.Close();
clientList.Remove(thisClientID);
}
}
}

View File

@ -1,9 +0,0 @@
C:\cygwin64\home\AquaC\CS-350-410-431-Group-Project\Server application\Server application\bin\Debug\Server application.exe.config
C:\cygwin64\home\AquaC\CS-350-410-431-Group-Project\Server application\Server application\bin\Debug\Server application.exe
C:\cygwin64\home\AquaC\CS-350-410-431-Group-Project\Server application\Server application\bin\Debug\Server application.pdb
C:\cygwin64\home\AquaC\CS-350-410-431-Group-Project\Server application\Server application\obj\Debug\Server application.csprojResolveAssemblyReference.cache
C:\cygwin64\home\AquaC\CS-350-410-431-Group-Project\Server application\Server application\obj\Debug\Server_application.Form1.resources
C:\cygwin64\home\AquaC\CS-350-410-431-Group-Project\Server application\Server application\obj\Debug\Server_application.Properties.Resources.resources
C:\cygwin64\home\AquaC\CS-350-410-431-Group-Project\Server application\Server application\obj\Debug\Server application.csproj.GenerateResource.Cache
C:\cygwin64\home\AquaC\CS-350-410-431-Group-Project\Server application\Server application\obj\Debug\Server application.exe
C:\cygwin64\home\AquaC\CS-350-410-431-Group-Project\Server application\Server application\obj\Debug\Server application.pdb