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 7926235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:41:21+00:00 2026-06-03T18:41:21+00:00

I have a socket server running on an android device. Then from a client

  • 0

I have a socket server running on an android device. Then from a client I send to the server a string and the server returns the same(echo server). The thing that I am trying to do is to get some other strings too, not only the one that I send it. Here is the server code:

boolean finished = false;   
try {
  DataInputStream in = new DataInputStream(client.getInputStream());
  PrintStream out = new PrintStream(client.getOutputStream());                                
  // Print a message:
  System.out.println("Client from : " + client.getInetAddress() + " port " + client.getPort());
  // now get the input from the socket...
  while(!finished) {
    String st = in.readLine();
    // Send the same back to client
    if (st.equals("hello")) {
      cur = dbHelper.getRandomQuestion();
      String question = cur.getString(cur.getColumnIndex("QUESTIONS"));
      String answer1 = cur.getString(cur.getColumnIndex("ANSWER1"));
      String answer2 = cur.getString(cur.getColumnIndex("ANSWER2"));
      String answer3 = cur.getString(cur.getColumnIndex("ANSWER3"));
      String answer4 = cur.getString(cur.getColumnIndex("ANSWER4"));
      out.println(question);                                        
    }                                    
    out.println(st);

Here is the client code:

InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: SENDING...");
Socket socket = new Socket(serverAddr, 5000);
System.out.println("Connected to " + socket.getInetAddress() + ":" + socket.getPort());
connected = true;
while(connected) {
  try {                         
    BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );                        
    PrintStream out = new PrintStream(socket.getOutputStream());                                            
    out.println(data.getText());                                 
    line = in.readLine();  // read server
    System.out.println("Echo: " + line);
    handler.post(updateUI); // here i change a textview to show the echoed string
    in.close();  // Close stream
    out.close();
  } catch (Exception e) {
    Log.e("ClientActivity", "S: Error", e);                             
  }

  connected = false;
}

I want to return answer1,answer2,asnwer3,answer4 also. How i could do that?

  • 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-03T18:41:24+00:00Added an answer on June 3, 2026 at 6:41 pm

    You are dealing with Strins, so i’d suggest to use readUTF() and writeUTF().
    The point is that you can send only one string at time. If you want to send more strings you have to create a single String and putting a sort of spacer between the strings.

    When i wrote somthing similar i used the newline character as the spacer and formatted my string in the following way:

    String1 + SPACER + String2 + SPACER + String….

    On the other side when you receive this long string you can use the split method to get an array of strings

    String[] answers = input.slpit(SPACER);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Java socket client app, that sends same string to socket
Have a server-socket running in an android application, which I debug using the emulator.
I have a socket.io server running and a matching webpage with a socket.io.js client.
I have a server class which connects a client on a specific server socket
So I have an android application that needs to connect to a socket server.
I have an android emulator I'm running from eclipse and just trying to connect
We have an async socket server written in C#. (running on Windows Web Server
Here's my problem: I have server A, running node.js and using socket.io for communicating
I'm having trouble connecting a socket from my android phone to my computer running
I have a windows server running IIS and node.js with socket.io for an interactive

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.