private static void processChoice(int userChoice, addressBook myContacts) { switch (userChoice) { case 1: //invoke client.sendMessage Client myClient = new Client(); Console.WriteLine("Who would you like to send a message to? "); myContacts.listAll(); Console.WriteLine("Please enter the number corresponding to the" + " desired recipient: "); int recipient = Convert.ToInt32(Console.ReadLine()); myClient.sendMessage(recipient, 6283, myContacts); break; case 2: //display messages from .txt/database Utilities myUtilities = new Utilities(); myUtilities.readMessages(); break; case 3: //list contacts myContacts.listAll(); break; default: Console.WriteLine("Error: Invalid Input"); break; } }
private static void menu(addressBook myContacts) { int choice = 0; int send = 1; int view = 2; int contacts = 3; int quit = 4; Console.WriteLine("Welcome to TauNet"); do { Console.WriteLine("What would you like to do?"); Console.WriteLine("(" + send + ") Send a Message."); Console.WriteLine("(" + view + ") View Messages."); Console.WriteLine("(" + contacts + ") List Contacts."); Console.WriteLine("(" + quit + ") Quit."); Console.WriteLine("Please enter corresponding number: "); try { choice = Convert.ToInt32(Console.ReadLine()); if (choice != quit) //skip the call to processChoice method if user selects quit. { processChoice(choice, myContacts); } } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } } while (choice != quit); }
//prompt user for input and format input //to the protocol specification byte[] enterMessage(int hostname, addressBook myContacts) { //code 13 and code 10 in byte array for NL byte[] newLine = Encoding.ASCII.GetBytes(Environment.NewLine); ASCIIEncoding asen = new ASCIIEncoding(); byte[] message; string sender = "itsjonnyjyo"; byte[] bSender = asen.GetBytes(sender); string recipient = (myContacts.returnUsername(hostname)).ToLower(); byte[] bRecipient = asen.GetBytes(recipient); string header = ("version: 0.2"); byte[] bHeader = asen.GetBytes(header); Console.Write("Enter the string to be transmitted : "); string body = (Console.ReadLine().ToLower()); byte[] bBody = asen.GetBytes(body); List<byte> messageList = new List<byte>(); //Concat the elements of the message into ONE byte array messageList.AddRange(bHeader); messageList.AddRange(newLine); messageList.AddRange(asen.GetBytes("from: ")); messageList.AddRange(bSender); messageList.AddRange(newLine); messageList.AddRange(asen.GetBytes("to: ")); messageList.AddRange(bRecipient); messageList.AddRange(newLine); messageList.AddRange(newLine); messageList.AddRange(bBody); messageList.AddRange(newLine); message = messageList.ToArray(); return message; }
public void sendMessage(int hostname, int port, addressBook myContacts) { try { Utilities myUtilities = new Utilities(); byte[] ba = enterMessage(hostname, myContacts); Console.WriteLine("Connecting....."); TcpClient tcpclnt = new TcpClient(myContacts.returnHostname(hostname), port); Stream stm = tcpclnt.GetStream(); Console.WriteLine("Transmitting....."); //encrypt the message byte[] cipherText = myUtilities.encrypt(ba, 20, "password"); //Console.WriteLine("Finished encryption"); //write cipherText to current stream stm.Write(cipherText, 0, cipherText.Length); Console.WriteLine("Messege send succesful"); tcpclnt.Close(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } }
static void Main(string[] args) { /* ** Server runs at startup. ** Client is invoked in a new thread when ** user requests to send a message and requests ** a connection with the domain or ip that the ** user specifies. */ addressBook myContacts = new addressBook(); menu(myContacts); }