Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi im working on a simple IM chat messenger and every thing works fine as long as i sends a "controlled" amount of lines to the server but what i want is to let the user be able from the chat window to send multiple lines. at the moment im using readlines because i cant get .read or .write to work on either the server or client side. i've also been looking on the web on how to use the "NetworkStream" but from the demos i've tried its very buggy and seems to mess up chars at random.

here is a simple code of the client that sends messages:
C#
private void SendMessage()
{
    string[] message = chatMessage.Text.Split('\n');
    chatText.Text += "\n" + userName + " Says:\n" + chatMessage.Text;
    chatMessage.Text = "";
    try
    {
        swSender = new StreamWriter(tcpServer.GetStream());
        swSender.WriteLine("message");
        swSender.WriteLine(userName);
        swSender.WriteLine(userReciever);
        foreach (string s in message)
        {
            swSender.WriteLine(s);
        }
        swSender.Flush();
    }

there is only some non importance catch after that.

simple code of servers reciever and sender:
        private void SendMessage(StreamReader srReceiver, StreamWriter swSender, TcpClient tcpClient, NetworkStream clientStream)
//every thing starts with a request thread that each connected user have and then it goes to either login, update status or as seen here SendMessage also the networkstream clientStream was a test to recieve a stream and use DataAvaible but I cant get that one to work probably either.
        {
            string userSender = srReceiver.ReadLine();
            string userReceiver = srReceiver.ReadLine();
            string message = "";
            List<string> completeMessage = new List<string>();
            int index = 0;
            //this here was a test to make it work but it wont and gets "stuck" then nothing more is readable. ignore this do while for now
            do
            {
                message = srReceiver.ReadLine();
                if (message != null)
                {
                    completeMessage.Add(message);
                }
                index++;
            }
            while (srReceiver.ReadLine() != null);
            //this is where i want to send the whole thing to the receiver defined from data from the client as "userReceiver"
            try
            {
                if (htUsers.Contains(userReceiver) == true)
                {
                    TcpClient tcpClientReceiver = new TcpClient();
                    tcpClientReceiver = (TcpClient)htUsers[userReceiver];
                    StreamWriter swSendertoReceiver = new StreamWriter(tcpClientReceiver.GetStream());
                    swSendertoReceiver.WriteLine("message");
                    swSendertoReceiver.WriteLine(userSender);
                    swSendertoReceiver.WriteLine(userReceiver);
                    foreach (string s in completeMessage)
                    {
                        message = s;
                        swSendertoReceiver.WriteLine(message);
                        this.add_commandwindow_text("\n[" + DateTime.Now + "] " + userSender + " to " + userReceiver + ": " + message);
                    }
                    swSendertoReceiver.Flush();
                }
            }


the biggest problem at this point is the server however i am looking for a way that could improve how the client sends messages but as i said i cant make write and read to work.

does any one have any idea how i could do this i a better and working way? i've also tried swWriter.Close() on client side but that makes errors on the server and the same happens the otherway around...

Ty - Jackie

EDIT: i've found something that migth work but seems like a very bad way to do it by adding a "end-of-stream string" witch i think is "duck-tape" code, im sure there is a smarter way ^^
Posted
Updated 25-Oct-11 5:36am
v3

1 solution

NetworkStreams don't guarantee that one send = one receive. So you either need to add a delimiter which marks the end of a message, and buffer results until you reach it (ReadLine is doing this for you automatically), or you need to send a message header which includes the message length before you send the content, and buffer until you have received the amount you were expecting.

Have a look at ClientInfo.Read (for delimiter-separated) and ReadInternal (for header+content) in my Sockets library code here[^].
 
Share this answer
 
Comments
Jackie00100 25-Oct-11 11:48am    
is it just me or are those for asp.net? im currently doing it winform based ptherwise it seems nice you're link ^^
BobJanova 25-Oct-11 12:05pm    
They have no UI representation, so they're not 'for' either, they're for general use within .Net. I've used them predominantly in console or WinForms apps, myself.
Jackie00100 25-Oct-11 13:15pm    
okay i see im trying to look more into NetworkStreams they seems to be able to do what i want but i cant make it work still sadly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900