开发者

SOCKET chat C# with private messaging

开发者 https://www.devze.com 2022-12-24 03:11 出处:网络
I want to create SOCKET chat(server + clients) with private messaging. When client write smth to stream, he should notify server that it

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 thi开发者_JAVA百科s 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


One really trivial way to fix your implementation is to prepend non-private messages with something else, say "NORM|". This way you can't fake private messages with normal ones.

Not really sure that PRIV| is enough though, a private message is sent to someone, so you'd have to include the recipient too right?

Anyway, the usual way to do this is to form "packets" from your commands. Your packet will contain the payload (the text to be sent) along with a header (the first few bytes that usually start with packet length, then command id -- normal msg, private msg, /who command, ping/pong, etc etc, and then some arbitrary data for the specific command you chose, like say.. a recipient name). You CAN make it into just one string if you want, by, for example, joining the fields with | characters like in your message, but this limits your field choices to non-| characters. A better way is to send them in binary format.

update: regarding your addition, I don't see what error you're talking about. The code seems fine to me. You split the line into tokens and check the command token first (priv/chat/whatever), then decide how to handle the rest of the tokens (the message in this case) depending on what the command was.

I need to see more code, what you are expecting to get and what you are getting to help you further.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号