SOCKET 聊天 C# 与私人消息传递

发布于 2024-08-26 23:26:41 字数 1245 浏览 3 评论 0原文

我想用私人消息创建 SOCKET 聊天(服务器+客户端)。 当客户端将数据写入流时,他应该通知服务器它 是用户 X 的私人消息,我该怎么做?

实际上我可以这样做:

 string command = "PRIV|" + txtMessage.Text;
 swSender.WriteLine(command);

但我认为这不好,例如如果用户想要发送消息 就像我们的“PRIV|”标记这将是错误

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();
}
}

更新:好的,另一个问题是我如何识别它是私人消息,例如,如果我使用此方法:

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
}

如果用户想要发送像我们的“PRIV|”这样的消息标记这将是错误

更新:在这种情况下,如果客户端想要发送消息“测试消息|其他测试” logwriter.WriteLine(tokens[1]) - 仅显示标记[1] - “测试消息”,没有“其他测试”消息

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜尕妞 2024-09-02 23:26:41

修复实现的一种非常简单的方法是在非私有消息前面添加其他内容,例如 "NORM|"。这样你就不能用普通的私人消息来伪造私人消息。

但不太确定 PRIV| 是否足够,私人消息已发送给某人,因此您也必须包括收件人,对吧?

无论如何,通常的方法是根据命令形成“数据包”。您的数据包将包含有效负载(要发送的文本)以及标头(通常以数据包长度开头的前几个字节,然后是命令 ID - 正常消息、私有消息、/who 命令、ping/pong 等) ,然后是您选择的特定命令的一些任意数据,例如……收件人姓名)。如果您愿意,您可以将其变成一个字符串,例如,通过像消息中那样使用 | 字符连接字段,但这会将您的字段选择限制为非 |字符。更好的方法是以二进制格式发送它们。

更新:关于您的添加,我不明白您在说什么错误。该代码对我来说似乎很好。您将该行分成多个令牌,并首先检查命令令牌(priv/chat/whatever),然后根据命令决定如何处理其余令牌(本例中的消息)。

我需要查看更多代码,您期望得到什么以及您将得到什么来进一步帮助您。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文