Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the reason i want to do this is simply because the string will be for application control and bytes for files. what i want to know is can i send a file and send a message at the same time. how do i tell which byte is for what... i read somewhere that i could try doing this:

"prefix each chunk of data you sent with an identifier. e.g. maybe the first byte would be 0x00 = login messge, 0x01 = logout messages, 0x02 = file data"

if that's a viable solution, how do you implement that?

Thank you:) PS sorry for my bad english
Posted

1 solution

You need to use a synchronization byte, and channels (a task identifier).

I've written a TCP/IP library that will do exactly what you need. Have a look:

Reusable multithreaded TCP client and server classes with example project in VB.NET

- Pete
 
Share this answer
 
Comments
dookie009 4-Sep-13 2:35am    
@pete thank you so much for the reply, i have looked at your code and is somewhat hard to follow, but other functions i could easily grasp(cpu monitoring and sending data...) thank you so much, you saved my project. can you kindly tell me how does one implement "channels (task identifiers)? im really interested in that.
pdoxtader 4-Sep-13 8:45am    
I'm glad it worked out for you.

A channel, or a task identifier is a is a TCP communication concept not implemented by Microsoft. If you want to roll your own TcpListener / TcpClient wrappers, you'll have to implement this yourself. This is how it works:

TcpListener and TcpClient - out of the box, don't do communication synchronization. They'll let you send bytes at the same time, but they'll stutter and stall while it's happening, drastically reducing performance. When I started building these classes, I knew I'd have to deal with that.

I accomplished it by sending a synchronization byte back and fourth between the client and the server while they are connected. Whoever has the synchronization byte can send bytes, and whoever doesn't only listens and receives bytes. To me, it was kind of like hockey players passing a puck back and fourth... so I nicknamed the synchronization byte the "puck".

If you implement a system like this, you have the option of making each side's turn with the puck very limited... lets say that whoever has the puck can only send a maximum of 5k bytes with it. That means that the puck is changing hands very quickly, and to the end user you can make it look like you are sending and receiving bytes at the same time when actually you are just carefully synchronizing who's talking and who's listening.

Lastly, I had to build some kind of logical packet header, or when the bytes arrived on the other end, the receiving machine wouldn't know when to stop listening... wouldn't be able to tell what was the puck, and what was user data. My packet headers are made up of three bytes: a channel identifier byte, and two packet size bytes. The two packet size bytes are parts of a short - together, cast into a short, they hold the total number of user bytes sent along in this packet. The channel byte is a number that represents a logical communications channel. You send your bytes along with this channel number, and when they arrive on the other end, they arrive with this channel number. So you can have 1 - 255 different things being send or received AT ONCE, bandwidth allowing. You just need to handle the logic involved with processing bytes as they arrive with that channel id.

So, potentially, you can have 254 different things being sent or received at the same time over a single port with these classes.

If you want to roll your own, well then that's the concept... but why reinvent the wheel? I spent weeks working on this, testing it, using it in projects I was / am working on professionally... it's been on Codeproject.com for more then a year, and since then people have found and offered fixes for bugs... ran into problems and offered help to each other... it has even had an android client written for the server class, and although I haven't seen it, I'm told that it's in use all over the country.

Using it is simple and straight forward, and if you have a question you should look through the conversation on it's Codeproject page... if you don't find the answer there, just ask me and I'll help you out.

- Pete
dookie009 4-Sep-13 9:17am    
you sir.... you are a champ... im lost for words. the force is too strong with you... thank you so much. i highly appreciate your kindness and taking time to assist me and the code project community. we need people like you in this industry.you have saved us lots of hours. thank you sir
pdoxtader 4-Sep-13 9:24am    
Lol... Your welcome.

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