Solved

Best way to use Sockets in C#

Posted on 2012-03-19
16
717 Views
Last Modified: 2013-12-03
I have seen a lot of ways to deal with sockets in VB.NET, however I am not sure which way is the best way that allow:
1. Sending string data (both sides)
2. Sending binary data (both sides, including large binary data and files)

Important: I DONT want to know how to use Sockets, I already know that.

I want the best way to use to achieve what I said above.

Basically I want the following methods to be included:
sendData();
sendFile();

I want to be using TcpListener/TcpClient.

The problem I usually face is when I have 1025 bytes buffer and I receive for example two binary buffers, one use the full 1025 and the other use for example 400 bytes from 1024, so if i wrote that to a file it will write about 2050 which is invalid file size, the file size is only 1425 bytes.
0
Comment
Question by:Mohamed Abowarda
  • 5
  • 3
  • 2
  • +2
16 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
make your methods look like this on receiving end:-
public void ReceiveData(byte[] arrBytes);
public void ReceiveFile(FileStream stream);

Open in new window

0
 
LVL 12

Author Comment

by:Mohamed Abowarda
Comment Utility
@BuggyCoder: Can you give an example or sample code? Usually I receive the data in bytes and converting bytes to FileStream, I think it will keep unnecessary data at the end.

Another thing is sending Binary data, could be 10 MB of binary data but its NOT a file.

Thanks,
0
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
even in case of file as well, just use the first method.
Just make sure that on the other end send your file as stream of bytes....

here is a link to convert filestream to byte array:-
http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
>The problem I usually face is when I have 1025 bytes buffer and I receive for example two binary buffers, one use the full 1025 and the other use for example 400 bytes from 1024, so if i wrote that to a file it will write about 2050 which is invalid file size, the file size is only 1425 bytes.


Have you tried using the stream.length property to determine the size of data instead of using 1024 bytes?


http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx
0
 
LVL 12

Author Comment

by:Mohamed Abowarda
Comment Utility
@CodeCruiser: I am not sure where to use stream.

I want someone to modify the following code to solve the problem (please see the comments in the code):

// Public Declaration
bytes[] buff = new bytes[1024];

// This line is used after connection
tcpClient.Client.BeginReceive(buff, 0, buff.Length, SocketFlags.None, DataReceived, null);

void DataReceived()
{
      // Here you can process the buffer you have received...
      // EXAMPLE: string str = Encoding.ASCII.GetString(buff);
      // The problem I usually get here because buff contains 1025 bytes of data EVEN if the server sent only 50 bytes.

      // Receive more data
      tcpClient.Client.BeginReceive(buff, 0, buff.Length, SocketFlags.None, DataReceived, null);
}

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 300 total points
Comment Utility
The EndReceive method returns an integer which is the number of bytes received so you can use that to determine how many bytes out of 1024 to write to the file.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 12

Author Comment

by:Mohamed Abowarda
Comment Utility
@CodeCruiser: Can you please update the sample code that I posted earlier.

Thanks,
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Where is your endreceive code?
0
 
LVL 12

Author Comment

by:Mohamed Abowarda
Comment Utility
I am not using endreceive code yet, the code I am using is similar to the one I posted earlier:
// Public Declaration
bytes[] buff = new bytes[1024];

// This line is used after connection
tcpClient.Client.BeginReceive(buff, 0, buff.Length, SocketFlags.None, DataReceived, null);

void DataReceived()
{
      // Here you can process the buffer you have received...
      // EXAMPLE: string str = Encoding.ASCII.GetString(buff);
      // The problem I usually get here because buff contains 1025 bytes of data EVEN if the server sent only 50 bytes.

      // Receive more data
      tcpClient.Client.BeginReceive(buff, 0, buff.Length, SocketFlags.None, DataReceived, null);
}

Open in new window


I only need to update that code to get the result I asked for.

Thanks,
0
 
LVL 16

Assisted Solution

by:hjgode
hjgode earned 200 total points
Comment Utility
Hello

you have use EndReceive and the integer that returns the number of bytes received or you will nit know, how many bytes have been read in reality.

Using buffers to read an unknown number bytes is done normally in chunks of a given size and using the number of bytes read returned by a read. So, you have a buffer of 1024 bytes and start reading. Every read has to be checked for the number of bytes read. Possibly, the number of bytes read is only 500. Then you have to stop reading and use only the first 500 bytes of your buffer. If the number of bytes read is equal the buffers ize, you have to do additional reads until the number of read bytes is less then your buffer.

~josef
0
 
LVL 32

Expert Comment

by:sarabande
Comment Utility
for variable sized data via sockets you normally would pass a fixed-sized "header" prior to the data. the header contains (at least) the length of the data in bytes as first information. often you also would add sender information or some type of message and put them all to a  structure. the structure must have same alignment and size on all platforms (simply use 32-bit integer type for all members). then your receive function firstly would read until the full header was read. if sender and receiver could be on different endian platforms the sender would convert all integer members to "network order" what is big-endian. there are platform-specific functions htons (for 16-bit integer) and htonl (32-bit) which do that job. after you received the full header you would reconvert the integer length by calling ntohl. after you know the length of the data stream you could allocate an appropriate buffer and read in a loop until error or until all data was received.

Sara
0
 
LVL 16

Expert Comment

by:hjgode
Comment Utility
Hi

I aggree to the way sarabande describes a bniary socket transfer. It is also a good idea, to describe the length of the data first and possibly to add some CRC or similar error checking.

That leads to your first question, if it is better to use strings or bytes to transfer data via sockets. If you go with strings, you may have to control the way unicode or UTF8 strings are converted to/from byte arrays, as finally, sockets only transfer bytes. You have also to consider, how the end of a string is marked (normally with one or two (unicode) null byte(s)). Or you add some marker to the end like "<EOT>".

But finally, you end up with reading bytes in chunks until all bytes are read. You have to re-assemble the bytes back to a large array or a string.

~josef
0
 
LVL 12

Author Closing Comment

by:Mohamed Abowarda
Comment Utility
I have been busy with other projects in other programming languages, so I didn't get a chance to even try the solution here, using EndReceive() works perfectly!

Thanks,
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now