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

  • Home
  • SEARCH
  • 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 7936275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:09:51+00:00 2026-06-03T22:09:51+00:00

I am trying to send multiple data from server to client using TCP. I

  • 0

I am trying to send multiple data from server to client using TCP. I want to create only one TCP connection for the entire session. How do I go about doing this?

I tried a code with the following flow, but the program stops after the first response is received.

Client side

1.create sockets and streams
2.send request for first data
3.wait for response from server
4.send next request <----------- server doesn't seem to handle this request
5.get next response from server

Server side

1.Create server socket and wait for incoming connections
2.Parse incoming request
3.Send response
4.Parse next request
5.Send next response

I do not close the sockets and streams on both sides while the session is alive.

Update
Here is my code snippet:
Client

public void processRequest() throws Exception {

    Socket tempSocket = new Socket("0.0.0.0", 6782);

    String requestLine = "This is request message 1" + CRLF;

    DataOutputStream outToServer = new DataOutputStream(tempSocket.getOutputStream());            
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(tempSocket.getInputStream())); 

    outToServer.writeBytes(requestLine + CRLF);

    String serverResponse = inFromServer.readLine();
    System.out.println(serverResponse);

    requestLine = "This is request message 2" + CRLF;

    outToServer.writeBytes(requestLine + CRLF);

    serverResponse = inFromServer.readLine();
    System.out.println(serverResponse);

    outToServer.close();
    inFromServer.close();
    tempSocket.close();
}

Server

public void processRequest() throws Exception {

    createConnections();

    String requestLine = inFromClient.readLine();
    System.out.println(requestLine);

    String responseLine = "This is the response to messsage 1";
    outToClient.writeBytes(responseLine + CRLF);

    requestLine = inFromClient.readLine();
    System.out.println(requestLine);

    responseLine = "This is the response to message 2";
    outToClient.writeBytes(responseLine + CRLF);
}

Output

Client:

This is the response to messsage 1
This is the response to message 2
BUILD SUCCESSFUL (total time: 1 second)

Server:

This is request message 1

null
java.net.SocketException: Broken pipe
  • 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-06-03T22:09:53+00:00Added an answer on June 3, 2026 at 10:09 pm

    I think the problem is in your client code. You wrote:

        String requestLine = "This is request message 1" + CRLF;
        .....
        outToServer.writeBytes(requestLine + CRLF);
    

    You add CRLF to requestLine, and add it again when send it to server. Ensure that adding CRLF only once per message you want to send, it will behave as you want.

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

Sidebar

Related Questions

Im trying to send multiple data items over a tcp socket from an android
i am trying to send multiple data using j query $.ajax method to my
I'm trying to send back multiple variables from a php file to ajax using
I'm trying to insert multiple rows using SqlCommand from C# to SQL Server. I'm
I am trying to send a dictionary of information from one iphone to another
I am trying to post multiple variables from the already dynamically create-able username id's
In MVC3 Razor: I am trying to create a form, dynamically, using fields from
Trying to send an array from: <select name='galaddvenn[]' class='sel_add vl hidden' multiple='multiple'> <option value='53'>name1</option>
I'm trying to send an e-mail to multiple e-mail address in my database. Here
I'm trying to send emails from my Grails 2.0 app via Sendgrid on Heroku

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.