Java Socket Server
I have a Java process that is creating a listener on a TCP Socket using java.io.ServerSocket something like this (simplified):
ServerSocket server = new ServerSocket(4444,20);
server.accept();
The Java Process Fires off a Worker Thread when a Request Is Received and the Worker then sends a JSON string using java.io.PrintWriter and java.net.Socket:
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
out.println("JSON STRING");
out.flush();
out.close();
clientSocket.close();
I have simplified the Java code but this is essentially all it is doing.
.NET Socket Client
Next I have a .NET Application that communicates with the machine hosting this Java Process:
//Create Connection
Socket clientSocket = new Socket(AddressFamily.InterNetwork,
StreamType.Stream, ProtocolType.Tcp);
mySocket.Connect("192.168.1.102", 4444);
//Initialize Byte Buffer Larger than Expected JSON String and Read Bytes
byte[] receivedData = new byte[524288];
int numberOfBytes = clientSocket.Receive(receivedData, SocketFlags.None);
clientSocket.Close();
//Populate a new byte array of the exact size received with the received data
byte[] formatedBytes = new byte[numberOfBytes];
for (int i=0; i< numberOfBytes; i++)
{
formatedBytes[i] = receivedData[i];
}
//Convert Byte Array to String & Output Results
Response.ClearContent();
Response.ContentType("text/plain");
Response.Write(new System.Text.ASCIIEncoding().GetString(receivedData));
My Issue is that for whatever reason, this implementation does not work when I attempt to send slightly larger JSON Strings over the Socket Stream. With small datasizes (under 2KB) I have successfully tested this implementation with over 100 Clients connecting and receiving data without any issues, however trying to increase the JSON String size to about 256KB results in the .NET Application truncating the results. Increasing the size of the byte buffer array does not help either - it seems as if the .NET application is dropping the connection before all the data is transmitted, or the Java Application is not sending the entire String using the PrintWriter.
Any insight into this issue would be greatly appreciated - I'll post any updates if I make any progress myself.
Here is the Solution I came to - Server Is Working Great Now! Thanks Again!
byte[] receivedData = new byte[512000]; // 4 Meg Buffer
Socket mySocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
mySocket.Connect("172.26.190.205", 4444);
mySocket.ReceiveBufferSize = 8192;
int numberOfBytesRead = 0;
int totalNumberOfBytes = 0;
do
{
numberOfBytesRead = mySocket.Receive(receivedData,totalNumberOfBytes ,
mySocket.ReceiveBufferSize,SocketFlags.None);
totalNumberOfBytes += numberOfBytesRead;
}
while (numberOfBytesRead > 0);
mySocket.Close();
byte[] formatedBytes = new byte[totalNumberOfBytes ];
for (int i = 0; i < totalNumberOfBytes ; i++)
{
formatedBytes[i] = receivedData[i];
}
Response.ClearContent();
Response.ContentType = "text/plain";
Response.Write(new System.Text.ASCIIEncoding().GetString(formatedBytes));