Reputation: 19
When sending Packets with Simple TCP the Packets are getting involved together
First I run this Code:
PacketSender.ValidateLogin();
PacketSender.GetProfileInfo();
PacketSender.GetFriendships();
PacketSender.GetFriendRequests();
This are the functions:
public static void ValidateLogin()
{
string Packet = string.Format("{0} {1} {2} {3}", "validateLogin", LocalData.ClientID, LocalData.Username, LocalData.Password);
Client.Send(Packet);
}
public static void GetProfileInfo()
{
string Packet = string.Format("{0} {1} {2} {3} {4}", "getProfileInfo", LocalData.ClientID, LocalData.Username, LocalData.Password, TempData.CurrentProfileOwnerUsername);
Client.Send(Packet);
}
public static void GetFriendships()
{
string Packet = string.Format("{0} {1} {2} {3} {4}", "getFriendships", LocalData.ClientID, LocalData.Username, LocalData.Password, TempData.CurrentProfileOwnerUsername);
Client.Send(Packet);
}
public static void SendFriendRequest()
{
string Packet = string.Format("{0} {1} {2} {3} {4}", "sendFriendRequest", LocalData.ClientID, LocalData.Username, LocalData.Password, TempData.CurrentProfileOwnerUsername);
Client.Send(Packet);
}
This is the 'Client' Class 'Send' function:
private SimpleTcpClient Client_;
public void Send(string Packet)
{
try
{
Task.Run(() => Client_DataSend(Packet));
}
catch
{
MessageBox.Show("Error while communicating with the server.", "Psychonuts");
Application.Exit();
}
}
void Client_DataSend(string Packet)
{
Client_.Connect("127.0.0.1", Convert.ToInt32(2181));
Client_.WriteLineAndGetReply(Packet, TimeSpan.FromSeconds(1));
Client_.Disconnect();
}
This is from the Server log showing the Packets are getting involved together:
Received: getFriendships pQxIr9d/HUyUHbzD/4JfIA admin admin admingetProfileInfo pQxIr9d/HUyUHbzD/4JfIA admin admin adminvalidateLogin pQxIr9d/HUyUHbzD/4JfIA admin admin
Sent: getFriendships accountIsntExist
Received: getProfileInfo pQxIr9d/HUyUHbzD/4JfIA admin admin adminvalidateLogin pQxIr9d/HUyUHbzD/4JfIA admin admin
Sent: getProfileInfo accountIsntExist
I want the packets to be separated for each packet I send What is going on and causing it and how do I fix it, appreciate any help.
Upvotes: 2
Views: 124
Reputation: 1062800
TCP is a stream protocol, not a packet protocol. Any packet-level semantics happen outside of the application layer (layer 7). What this means is that in any TCP protocol, you need to add your own "framing". For text based protocols this usually means a terminator sentinel, often simply some combination of CR/LF. For binary protocols, this usually means adding a length prefix in some predictable header. This applies in both directions.
Upvotes: 1