added exception catching for dataavailable

This commit is contained in:
princessbobsanta 2016-04-23 15:13:11 -10:00
parent 75d605b5c7
commit 917d49dce9
20 changed files with 216 additions and 191 deletions

View File

@ -76,17 +76,22 @@ namespace GameCreatorGroupProject
//reads messages //reads messages
while (client.Connected && !dc) while (client.Connected && !dc)
{ {
//possible issue if server has not yet read sent data //if disconnected dataavailable field will throw an acception, and should set connected to false
if (stream.DataAvailable) try
{ {
//reads stream data //possible issue if server has not yet read sent data
message = reader.ReadLine(); if (stream.DataAvailable)
{
//reads stream data
message = reader.ReadLine();
//add code to write message to chat interface //add code to write message to chat interface
MessageBox.Show(message); MessageBox.Show(message);
}
} }
catch (Exception) { }
} }
MessageBox.Show("Disconnected.", "Unable to connect to chat server.", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Disconnected.", "Unable to connect to chat server.", MessageBoxButtons.OK, MessageBoxIcon.Information);

View File

@ -139,33 +139,37 @@ namespace GameCreatorGroupProject
//checks if both clients are still connected, and if disconnect was called //checks if both clients are still connected, and if disconnect was called
while (client.Connected && staticClient.Connected && !dc) while (client.Connected && staticClient.Connected && !dc)
{ {
//checks if server has sent a connection request try
if (stream.DataAvailable)
{ {
//reads stream data //checks if server has sent a connection request
connectInfo = new Tuple<byte, uint>(reader.ReadByte(), reader.ReadUInt32()); if (stream.DataAvailable)
//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: //reads stream data
c = new ChatClient(connectInfo.Item2); connectInfo = new Tuple<byte, uint>(reader.ReadByte(), reader.ReadUInt32());
break; //creates requested client
case 2: //might need to add way to control clients (eg disconnect, etc), probably add to a list or somthing
c = new ResourceClient(connectInfo.Item2); switch (connectInfo.Item1)
break; {
case 3: case 1:
c = new RTCClient(connectInfo.Item2); c = new ChatClient(connectInfo.Item2);
break; 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 //tells user if client disconnected
MessageBox.Show("Disconnected.", "Unable to connect to server.", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Disconnected.", "Unable to connect to server.", MessageBoxButtons.OK, MessageBoxIcon.Information);

View File

@ -129,26 +129,29 @@ namespace Server_application
NetworkStream outStream; NetworkStream outStream;
while (thisClient.Connected && running) while (thisClient.Connected && running)
{ {
//checks if data is available on the clients stream try
if (inStream.DataAvailable)
{ {
message = reader.ReadLine(); //checks if data is available on the clients stream
//sends data to all clients in chat if (inStream.DataAvailable)
foreach (KeyValuePair<TcpClient, string> c in clientList)
{ {
string clientName; message = reader.ReadLine();
//sends data to all clients in chat
//creates StreamWriter for current outgoing client foreach (KeyValuePair<TcpClient, string> c in clientList)
outStream = c.Key.GetStream(); {
writer = new StreamWriter(outStream); string clientName;
//appends sender name to beginning of message
clientList.TryGetValue(thisClient, out clientName); //creates StreamWriter for current outgoing client
//writes sender and message to outgoing stream outStream = c.Key.GetStream();
writer.WriteLine(clientName + ": " + message.ToString()); writer = new StreamWriter(outStream);
writer.Flush(); //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(); } if (writer != null) { writer.Close(); }

View File

@ -36,7 +36,7 @@ namespace Server_application
listener.Start(); listener.Start();
} }
//displays error box if unable to start server //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); 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 //control while client connected and server running
while (thisClient.Connected && running) while (thisClient.Connected && running)
{ {
//checks if data is available on the clients stream try
if (stream.DataAvailable)
{ {
//gets the type of request sent by the client //checks if data is available on the clients stream
byte reqType = reader.ReadByte(); if (stream.DataAvailable)
switch (reqType)
{ {
//request for chat server //gets the type of request sent by the client
case 1: byte reqType = reader.ReadByte();
//locks to ensure server counter not modified by another thread switch (reqType)
lock (serverCounterLock) {
{ //request for chat server
//increments server counter case 1:
serverCounter++; //locks to ensure server counter not modified by another thread
//creates chat server with serverCounter as its ID in new thread lock (serverCounterLock)
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)
{ {
//connection request for chat server //increments server counter
case 1: serverCounter++;
//locks to ensure ChatServer's currentID is not changed //creates chat server with serverCounter as its ID in new thread
lock (ChatServer.IDLock) Thread tC = new Thread(startChatServer);
{ tC.Start(serverCounter);
//tells chat servers listener that incoming request is for server with specified ID //writes the assigned serverID to client
ChatServer.setCurrentID(serverID); writer.Write(serverCounter);
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 break;
if (connect.Connected && success) //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); writer.Write(true);
} }
else else
{ {
//else indicates connection unsuccessful //tells requester specified client is not connected
writer.Write(false); 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 //cleanup

View File

@ -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\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\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