2012-05-16 125 views
0

我有一個客戶端/服務器應用程序,其中服務器在Java中,客戶端在Vb.net中。通過TCP發送大字符串時丟失數據。 (客戶端/服務器)

當我從客戶端發送大字符串到服務器沒有收到完整的文本。 請幫忙。

代碼如下。

client-- VB.net-

 Try 
     Dim clientSocket As New System.Net.Sockets.TcpClient() 

     ' msg("Client Started") 
     clientSocket.Connect(StrIP_Add, intPort) 
     clientSocket.SendBufferSize=104857600 
     '6511 6522 
     ' Label1.Text = "Client Socket Program - Server Connected ..." 

     Dim serverStream As NetworkStream = clientSocket.GetStream() 
     Dim outStream(104857600) As Byte 

     ' MsgBox(strValidator.Trim.Length) 

     outStream = System.Text.Encoding.ASCII.GetBytes(strValidator.Trim) 
     ' Dim outStream As Byte() = "sdsfd" 
     System.Threading.Thread.Sleep(2000) 
     serverStream.Write(outStream, 0, outStream.Length) 
     System.Threading.Thread.Sleep(2000) 
     serverStream.Flush() 

     Dim inStream(104857600) As Byte 
     serverStream.Read(inStream, 0, outStream.Length) '104857600) ' CInt(clientSocket.ReceiveBufferSize)) 
     Dim returndata As String = _ 
     System.Text.Encoding.ASCII.GetString(inStream) 
     ' msg("Data from Server : " + returndata) 
     clientSocket.Close() 

    Catch ex As Exception 
     ' VikUcMsg.AddMessage("<b><u>" & Page.Title & "</u></b><br><br>" & "No Connectivity on the port :" & intPort, enmMessageType.Error) 


    End Try 

server--的Java

BufferedInputStream RecievedBuffer = new BufferedInputStream(
       TCPIP_Client_SOCKET.getInputStream()); 
     InputStreamReader RecievedInputStreamReader = new InputStreamReader(
       RecievedBuffer); 

     System.out.println(RecievedBuffer.toString().length()); 
     //char[] RecievedChars = new char[TCPIP_Client_SOCKET 
       //.getReceiveBufferSize()]; 

     char[] RecievedChars = new char[100000]; 
     //Thread.sleep(5000); 
     RecievedInputStreamReader.read(RecievedChars); 
     //Thread.sleep(5000); 
     String strRecievedData=null; 
     //Thread.sleep(5000); 
     strRecievedData = new String(RecievedChars).trim(); 
     //strRecievedData = RecievedChars.; 
     Thread.sleep(5000); 
     if (strRecievedData!=null) 
     { 
      System.out.println(strRecievedData); 
         } 

strRecievedData只有havig 8192所有的時間。

回答

1

簡而言之,你必須在從套接字讀取時循環,因爲不能保證每次嘗試讀取時都會收到多少字節。

僞代碼:

while (!msgCompleted && !overallTimeout) 
{ 
    bytesRead = netstream.Read(readBuffer); 

    if (bytesRead > 0) 
    { 
     // here append readBuffer to msgBuffer from offset to offset+bytesRead 

     offset += bytesRead // update offset so you can keep appending 

    // inspect the msgBuffer to see if the message is completed 
    } 

} 

這一切都這樣說,你得在你的代碼nyumerous其他問題。例如...

您分配104857601(未104857600)字節的緩衝區位置:

Dim outStream(104857600) As Byte

,然後丟棄並更換緩衝與任何內容也會從strValidator reurned:

outStream = System.Text.Encoding.ASCII.GetBytes(strValidator.Trim)

預先分配它只是爲了替換它。

另一條...

您分配一定長度的輸入緩衝器:

Dim inStream(104857600) As Byte

但隨後讀入使用不同的緩衝區的長度緩衝器:

serverStream.Read(inStream, 0, outStream.Length)

根據長度,這很容易出錯。

您還需要在這個VB中讀取循環,就像Java讀取一樣。

+0

現在工作正常..謝謝.. .. :) – Kailasnadh

相關問題