added exception catching for dataavailable
This commit is contained in:
parent
75d605b5c7
commit
917d49dce9
Binary file not shown.
@ -76,17 +76,22 @@ namespace GameCreatorGroupProject
|
||||
//reads messages
|
||||
while (client.Connected && !dc)
|
||||
{
|
||||
//possible issue if server has not yet read sent data
|
||||
if (stream.DataAvailable)
|
||||
//if disconnected dataavailable field will throw an acception, and should set connected to false
|
||||
try
|
||||
{
|
||||
//reads stream data
|
||||
message = reader.ReadLine();
|
||||
//possible issue if server has not yet read sent data
|
||||
if (stream.DataAvailable)
|
||||
{
|
||||
//reads stream data
|
||||
message = reader.ReadLine();
|
||||
|
||||
|
||||
//add code to write message to chat interface
|
||||
MessageBox.Show(message);
|
||||
//add code to write message to chat interface
|
||||
MessageBox.Show(message);
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception) { }
|
||||
|
||||
}
|
||||
MessageBox.Show("Disconnected.", "Unable to connect to chat server.", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
|
@ -139,33 +139,37 @@ namespace GameCreatorGroupProject
|
||||
//checks if both clients are still connected, and if disconnect was called
|
||||
while (client.Connected && staticClient.Connected && !dc)
|
||||
{
|
||||
//checks if server has sent a connection request
|
||||
if (stream.DataAvailable)
|
||||
try
|
||||
{
|
||||
//reads stream data
|
||||
connectInfo = new Tuple<byte, uint>(reader.ReadByte(), reader.ReadUInt32());
|
||||
//creates requested client
|
||||
//might need to add way to control clients (eg disconnect, etc), probably add to a list or somthing
|
||||
switch (connectInfo.Item1)
|
||||
//checks if server has sent a connection request
|
||||
if (stream.DataAvailable)
|
||||
{
|
||||
case 1:
|
||||
c = new ChatClient(connectInfo.Item2);
|
||||
break;
|
||||
case 2:
|
||||
c = new ResourceClient(connectInfo.Item2);
|
||||
break;
|
||||
case 3:
|
||||
c = new RTCClient(connectInfo.Item2);
|
||||
break;
|
||||
//reads stream data
|
||||
connectInfo = new Tuple<byte, uint>(reader.ReadByte(), reader.ReadUInt32());
|
||||
//creates requested client
|
||||
//might need to add way to control clients (eg disconnect, etc), probably add to a list or somthing
|
||||
switch (connectInfo.Item1)
|
||||
{
|
||||
case 1:
|
||||
c = new ChatClient(connectInfo.Item2);
|
||||
break;
|
||||
case 2:
|
||||
c = new ResourceClient(connectInfo.Item2);
|
||||
break;
|
||||
case 3:
|
||||
c = new RTCClient(connectInfo.Item2);
|
||||
break;
|
||||
}
|
||||
//connects client to server in new thread
|
||||
Thread t = new Thread(startClient);
|
||||
//gives thread enhanced priority to attempt to connect as quickly as possible
|
||||
t.Priority = ThreadPriority.AboveNormal;
|
||||
t.Start(c);
|
||||
available.Enqueue(c);
|
||||
clients.Add(c);
|
||||
}
|
||||
//connects client to server in new thread
|
||||
Thread t = new Thread(startClient);
|
||||
//gives thread enhanced priority to attempt to connect as quickly as possible
|
||||
t.Priority = ThreadPriority.AboveNormal;
|
||||
t.Start(c);
|
||||
available.Enqueue(c);
|
||||
clients.Add(c);
|
||||
}
|
||||
catch (Exception) { }
|
||||
}
|
||||
//tells user if client disconnected
|
||||
MessageBox.Show("Disconnected.", "Unable to connect to server.", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -129,26 +129,29 @@ namespace Server_application
|
||||
NetworkStream outStream;
|
||||
while (thisClient.Connected && running)
|
||||
{
|
||||
//checks if data is available on the clients stream
|
||||
if (inStream.DataAvailable)
|
||||
try
|
||||
{
|
||||
message = reader.ReadLine();
|
||||
//sends data to all clients in chat
|
||||
foreach (KeyValuePair<TcpClient, string> c in clientList)
|
||||
//checks if data is available on the clients stream
|
||||
if (inStream.DataAvailable)
|
||||
{
|
||||
string clientName;
|
||||
|
||||
//creates StreamWriter for current outgoing client
|
||||
outStream = c.Key.GetStream();
|
||||
writer = new StreamWriter(outStream);
|
||||
//appends sender name to beginning of message
|
||||
clientList.TryGetValue(thisClient, out clientName);
|
||||
//writes sender and message to outgoing stream
|
||||
writer.WriteLine(clientName + ": " + message.ToString());
|
||||
writer.Flush();
|
||||
message = reader.ReadLine();
|
||||
//sends data to all clients in chat
|
||||
foreach (KeyValuePair<TcpClient, string> c in clientList)
|
||||
{
|
||||
string clientName;
|
||||
|
||||
//creates StreamWriter for current outgoing client
|
||||
outStream = c.Key.GetStream();
|
||||
writer = new StreamWriter(outStream);
|
||||
//appends sender name to beginning of message
|
||||
clientList.TryGetValue(thisClient, out clientName);
|
||||
//writes sender and message to outgoing stream
|
||||
writer.WriteLine(clientName + ": " + message.ToString());
|
||||
writer.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception) { }
|
||||
}
|
||||
|
||||
if (writer != null) { writer.Close(); }
|
||||
|
@ -36,7 +36,7 @@ namespace Server_application
|
||||
listener.Start();
|
||||
}
|
||||
//displays error box if unable to start server
|
||||
catch (SocketException e)
|
||||
catch (SocketException)
|
||||
{
|
||||
MessageBox.Show("A network error has occured.", "Unable to start chat server.", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
@ -85,165 +85,169 @@ namespace Server_application
|
||||
//control while client connected and server running
|
||||
while (thisClient.Connected && running)
|
||||
{
|
||||
//checks if data is available on the clients stream
|
||||
if (stream.DataAvailable)
|
||||
try
|
||||
{
|
||||
//gets the type of request sent by the client
|
||||
byte reqType = reader.ReadByte();
|
||||
switch (reqType)
|
||||
//checks if data is available on the clients stream
|
||||
if (stream.DataAvailable)
|
||||
{
|
||||
//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(cStream);
|
||||
//indicates whether client successfully connected
|
||||
bool success = false;
|
||||
//checks the type of server connection request for
|
||||
switch (serType)
|
||||
//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)
|
||||
{
|
||||
//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);
|
||||
ChatServer.setExpectedClient(clientID);
|
||||
//tells chat server that a connection is expected
|
||||
ChatServer.connectExpected.Set();
|
||||
//tells client to connect
|
||||
cWriter.Write(serType);
|
||||
cWriter.Write(serverID);
|
||||
//waits for client to connect to unlock, connectExpected set to false when it does
|
||||
success = ChatServer.connected.Wait(TimeSpan.FromSeconds(5));
|
||||
//ensures values reset regardless of outcome
|
||||
ChatServer.connectExpected.Reset();
|
||||
ChatServer.connected.Reset();
|
||||
}
|
||||
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.setExpectedClient(clientID);
|
||||
ResourceServer.connectExpected.Set();
|
||||
cWriter.Write(serType);
|
||||
cWriter.Write(serverID);
|
||||
success = ResourceServer.connected.Wait(TimeSpan.FromSeconds(5));
|
||||
//ensures values reset regardless of outcome
|
||||
ResourceServer.connectExpected.Reset();
|
||||
ResourceServer.connected.Reset();
|
||||
}
|
||||
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.setExpectedClient(clientID);
|
||||
RTCServer.connectExpected.Set();
|
||||
cWriter.Write(serType);
|
||||
cWriter.Write(serverID);
|
||||
success = RTCServer.connected.Wait(TimeSpan.FromSeconds(5));
|
||||
//ensures values reset regardless of outcome
|
||||
RTCServer.connectExpected.Reset();
|
||||
RTCServer.connected.Reset();
|
||||
}
|
||||
break;
|
||||
//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);
|
||||
}
|
||||
//checks if requested client still connected
|
||||
if (connect.Connected && success)
|
||||
break;
|
||||
//request for resource server
|
||||
case 2:
|
||||
//locks to ensure server counter not modified by another thread
|
||||
lock (serverCounterLock)
|
||||
{
|
||||
//tells requester client successfully connected if it is
|
||||
//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(cStream);
|
||||
//indicates whether client successfully connected
|
||||
bool success = false;
|
||||
//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);
|
||||
ChatServer.setExpectedClient(clientID);
|
||||
//tells chat server that a connection is expected
|
||||
ChatServer.connectExpected.Set();
|
||||
//tells client to connect
|
||||
cWriter.Write(serType);
|
||||
cWriter.Write(serverID);
|
||||
//waits for client to connect to unlock, connectExpected set to false when it does
|
||||
success = ChatServer.connected.Wait(TimeSpan.FromSeconds(5));
|
||||
//ensures values reset regardless of outcome
|
||||
ChatServer.connectExpected.Reset();
|
||||
ChatServer.connected.Reset();
|
||||
}
|
||||
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.setExpectedClient(clientID);
|
||||
ResourceServer.connectExpected.Set();
|
||||
cWriter.Write(serType);
|
||||
cWriter.Write(serverID);
|
||||
success = ResourceServer.connected.Wait(TimeSpan.FromSeconds(5));
|
||||
//ensures values reset regardless of outcome
|
||||
ResourceServer.connectExpected.Reset();
|
||||
ResourceServer.connected.Reset();
|
||||
}
|
||||
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.setExpectedClient(clientID);
|
||||
RTCServer.connectExpected.Set();
|
||||
cWriter.Write(serType);
|
||||
cWriter.Write(serverID);
|
||||
success = RTCServer.connected.Wait(TimeSpan.FromSeconds(5));
|
||||
//ensures values reset regardless of outcome
|
||||
RTCServer.connectExpected.Reset();
|
||||
RTCServer.connected.Reset();
|
||||
}
|
||||
break;
|
||||
}
|
||||
//checks if requested client still connected
|
||||
if (connect.Connected && success)
|
||||
{
|
||||
//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))
|
||||
{
|
||||
//tells requester specified client is connected
|
||||
writer.Write(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
//else indicates connection unsuccessful
|
||||
//tells requester specified client is not connected
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception) { }
|
||||
}
|
||||
|
||||
//cleanup
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,2 +1,11 @@
|
||||
C:\Users\Jared\Documents\GitHub\CS-350-410-431-Group-Project\Server application\Server application\bin\Debug\Server application.exe.config
|
||||
C:\Users\Jared\Documents\GitHub\mod14\Server application\Server application\bin\Debug\Server application.exe.config
|
||||
C:\Users\Jared\Documents\GitHub\modalot\Server application\Server application\bin\Debug\Server application.exe.config
|
||||
C:\Users\Jared\Documents\GitHub\modalot\Server application\Server application\bin\Debug\Server application.exe
|
||||
C:\Users\Jared\Documents\GitHub\modalot\Server application\Server application\bin\Debug\Server application.pdb
|
||||
C:\Users\Jared\Documents\GitHub\modalot\Server application\Server application\obj\Debug\Server application.csprojResolveAssemblyReference.cache
|
||||
C:\Users\Jared\Documents\GitHub\modalot\Server application\Server application\obj\Debug\Server_application.Form1.resources
|
||||
C:\Users\Jared\Documents\GitHub\modalot\Server application\Server application\obj\Debug\Server_application.Properties.Resources.resources
|
||||
C:\Users\Jared\Documents\GitHub\modalot\Server application\Server application\obj\Debug\Server application.csproj.GenerateResource.Cache
|
||||
C:\Users\Jared\Documents\GitHub\modalot\Server application\Server application\obj\Debug\Server application.exe
|
||||
C:\Users\Jared\Documents\GitHub\modalot\Server application\Server application\obj\Debug\Server application.pdb
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user