This code is a spinet from my socket listener program. The problem is I am not getting the whole message. I am receiving 1382 bytes. However, as you can see in the code, I have defined the array size to 15000.
namespace Listener
{
class Server
{
static void Main(string[] args)
{
IPAddress localAddr = IPAddress.Parse(args[0]);
System.Console.WriteLine("The local IP is {0}",
localAddr);
Int32 port = int.Parse(args[1]);
System.Console.WriteLine("The port is {0}", port);
TcpListener myListener = new TcpListener(localAddr,
port);
byte[] bytes = new byte[15000];
string sem = "";
do
{
Console.Write("Waiting");
myListener.Start();
Socket mySocket = myListener.AcceptSocket();
// receiving the hl7 message
mySocket.Receive(bytes);
string receiveMessage =
Encoding.ASCII.GetString(bytes);
// write out the hl7 message to a receiving
folder
DateTime currentDate = DateTime.Now;
long eTicks = currentDate.Ticks;
System.IO.File.WriteAllText(@"y:\results\" +
eTicks + ".hl7", receiveMessage);
// build the acknowledgemnent message to send
back to the client
try
{
Thanks for any help guys.
Socket.Receive() will only obtain the first datagram each call. Verify there is more than 1382 bytes sent by the client side in the first call.
If there is more data to be sent then either have the client queue it up for one Send call, or continuously call Receive() and append onto another buffer until you know it’s completed.
Edited for example:
What you’re looking for is non-blocking IO. One way to implement it is lined out here. If you have a class per client-connection, it might look like this:
I didn’t verify this, but it should get you in the right direction. I assumed that when the client is done sending data, then the server should stop reading it. I also assumed that your do { } was the start of an infinite loop that waited for new connections. You can also make use of BeginAccept() to make that portion of the code non-blocking, but that depends on your use case if you need to.
Note that every connection opened this way will result in a new ThreadPool thread.