I want to create SOCKET chat(server + clients) with private messaging. When client write smth to stream, he should notify server that it is private message for user X, how can i do this?
Actually i can do smth like this:
string command = "PRIV|" + txtMessage.Text;
swSender.WriteLine(command);
but i think it isn't good, for example if user wants send message like our "PRIV|" flag it will be errors
public class TestChat
{
private StreamWriter swSender;
private IPAddress ipAddr;
private void InitializeandSend()
{
//ip from text box
ipAddr = IPAddress.Parse(txtIp.Text);
// Start a new TCP connections to the chat server
tcpServer = new TcpClient();
tcpServer.Connect(ipAddr, 1986);
//...
//sending message from text box
swSender.WriteLine(txtMessage.Text);
swSender.Flush();
}
}
update :ok another question how i can identife that it is private message, for example if i use this method:
private NetworkStream ns;
Byte[] buffer = new Byte[2048];
ns.Read(buffer,0,buffer.Length);
string chatter = System.Text.Encoding.ASCII.GetString(buffer);
string[] tokens = chatter.Split(new Char[]{'|'});
if (tokens[0] == "CHAT")
{
logwriter.WriteLine(tokens[1]); //writing
}
if user wants send message like our "PRIV|" flag it will be errors
update :in this case if client want to send message "Test message|other test"
logwriter.WriteLine(tokens[1])
- shows only tokens[1] - "Test message" without "other test" message