Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3323414
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:18:00+00:00 2026-05-17T23:18:00+00:00

Java Socket Server I have a Java process that is creating a listener on

  • 0

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));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:18:00+00:00Added an answer on May 17, 2026 at 11:18 pm

    While TCP is a stream in concept, the underlying IP sends and receives packets. For the application this means that you always read from socket in a loop, because single “write” by the sender might result in multiple “reads” by the receiver, and the other way around.

    This boils down to the fact that it’s up to the TCP/IP stack implementation how to split the data you send into packets. The receiving side, again the network stack, does not know how many bytes to expect. It just takes what it got and wakes up the process that waits on that socket, if there’s one, or buffers the bytes otherwise.

    Your case is easy – just keep reading from the socket until you see the EOF, i.e. zero bytes read. But in general you need an application-level protocol that either defines message lengths, establishes rules for message boundaries, or embeds explicit length information into messages themselves.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all. I have a server written in java using the ServerSocket and Socket
I'm using a Java socket, connected to a server. If I send a HEADER
Java Newbie here. I have a JFrame that I added to my netbeans project,
I realize that since UNIX sockets are platform-specific, there has to be some non-Java
My RMI enabled application seems to be leaking sockets. I have a Java application
(Java question) If I reference a field in an inner class, does this cause
I want to know, how sockets are implemented in the Java Virtual Machine. Is
Java has generics and C++ provides a very strong programming model with template s.
Java has a convenient split method: String str = The quick brown fox; String[]
Java is one of my programming languages of choice. I always run into the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.