private void dataRecieved(IAsyncResult iare) { Debug.log("Data recieved!"); try { int bytesRead = mainCsocket.EndReceive(iare); if (bytesRead < 1) { //They disconnected MyConsole.errorLine("Connection has been closed by remote host."); try { mainCsocket.Shutdown(SocketShutdown.Both); } catch (Exception e) { Debug.log(e.ToString()); } mainCsocket = null; return; } Debug.log("Bytes Read: " + bytesRead); byte[] buffer = (byte[])iare.AsyncState; string rec = new string(Encoding.UTF8.GetChars(buffer, 0, bytesRead)); if (rec == "" || rec.TrimStart(' ') == "") { //caca } else if (rec.StartsWith("/jazyisawesome")) //It's my sig so I know this is my app. { if (!isJapp) { isJapp = true; sendString("/jazyisawesome"); //Circlejerk!! :D } Debug.log("My app has connected to me."); } else if (rec.StartsWith("/jia")) { MyConsole.color(ConsoleColor.Green, DateTime.Now.ToString("MMM d, h:m:s-")); MyConsole.write(rec.Substring(4)); } else { MyConsole.write(rec); //TODO send it back so they can see and check connectivity. NO } waitForData(); } catch (SocketException se) { MyConsole.errorLine("Connection was forcibly closed!"); //mainCsocket = null; Debug.log(se.ToString()); } catch (Exception e) { Debug.log(e.ToString()); } }
public Program(string[] args, bool setPort) { Console.Title = "Jazy's Chat App"; MyConsole.colorLine(ConsoleColor.White, "Jazy Chat App. v1.0 Contact: [email protected]\nType /help for a list of commands.\n"); int port = 8000; try { if (args != null && args.Length > 0) { int remotePort = 8000; if (args[0].Contains(':')) { remotePort = int.Parse(args[0].Split(':')[1]); } else if (args.Length > 1) { remotePort = int.Parse(args[1]); } if (args.Length > 2) { port = int.Parse(args[2]); } MyConsole.colorLine(ConsoleColor.White, "Connecting you to {0}:{1}", args[0], args[1]); connectTo(args[0], remotePort); } else if (setPort) { MyConsole.write("Please enter a port number to start listening on: "); port = int.Parse(Console.ReadLine()); } //mainCsocket = null; mainSsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); mainSsocket.Bind(new IPEndPoint(IPAddress.Any, port)); mainSsocket.Listen(4); mainSsocket.BeginAccept(new AsyncCallback(onAccept), null); MyConsole.writeLine("Listening for incoming connections on port {0}", port); } catch (FormatException fe) { MyConsole.errorLine("Error: {0}... Restarting...", fe.Message); Debug.log(fe.ToString()); Restart(); } catch (Exception e) { MyConsole.errorLine("Error starting app: {0}\nRestarting...", e.Message); Debug.log(e.ToString()); Restart(); } string input = ""; do { input = Console.ReadLine(); if (input.StartsWith("/ping")) { if (input.StartsWith("/ping ")) { //They have an address so lets do a proper ping pingAdd(input.Substring(6)); } else if (mainCsocket != null && mainCsocket.Connected) { //They are ping the connected person. pingAdd(mainCsocket.RemoteEndPoint.ToString().Split(':')[0]); } else { MyConsole.errorLine("Cannot ping."); } } else if (input == "/help") { MyConsole.colorLine(ConsoleColor.Green, "Commandline startup --> appname ([hostname/ip to connect to] (port to connect to default 8000) (server port default 8000)]\n\n" + "() Optional [] Required\n" + "/connected -- Returns wether you are currently connected or not.\n" + "/ping (hostname) -- If (hostname) is supplied pings (hostname) else pings remote connection.\n" + "/connect [hostname] -- Attempts to connect to [hostname] or ip address.\n" + "/disconnect -- Closes the current connection.\n" + "/restart -- Restarts the app. (Closes any connections in the process).\n" + "/filetext [file path] -- Used to read text from a file and send it in one big chunk.\n" + "/quit -- Exits the application.\n\n" + "MAX OF 250 Characters! To pass more characters use the /filetext command!" ); } else if (input.StartsWith("/connect ")) { try { input = input.Replace("/connect ", ""); if (mainCsocket != null) { mainCsocket.Close(); } int p = 8000; isJapp = false; if (input.Contains(':')) { p = int.Parse(input.Split(':')[1]); input = input.Substring(0, input.IndexOf(':')); } else if (setPort) { MyConsole.write("Enter port number to connect to: "); p = int.Parse(Console.ReadLine()); } connectTo(input, p); } catch (Exception e) { MyConsole.errorLine("Error: {0}", e.Message); } } else if (input == "/connected") { if (mainCsocket != null && mainCsocket.Connected == true) { MyConsole.colorLine(ConsoleColor.Cyan, "You are connected to {0}", mainCsocket.RemoteEndPoint.ToString()); } else { MyConsole.colorLine(ConsoleColor.Cyan, "You are not connected."); } } else if (input.StartsWith("/dis")) { try { mainCsocket.Shutdown(SocketShutdown.Both); } catch (Exception e) { Debug.log(e.ToString()); } finally { mainCsocket = null; MyConsole.colorLine(ConsoleColor.Cyan, "Disconnected"); } } else if (input == "/restart") { Restart(); } else if (input.StartsWith("/filetext ")) { input = input.Substring(10); sendFileText(input); } else if (input == "/debug") { Close(); new Program(args, true); } else if (Encoding.UTF8.GetByteCount(input) > 250) { MyConsole.errorLine("You have passed the 250 char limit! Use /filetext to send big chunks of text. Your message was not sent."); } else { sendString(input); } } while (input != "/quit"); Close(); }