private void Receive() { bool already_check = true; while (connected) { try { Byte[] buffer = new Byte[128]; clientSocket.Receive(buffer); string incomingMessage = Encoding.Default.GetString(buffer); incomingMessage = incomingMessage.Substring(0, incomingMessage.IndexOf("\0")); //logs.AppendText("Message received" + "\n"); //logs.AppendText("Server: " + incomingMessage + "\n"); if (incomingMessage == "EfeYarenYigitKayaYou successfully logged inEfeYarenYigitKaya") { textBox_message.Enabled = true; //after successfully connection, textbox button should be active. send_button.Enabled = true; //after connection, send button should be active. logs.AppendText("Connected to the server!\n"); disconnect_button.Enabled = true; //after connected disconnect button should be active. userName = incomingName; connect_button.Enabled = false; //already connected so connect button shoul be inactive AcceptBut.Enabled = true; RejectBut.Enabled = true; friendsBut.Enabled = true; sendReqBut.Enabled = true; button1.Enabled = true; deleteButton.Enabled = true; } else if (incomingMessage == "EfeYarenYigitKayaThis username does not exist in databaseEfeYarenYigitKaya") //server sends does not exist message { logs.AppendText("You are not in the database\n"); //if this message came from the server and username was wrong, print this } else if (incomingMessage == "EfeYarenYigitKayaYou are already logged inEfeYarenYigitKaya") { logs.AppendText("You already logged in\n"); //correct username but it already connected already_check = false; //for blocked to another connection from same user } else if (incomingMessage.Length > 18 && incomingMessage.Substring(0, 18) == "EfeYarenYigitKaya£") { string inviter = incomingMessage.Substring(18, incomingMessage.Length - 18); if (!requestList.Contains(inviter)) { requestList.Add(inviter); // yeni ekledim whenever you receive request that does not exist in your request list add it to your list } friendReqList.Clear(); //requestList.Add(inviter); //bool flag = true; string currentRequest = ""; foreach (string name in requestList) { currentRequest += name + "\n"; } friendReqList.AppendText(currentRequest); // if(name == inviter) // { // flag = false; // } //} //if (flag) //{ // requestList.Add(inviter); // currentRequest += inviter; //} //friendReqList.AppendText(currentRequest + "\n"); //friendReqList.AppendText(inviter + "\n"); } else if (incomingMessage.Length > 18 && incomingMessage.Substring(0, 19) == "EfeYarenYigitKaya*£") { string myMessage = incomingMessage.Substring(19, incomingMessage.Length - 19); logs.AppendText(myMessage + "\n"); } else if (incomingMessage.Length > 18 && incomingMessage.Substring(0, 19) == "EfeYarenYigitKaya*!") { int index = incomingMessage.IndexOf("%"); string sender = incomingMessage.Substring(19, index - 19); string myMessage = incomingMessage.Substring(index + 1); logs.AppendText(sender + ": " + myMessage + "\n"); } else if (incomingMessage.Length > 18 && incomingMessage.Substring(0, 20) == "EfeYarenYigitKaya*<f") { string deletedfriend = incomingMessage.Substring(20); logs.AppendText("There is no such friend as " + deletedfriend + " in your friends list\n"); } else if (incomingMessage.Length > 18 && incomingMessage.Substring(0, 20) == "EfeYarenYigitKaya*<s") { string deletedfriend = incomingMessage.Substring(20); logs.AppendText("You have succesfully remove " + deletedfriend + " from your friends list\n"); } else if (incomingMessage.Length > 18 && incomingMessage.Substring(0, 19) == "EfeYarenYigitKaya*<") { string clientName = incomingMessage.Substring(19); logs.AppendText(clientName + " removed you from friends\n"); } else if (incomingMessage.Length > 18 && incomingMessage.Substring(0, 18) == "EfeYarenYigitKaya#") // once accept friend request { string invitee = incomingMessage.Substring(18, incomingMessage.Length - 18); //friendReqList.AppendText(invitee + "\n"); logs.AppendText(invitee + " Accepted your friend request\n"); } else if (incomingMessage.Length > 18 && incomingMessage.Substring(0, 18) == "EfeYarenYigitKaya&") { string invitee = incomingMessage.Substring(18, incomingMessage.Length - 18); friendReqList.AppendText(invitee + "\n"); } else if (incomingMessage.Length > 18 && incomingMessage.Substring(0, 19) == "EfeYarenYigitKaya*&") { string invitee = incomingMessage.Substring(19, incomingMessage.Length - 19); logs.AppendText(invitee + "\n"); } else if (incomingMessage.Length > 17 && incomingMessage.Substring(0, 18) == "EfeYarenYigitKaya$") // receive friends from server { //FriendList.AppendText("print friend list"); FriendList.Clear(); string friends = incomingMessage.Substring(18, incomingMessage.Length - 18); friends = friends.Replace(',', '\n'); FriendList.AppendText(friends); //if(friends.Length > 1) //FriendList.AppendText("********\n"); } else if (incomingMessage.Substring(0, 3) == "123") { logs.AppendText(incomingMessage.Substring(3)); // you have already requested and wait for answer } else if (incomingMessage.Substring(0, 3) == "321") { logs.AppendText(incomingMessage.Substring(3)); // you requested but there is no such user in database } else { incomingMessage = incomingMessage.Substring(17, incomingMessage.Length - 34); //true connection and message logs.AppendText(incomingMessage + "\n"); //print the incoming message } } catch (Exception e) { //FriendList.AppendText(e.Message); if (!terminating) { if (already_check) { logs.AppendText("The server has disconnected\n"); //disconnect button activated } server_check = false; already_check = true; // user can enter again connect_button.Enabled = true; //user can connect again because disconnect now textBox_message.Enabled = false; //user cannot write text send_button.Enabled = false; //user cannot send message } clientSocket.Close(); connected = false; } } }