Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 699807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:25:38+00:00 2026-05-14T03:25:38+00:00

I want to create SOCKET chat(server + clients) with private messaging. When client write

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:25:38+00:00Added an answer on May 14, 2026 at 3:25 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer in a word, yes. the only problem you might run… May 14, 2026 at 10:30 pm
  • Editorial Team
    Editorial Team added an answer I found that *.LIB file uses the ar file format.… May 14, 2026 at 10:30 pm
  • Editorial Team
    Editorial Team added an answer Try <add key="mykey" value="A,B,C"/> And string[] mykey = ConfigurationManager.AppSettings["mykey"].Split(','); May 14, 2026 at 10:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.