public static Boolean requestConnect(IPAddress address, int port) { bool connectionSuccessful = false; bool userDone = false; UdpClient client = new UdpClient(); Globals.discoveredUDPClients.Clear(); //refresh discovered while (!userDone) { int attempts = 0; //refresh number of attempts Console.WriteLine("\n\nRequesting connection to " + address + ":"); while (attempts <= Globals.maxNetworkingAttempts && !connectionSuccessful) { Console.WriteLine(" Attempt " + attempts + " of " + Globals.maxNetworkingAttempts + "..."); IPEndPoint ip = new IPEndPoint(address, port); byte[] bytes = new udpMessage("CONNECT", 1, 0, "").getByteArray(); client.Send(bytes, bytes.Length, ip); while (!Globals.discoveredUDPClients.IsEmpty) { Tuple <IPAddress, udpMessage> currentItem; if (Globals.discoveredUDPClients.TryDequeue(out currentItem) && currentItem.Item1.Equals(address) && currentItem.Item2.command.Equals("CONNECT")) { connectionSuccessful = true; userDone = true; } } Thread.Sleep(Globals.ThreadSleepTime); attempts++; } if (!connectionSuccessful) { Console.Write(" There was no response; would you like to try again?\n >> "); if (checkMenuResponseFalse(Console.ReadLine())) { userDone = true; } } } client.Close(); if (connectionSuccessful) { Console.WriteLine(" :) Connection approved!"); } else { Console.WriteLine(" !! Connection Request Unsuccessful"); } return(connectionSuccessful); }
public static Boolean udpPlayRockPaperScissors(IPAddress iPAddress, int port, out bool winner) { winner = false; Boolean winnerFound = false; if (Globals.rpsDebugging) { Console.WriteLine(" Playing rock, paper, scissors!"); } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); UdpClient client = new UdpClient(); //Globals.discoveredUDPClients.Clear(); //refresh discovered int rpsThrow = new Random().Next(0, 10000); int currentMessageId = 333; while (!winnerFound) { if (Globals.rpsDebugging) { Console.WriteLine(" :) Your throw: " + rpsThrow); } IPEndPoint ip = new IPEndPoint(iPAddress, port); byte[] bytes = new udpMessage("RPS", currentMessageId, rpsThrow, "").getByteArray(); for (int i = 0; i < Globals.udpPulse; i++) { client.Send(bytes, bytes.Length, ip); } Thread.Sleep(Globals.ThreadSleepTime); while (!Globals.discoveredUDPClients.IsEmpty) { Tuple <IPAddress, udpMessage> currentItem; udpMessage lastItem = new udpMessage("RPS", currentMessageId - 1, 0, ""); if (Globals.discoveredUDPClients.TryDequeue(out currentItem) && currentItem.Item1.Equals(iPAddress)) { int otherThrow = currentItem.Item2.number; if (Globals.rpsDebugging) { Console.WriteLine(" Their throw: " + currentItem.Item2.number); } if (currentItem.Item2.messageId == currentMessageId) { if (otherThrow > rpsThrow) { winnerFound = true; if (Globals.rpsDebugging) { Console.WriteLine(" They won!"); } } else if (rpsThrow > otherThrow) { winner = true; winnerFound = true; if (Globals.rpsDebugging) { Console.WriteLine(" :) You won!"); } } else { if (Globals.rpsDebugging) { Console.WriteLine(" Tie? {0} from {1}", currentItem.Item2.number, currentItem.Item1); } } } } } if (stopwatch.ElapsedMilliseconds > Globals.maxRockPaperScissorsTime) { Console.WriteLine(" !! Chat host could not be resolved."); break; } Thread.Sleep(Globals.ThreadSleepTime); } client.Close(); return(winnerFound); }
public static Boolean startChatClient(IPAddress ipAddress, int port) { bool chatClientSuccess = false, done = false, isWinner = false, winnerFound = false, firstTime = true; Console.WriteLine("\n\nStarting TCP Chat client..."); winnerFound = udpPlayRockPaperScissors(ipAddress, Globals.udpPort, out isWinner); if (winnerFound) { if (isWinner) { bool otherReady = false; int attempts = 0; while (attempts < Globals.maxNetworkingAttempts) { while (!Globals.discoveredUDPClients.IsEmpty) { Tuple <IPAddress, udpMessage> currentItem; if (Globals.discoveredUDPClients.TryDequeue(out currentItem) && currentItem.Item1.Equals(ipAddress)) { if (currentItem.Item2.command.Equals("TCPREADY")) { otherReady = true; attempts = Globals.maxNetworkingAttempts; } } } attempts++; Thread.Sleep(Globals.ThreadSleepTime); } if (otherReady) { try { TcpClient tcpClient = new TcpClient(ipAddress.ToString(), port); NetworkStream nwStream = tcpClient.GetStream(); string messageToSend = ""; while (!done) { if (firstTime) { Console.Write(" Enter your message, type \"/exit\" when it's your turn to quit!\n >> "); firstTime = false; } else { Console.Write(" >> "); } messageToSend = Console.ReadLine(); if (messageToSend.Equals("/exit") || messageToSend.Equals("/quit") || messageToSend.Equals("/q")) { done = true; Byte[] dataToSend = System.Text.Encoding.ASCII.GetBytes(" !! The other user has left."); nwStream.Write(dataToSend, 0, dataToSend.Length); } else { Byte[] dataToSend = System.Text.Encoding.ASCII.GetBytes(messageToSend); nwStream.Write(dataToSend, 0, dataToSend.Length); dataToSend = new Byte[256]; // String to store the response ASCII representation. String responseData = String.Empty; // Read the first batch of the TcpServer response bytes. //Int32 bytes = nwStream.Read(data, 0, data.Length); //responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); //Console.WriteLine(" Received message:\n ", responseData); byte[] buffer = new byte[tcpClient.ReceiveBufferSize]; int bytesRead = nwStream.Read(buffer, 0, tcpClient.ReceiveBufferSize); string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine(" -- " + dataReceived); } } // Close everything. nwStream.Close(); tcpClient.Close(); } catch (ArgumentNullException e) { if (Globals.debugging) { Console.WriteLine("ArgumentNullException: {0}", e); } Console.WriteLine(" !! Error; try again later!"); } catch (SocketException e) { if (Globals.debugging) { Console.WriteLine("SocketException: {0}", e); } Console.WriteLine(" !! Error, the socket was likely still in use; try again in a minute!"); } catch (System.IO.IOException e) { if (Globals.debugging) { Console.WriteLine("IO Exception: " + e); } Console.WriteLine(" !! Error; the user likely left."); } } else { Console.WriteLine(" !! Could not connect to other client."); } } else { //---listen at the specified IP and port no.--- TcpListener listener = new TcpListener(IPAddress.Any, port); Console.WriteLine(" The other person gets to chat first!"); try { listener.Start(); UdpClient udpClient = new UdpClient(); IPEndPoint ip = new IPEndPoint(ipAddress, Globals.udpPort); byte[] udpBytes = new udpMessage("TCPREADY", 1, 1, "").getByteArray(); for (int i = 0; i < Globals.udpPulse; i++) { udpClient.Send(udpBytes, udpBytes.Length, ip); } udpClient.Close(); TcpClient tcpClient = listener.AcceptTcpClient(); NetworkStream nwStream = tcpClient.GetStream(); byte[] buffer = new byte[tcpClient.ReceiveBufferSize]; int bytesRead = nwStream.Read(buffer, 0, tcpClient.ReceiveBufferSize); string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine(" -- " + dataReceived); string messageToSend = ""; while (!done) { if (firstTime) { Console.Write(" Enter your message, type \"/exit\" when it's your turn to quit!\n >> "); firstTime = false; } else { Console.Write(" >> "); } messageToSend = Console.ReadLine(); if (messageToSend.Equals("/exit") || messageToSend.Equals("/quit") || messageToSend.Equals("/q")) { done = true; Byte[] dataToSend = System.Text.Encoding.ASCII.GetBytes(" !! The other user has left."); nwStream.Write(dataToSend, 0, dataToSend.Length); } else { Byte[] dataToSend = System.Text.Encoding.ASCII.GetBytes(messageToSend); nwStream.Write(dataToSend, 0, dataToSend.Length); dataToSend = new Byte[256]; String responseData = String.Empty; buffer = new byte[tcpClient.ReceiveBufferSize]; bytesRead = nwStream.Read(buffer, 0, tcpClient.ReceiveBufferSize); dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine(" -- " + dataReceived); } } tcpClient.Close(); listener.Stop(); } catch (ArgumentNullException e) { if (Globals.debugging) { Console.WriteLine("ArgumentNullException: {0}", e); } Console.WriteLine(" !! Error; try again later!"); } catch (SocketException e) { if (Globals.debugging) { Console.WriteLine("SocketException: {0}", e); } Console.WriteLine(" !! Error, the socket was likely still in use; try again in a minute!"); } catch (System.IO.IOException e) { if (Globals.debugging) { Console.WriteLine("IO Exception: " + e); } Console.WriteLine(" !! Error; the user likely left."); } } } Console.WriteLine(" Exiting the chat client"); return(chatClientSuccess); }
public static bool searchNearby() { var searchSuccess = false; var keepSearching = true; int maxAttempts = 10; int attempts = 0; var reportedClients = new List <IPAddress>(); //for UI reporting and interaction while (keepSearching) { UdpClient client = new UdpClient(); attempts = 0; //refresh number of attempts Globals.discoveredUDPClients.Clear(); //refresh discovered reportedClients = new List <IPAddress>(); //refresh valid available if (Globals.debugging) { reportedClients.Add(IPAddress.Parse(Globals.defaultIPAddress)); } Console.WriteLine("\n\nSearching...\nDiscovered Clients:"); while (attempts <= maxAttempts) { IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, Globals.udpPort); byte[] bytes = new udpMessage("SEARCH", 1, 0, "").getByteArray(); client.Send(bytes, bytes.Length, ip); while (!Globals.discoveredUDPClients.IsEmpty) { Tuple <IPAddress, udpMessage> currentItem; if (Globals.discoveredUDPClients.TryDequeue(out currentItem) && !reportedClients.Contains(currentItem.Item1) && !Globals.localIPAddresses.Contains(currentItem.Item1) && currentItem.Item2.command.Equals("SEARCH")) { reportedClients.Add(currentItem.Item1); Console.WriteLine(" [" + reportedClients.IndexOf(currentItem.Item1) + "] " + currentItem.Item1); } } Thread.Sleep(Globals.ThreadSleepTime); attempts++; } client.Close(); if (reportedClients.Count <= 0) { Console.WriteLine(" ! No nearby clients found."); } Console.Write(" Search again?\n >> "); var input = Console.ReadLine(); if (checkMenuResponseFalse(input)) { keepSearching = false; } } if (reportedClients.Count > 0) { int menuoptions = reportedClients.Count + 1; Console.WriteLine("\n\nChoose a client to connect to!"); foreach (IPAddress item in reportedClients) { Console.WriteLine(" [" + (reportedClients.IndexOf(item) + 1) + "] " + item); } Console.Write(" [0] exit\n >> "); int selectedMenuOption = 0; if (!Int32.TryParse(Console.ReadLine(), out selectedMenuOption) || selectedMenuOption > menuoptions || selectedMenuOption < 1) { selectedMenuOption = 0; //check for invalid selection } else { Globals.targetIpAddress = reportedClients[selectedMenuOption - 1]; //subtract 1 to offset for zero being exit Console.WriteLine(" Option {0} chosen, {1} set as target IP Address.", selectedMenuOption, Globals.targetIpAddress); searchSuccess = true; } } return(searchSuccess); }