/// <summary> /// Connects to the server and then logs you in /// </summary> /// <param name="un">The user's username</param> /// <param name="password">The user's password</param> /// <returns>True if the login is successful, false otherwise</returns> /// <example><code>if (!ClientNetwork.logInToServer("Username", "Password")) /// MessageBox.Show("Log in failed");</code></example> public static bool logInToServer(string un, string password) { // Close any existing connections Disconnect(false); Network.Header head; string randomCodeData = ""; try // Connect to the server { TcpClient client = new TcpClient(IPAddress, Port); connectionStream = client.GetStream(); connected = true; byte[] header = new byte[Network.HEADER_SIZE]; for (int i = 0; i < Network.HEADER_SIZE; i++) { while (connectionStream.DataAvailable == false) {} // ToDo: Add escape code header[i] = (byte)connectionStream.ReadByte(); } head = Network.bytesToHeader(header); byte[] randomCode = new byte[head.Length]; // Download the random pass code for security for (int i = 0; i < head.Length; i++) { while (connectionStream.DataAvailable == false) {} // ToDo: Add escape code randomCode[i] = (byte)connectionStream.ReadByte(); } randomCodeData = System.Text.UnicodeEncoding.Unicode.GetString(randomCode, 0, (int)head.Length); } catch { //System.Windows.Forms.MessageBox.Show("Could not connect to host"); connected = false; return false; } if (head.DataType != Network.DataTypes.RandomPassCode) // Wrong type for first packet { //System.Windows.Forms.MessageBox.Show("Illegal data received from host"); connected = false; return false; } // Create secure password password = SDCSCommon.CryptoFunctions.getMD5Hash(String.Concat(SDCSCommon.CryptoFunctions.getMD5Hash(password), randomCodeData)); Network.Header sendHead = new Network.Header(); sendHead.DataType = Network.DataTypes.LoginInformation; sendHead.FromID = 0; sendHead.ToID = -1; byte[] passBytes = System.Text.UnicodeEncoding.Unicode.GetBytes(password); byte[] usernameBytes = System.Text.UnicodeEncoding.Unicode.GetBytes(un); sendHead.Length = passBytes.Length + usernameBytes.Length + 4; SendData(SDCSCommon.Network.headerToBytes(sendHead), BitConverter.GetBytes(usernameBytes.Length), usernameBytes, passBytes); byte[] statusHeader = new byte[Network.HEADER_SIZE]; for (int i = 0; i < Network.HEADER_SIZE; i++) { while (connectionStream.DataAvailable == false) {} // ToDo: Add escape code statusHeader[i] = (byte)connectionStream.ReadByte(); } Network.Header statusHead = Network.bytesToHeader(statusHeader); byte[] statusCode = new byte[statusHead.Length]; // Download the random pass code for security for (int i = 0; i < statusHead.Length; i++) { while (connectionStream.DataAvailable == false) {} // ToDo: Add escape code statusCode[i] = (byte)connectionStream.ReadByte(); } if (!(statusHead.DataType == Network.DataTypes.LoginStatus && BitConverter.ToInt32(statusCode, 0) == Network.LoginOK)) { connected = false; try { connectionStream.Close(); } catch {} return false; } username = un; listeningThread = new Thread(new ThreadStart(listeningFunc)); listeningThread.Start(); return true; }
/// <summary> /// Sends an IM to the user specified by the toID with the message passed to this function. /// </summary> /// <param name="toID">The userID of the user this message is to be sent to</param> /// <param name="message">The message to send</param> /// <returns>True if the sending was successful (doesn't mean the user received it, only that it was sent to the server), false otherwise.</returns> /// <example><code>public void btnSendIM_clicked(object o, EventArgs e) /// { /// // This assumes that a function for looking up a user ID has been written and that txtUsername and txtMessage are text boxes. /// if (!SendIM(lookupUserID(txtUsername.Text), txtMessage.Text)) /// MessageBox.Show("Connection to server lost"); /// }</code></example> public static bool SendIM(int toID, string message) { Network.Header head = new Network.Header(); // FromID is set at the server head.FromID = 0; head.ToID = toID; head.DataType = Network.DataTypes.InstantMessage; byte[] data = System.Text.UnicodeEncoding.Unicode.GetBytes(message); head.Length = data.Length; return SendData(Network.headerToBytes(head), data); }
private void sendBuddyListData() { // This code effectively creates a semaphore lock (BuddyListData.SyncRoot) { Network.Header buddyHeader = new Network.Header(); buddyHeader.DataType = Network.DataTypes.BuddyListUpdate; buddyHeader.FromID = -1; buddyHeader.ToID = conn.userID; byte[] buddyBytes = Network.BuddyListDataToBytes((Network.BuddyListData[])BuddyListData.ToArray(typeof(Network.BuddyListData))); buddyHeader.Length = buddyBytes.Length; sendData(Network.headerToBytes(buddyHeader), buddyBytes); BuddyListData.Clear(); } }