Frage

Dieser Code ist ein Spinett aus meinem Socket-Listener-Programm.Das Problem ist, dass ich nicht die ganze Nachricht bekomme.Ich erhalte 1382 Bytes.Wie Sie im Code sehen können, habe ich jedoch die Arraygröße auf 15000 definiert. generasacodicetagpre.

danke für jegliche helps.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top