private void waitforClientConnection() { Byte[] ipAddr = new Byte[4]; // ipAddr[0] = 127; ipAddr[1] = 0; ipAddr[2] = 0; ipAddr[3] = 1; IPAddress ipAddress = new IPAddress(ipAddr); IPEndPoint listenerPort = new IPEndPoint(IPAddress.Any, 30000); listener = new TcpListener(listenerPort); listener.Start(); try { while (true) { connection = listener.AcceptSocket(); //blocking call - will wait for a connection request // MessageBox.Show("Connection Accepted"); connectedUser newConnection = new connectedUser(); userSockets.Add(new connectedUser { userSocket = connection, commStream = new NetworkStream(connection), Connected = true, ID = userId++ }); communicationThreads.Add(new Thread(new ParameterizedThreadStart(commProcedure))); communicationThreads[communicationThreads.Count - 1].Start(userSockets[userSockets.Count - 1]); } } catch (SocketException sockExcep) { listener = null; } }
private void commProcedure(object obj) //should only be passing in userSockets { connectedUser curUser = null; if (obj is connectedUser) { curUser = (connectedUser)obj; } object temp; try { while (true) { if (curUser != null) { try { temp = formatter.Deserialize(curUser.commStream); if (temp is Packets.connectPacket) { Packets.connectPacket msg = (Packets.connectPacket)temp; if (msg.p2p) { curUser.UserName = msg.clientUser; IPAddress userIP = null; foreach (var user in userSockets) { // find the requested user's IPAddress if (user.UserName == msg.targetUser) { userIP = user.UserIpAddress; } } //send target user IP Address to requester Packets.IpAddressPacket targIp = new Packets.IpAddressPacket(); targIp.p2pIpAddress = userIP; formatter.Serialize(curUser.commStream, targIp); //returns IpAddress type object to request to connect } else { curUser.UserName = msg.clientUser; IPEndPoint curUserIpPoint = curUser.userSocket.RemoteEndPoint as IPEndPoint; curUser.UserIpAddress = curUserIpPoint.Address; curUser.Connected = true; connectedClientsList.Add(curUser.UserName); Packets.clientListPackets clientList = new Packets.clientListPackets(); clientList.userList = connectedClientsList; formatter.Serialize(curUser.commStream, clientList); if (!lstConnectedUsers.Dispatcher.CheckAccess()) { Dispatcher.BeginInvoke(new scribe(writeToListBox), connectedClientsList); } } } if (temp is Packets.messagePacket) { Packets.messagePacket msg = (Packets.messagePacket)temp; // for each user connected send message received to all // except the sender foreach (var user in userSockets) { if (user.ID != curUser.ID && user.Connected) { formatter.Serialize(user.commStream, msg); } } } if (temp is Packets.disconnectPacket) { /// remove the user from the server and clean up the thread/socket Packets.disconnectPacket msg = (Packets.disconnectPacket)temp; foreach (var user in userSockets.Where(u => u.UserName == msg.clientUser)) { user.Connected = false; Id = user.UserName; } //disconnect socket and clean up threads // userSockets[userId - 1].Connected = false; //update the connectedClientList and notify other clients connectedClientsList.Remove(msg.clientUser); Packets.clientListPackets clientList = new Packets.clientListPackets(); clientList.userList = connectedClientsList; formatter.Serialize(curUser.commStream, clientList); if (!lstConnectedUsers.Dispatcher.CheckAccess()) { Dispatcher.BeginInvoke(new scribe(writeToListBox), connectedClientsList); } //shutdown the connection to the client and kill the thread int loc = userSockets.FindIndex( delegate(connectedUser user) { return(user.UserName == Id); } ); userSockets[loc].userSocket.Shutdown(SocketShutdown.Both); userSockets[loc].userSocket.Dispose(); userSockets[loc].userSocket.Close(); userSockets.RemoveAt(loc); try { communicationThreads[loc].Abort(); //abort will throw thread exception here } catch (ThreadAbortException e) { //do nothing if the thread is being terminated here //handle the exception thrown by Abort() without the message from the disconnect communicationThreads.RemoveAt(loc); this.userId--; //class level userId } //finally { //} } } catch (SerializationException e) { } } } } catch (SocketException e) { MessageBox.Show("Client Disconnected" + e.ToString()); } }