Reputation: 5
I have a Client/ server application where the Server is in java and Client is in Vb.net.
When i send large string from client to server am not receiving complete text. please help.
code attached below.
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 is only havig 8192 all the time.
Upvotes: 0
Views: 1689
Reputation: 10855
Well the short answer is that you must loop when reading from a socket because there is no guarantee how many bytes you will receive on each attempt to read.
Psuedo-code:
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
}
}
That all being said, you've got nyumerous other problems in your code. For example...
You allocate a 104857601 (not 104857600) byte buffer here:
Dim outStream(104857600) As Byte
And then discard and replace that buffer with whatever contents get reurned from strValidator:
outStream = System.Text.Encoding.ASCII.GetBytes(strValidator.Trim)
No point in pre-allocating it just to replace it.
Another one...
You allocate an input buffer of a certain length:
Dim inStream(104857600) As Byte
But then read into that buffer using the length of a different buffer:
serverStream.Read(inStream, 0, outStream.Length)
This is prone to errors depending on the lengths.
You will also need to loop in this VB read just as for the Java read.
Upvotes: 1