套接字:Java-Client到C#-Server,缺少一些数据

时间:2012-01-05 14:54:51

标签: c# java sockets

我在C#中编写了一个简单套接字服务器并且运行良好。我使用该连接将一些* .xml文件从一个设备(移动电话,PC)发送到另一个设备。我有3个不同的客户端,C#,Delphi和Java。服务器正在使用C#和Delphi,但是使用Java客户端它没有按预期工作。

java客户端的问题是,我只是从发送的xml文件中接收大约3/4的数据。我看了调试器,文档的每个字符都是由Java客户端发送的,但是没有收到。无论文档有多长并不重要,总会有一些缺失。我现在找了几个小时找到理由,但我不能。也许你们可以帮助我。

服务器:

public void WaitForClient(String filepath)
        {

int bytesRcvd;
            Byte[] rcvBuffer;
            string receivedMessage = "";

            try
            {
                {
                    // Generiere eine Client Verbindung
                    //client = listener.AcceptTcpClient();
                    client = listener.AcceptSocket();
                    //netStream = client.GetStream();
                    SetTexts(powercomConsole, "Handling client - ");
                    //Console.Write("Handling client - ");
                    int z = 0;
                    //int totalBytesEchoed = 0;
                    rcvBuffer = new byte[64];
                    //while ((bytesRcvd = netStream.Read(rcvBuffer, 0, rcvBuffer.Length)) > 0)
                    do
                    {
                        bytesRcvd = client.Receive(rcvBuffer);
                        for (int i = 0; i < rcvBuffer.Length; i++)
                        {
                            receivedMessage += (rcvBuffer[i] != 0) ? Convert.ToChar(rcvBuffer[i]).ToString() : "";
                            z++;
                            if (!receivedMessage.Equals(""))
                                Console.WriteLine("hans");
                            //receivedMessage += Convert.ToChar(rcvBuffer[i]);
                        }
                        rcvBuffer = new byte[64];
                    } while (bytesRcvd != 0);
                    WriteFile(filepath, receivedMessage);
                    SetTexts(powercomConsole, "Generated File written to " + filepath);
                    //Console.WriteLine("New File written to " + filepath);

                    // Schließe den Socket. Wir haben den Clienten erfolgreich abgewickelt
                    CloseServer();
                    SetTexts(powercomConsole, "Server closed");
                    //Console.WriteLine("Server closed");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                //netStream.Close();
            }
        } 

客户端:

public boolean sendByteBuffer(String profilename) {
    ByteBuffer buffer = fileToByteBuffer(profilename);
    for (int i = 0; i < fSize; i++)
    {
        char hans = (char) buffer.get();
        out.print(hans);
    }
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        fChan.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    try {
        fIn.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

private ByteBuffer fileToByteBuffer(String filepath) {
        ByteBuffer mBuf = null;

        try {
            fIn = new FileInputStream(filepath);
            fChan = fIn.getChannel();
            fSize = fChan.size();
            mBuf = ByteBuffer.allocate((int) fSize);
            fChan.read(mBuf);
            mBuf.rewind();
            // for (int i = 0; i < fSize; i++)
            // System.out.print((char) mBuf.get());
            // fChan.close();
            // fIn.close();
        } catch (IOException exc) {
            System.out.println(exc);
            System.exit(1);
        }

        return mBuf;
    }

这是我在fileToByteBuffer()方法

中使用的sendByteBuffer方法

谢谢你的推荐;)

0 个答案:

没有答案