Link to home
Start Free TrialLog in
Avatar of Kupi
Kupi

asked on

Sockets and messages problem

Hi, I'm learning C# socket programming and I found this tutorial:

http://www.theukwebdesigncompany.com/articles/asynchronous-socket-utility-classes.php

I modified the original code and now I have a server and a client application that send each other messages; messages are displayed in messageboxes, like a simple messenger.

All works great but there's one thing I don't understand: if I send more than one message while the messagebox is visible, the person I'm sending them will get a message in which all my latest messages are joined (I'd like them to be received as separate messages)... can someone explain me what I have to do to prevent this to happen?

Kupi
Avatar of _TAD_
_TAD_

I'm no expert, but since this has been open for the better part of a day with no response, I'll give you my 2 cents worth.

As I see it you have two choices:

1) Append the messages to each other in a list format (similar to AOL's AIM)
2) spawn a new message box/window with each incoming message (similar to ICQ)


Both have their pluses and minuses.  Just go with whatever you decide is best.  Or better yet, let the user decide which version he/she wants to use.


The easiest way to implement this is to always add a carriage return (\n) to the end of each and every transmission.  That way your new messages will at least be on different lines and not be crammed together.  

Further, if you look at many IM programs, they usually pre-pend the sender's ID/name to the message.  This also makes for an easy way to break messages.  To make your life simple, you might just want to prepend "\n"+<User'sName> to every outgoing message.  
try decreasing the buffer size from 1024 to 256 bytes.

Hope this helps
Jatinder
Avatar of Kupi

ASKER

Changing the buffer size didn't work...

I found that if I immediately print messages they don't merge... why??

So the only way to do what I need is to break messages manually? Can you provide a sample code?

Thanks!
Did u decrease the Send and recieve buffer both

For breaking messages
What  _TAD_  told is right

The easiest way to implement this is to always add a carriage return (\n) to the end of each and every transmission.  That way your new messages will at least be on different lines and not be crammed together.  

string[] myMsga = Message.Split('\n');

it will give u th array of message strings.


Jatinder
Avatar of Kupi

ASKER

What if my message already contains carriage returns?
u can use any special character for this purpose say #,@,~, or combination of two characters
Incase u r using combination of two characters then u wouldn't be able to use string.Split() method.

jatinder
Avatar of Kupi

ASKER

I thought messages could be sent as separate entities... is this impossible (even adding some code)?
ASKER CERTIFIED SOLUTION
Avatar of jatinderalagh
jatinderalagh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this

 if u r using beginSend metghod to send the packets
Then
You should not send new packet before others are sent.

If you call BeginSend multiple times, you can't do this.
You must send all packets with single send, if this isn't possible, you need
to wait before first send completes and calls callback method.


Jatinder