Pergunta

Este código é um clavecín do meu socket de escuta do programa.O problema é que eu não estou recebendo a mensagem inteira.Eu estou recebendo 1382 bytes.No entanto, como você pode ver no código, eu tenho definido o tamanho da matriz para 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 
                { 

Obrigado por qualquer ajuda caras.

Foi útil?

Solução

Soquete.Receber() só irá obter o primeiro datagrama de cada chamada.Verifique se há mais de 1382 bytes enviados pelo lado do cliente na primeira chamada.

Se não há mais dados a ser enviado, em seguida, ter a fila de cliente para Enviar uma chamada, ou continuamente chamada a Receber() e acrescentar para outro buffer até que você sabe que ela seja concluída.

Editado por exemplo:O que você está procurando não é o bloqueio de IO.Uma forma de implementar isso, é revestido de fora aqui.Se você tem uma classe por cliente de conexão, ele pode se parecer com isto:

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 
    }
}

Eu não verificar isso, mas deve ajudá-lo na direção certa.Eu achava que quando o cliente é feito o envio de dados e, em seguida, o servidor deve parar de ler.Eu também assumido que o do { } foi o início de um loop infinito que esperou para novas conexões.Você também pode fazer uso de BeginAccept() para fazer essa parte do código não-bloqueio, mas que depende do seu caso de uso, se necessário.

Note que cada conexão aberta desta forma, resultará em um novo thread do conjunto de threads.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top