Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7564695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:57:48+00:00 2026-05-30T13:57:48+00:00

This code is a spinet from my socket listener program. The problem is I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:57:49+00:00Added an answer on May 30, 2026 at 1:57 pm

    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:

    internal class Server
    {
        private 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();
                var clientConn = new ClientConnection(mySocket);
            } while (true);
        }
    }
    
    public class ClientConnection
    {
        private const int BUFFER_SIZE = 15000;
        readonly private byte[] _buffer = new byte[BUFFER_SIZE];
        readonly private Socket _socket;
        readonly private StringBuilder _output = new StringBuilder();
    
        public ClientConnection(Socket socket)
        {
            _socket = socket;
            _socket.BeginReceive(_buffer, 0, BUFFER_SIZE, SocketFlags.None, ReadCallback, null);
        }
    
        private void ReadCallback(IAsyncResult ar)
        {
            var read = _socket.EndReceive(ar);
    
            if (read > 0)
            {
                // receiving the hl7 message             
                string receiveMessage = Encoding.ASCII.GetString(_buffer, 0, read);
                _output.Append(receiveMessage);
    
                _socket.BeginReceive(_buffer, 0, BUFFER_SIZE, SocketFlags.None, ReadCallback, null);
            }
            else
            {
    
                // 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", _output.ToString());
    
                SendAcknowledgement();
            }
        }
    
        private void SendAcknowledgement()
        {
            // build the acknowledgemnent message to send back to the client 
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is from Prototype.js . I've looked at probably 20 different tutorials, and
This code does not seem to compile, I just need to write something to
This code is reading from mysql db tables and should return few names. But
This code throws parse error, which I do not understand why. function t(){ return
This code can be used to select the first ten records from a table
This code works: $(contentDiv).fadeIn(slow); This does not: var elementName = 'contentDiv'; $(elementName).fadeIn(slow); No fade
This code should works, but in fact the span is not displayed : <a
This code below causes infinate loop problem (as documented). So how do I set
This code give me this error: c.apply is not a function All code works
This code does not compile. public class Diamond { public static void diamondOfAsterisks(String *

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.