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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:30:27+00:00 2026-05-25T19:30:27+00:00

SO I just tried to read text from a socket, and I did the

  • 0

SO I just tried to read text from a socket, and I did the following:

import java.io.*;
import java.net.*;

public class apples{
    public static void main(String args[]) throws IOException{
        Socket client = null;
        PrintWriter output = null;
        BufferedReader in = null;
        try {
               client = new Socket("127.0.0.1", 2235);
               output = new PrintWriter(client.getOutputStream(), false);
               in = new BufferedReader(new InputStreamReader(client.getInputStream()));

               while (true) {
                       System.out.println("Line: " + client.getOutputStream());
               }
        }
        catch (IOException e) {
            System.out.println(e);
        }
        output.close();
        in.close();
        client.close();
    }
}

This prints out weird numbers and stuff like:

java.net.SocketOutputStream@316f673e

I’m not really sure of all the Java functions and things, so how would I make the output print out as text?

  • 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-25T19:30:28+00:00Added an answer on May 25, 2026 at 7:30 pm

    look at:

    while (true) {
         System.out.println("Line: " + client.getOutputStream());
    }
    

    getOutputSteam() returns an object that represents a stream. you can use this object to send data through the stream. here’s an example:

    BufferedOutputStream out = new BufferedOutputStream(this._socket.getOutputStream());
    out.write("hello");
    out.flush();
    

    this will send the message “hello” through the socket

    to read the data, you will use the inputstream instead

    let me just point out – this is a client that you are creating. You also need to create a server. Use java’s ServerSocket class for creating a server

    EDIT:
    you want to write a client/server application in java.
    you need to implement 2 processes: a client and a server.
    the server will listen on some port (using ServerSocket).
    the client will connect to that port, and send a message.

    first object you need to understand is ServerSocket.
    Server code:

    ServerSocket s = new ServerSocket(61616);    // this will open port 61616 for listening
    Socket incomingSocket = s.accept();    // this will accept new connections
    

    s.accept method is blocking – it waits for incoming connections, and goes to the next line only after a connection has been accepted. it creates a Socket object.
    for this socket object you will set up an input stream and output stream (to send/receive data).

    on the client:

    Socket s = new Socket(serverIp, serverPort);
    

    this will open a socket to the server. ip in your case will be “127.0.0.1” or “localhost” (local machine), and port will be 61616.

    you will again, set up input/output stream, to send/receive messages

    if you are connecting to a server that already exists, you only need to implement the client of course

    you can find many examples online

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

Sidebar

Related Questions

(VB.NET, .NET 3.5) I wrote the following function to read some text from txt
I have the following basic code to read a text file from a StreamReader:
I just downloaded the Oxygene free command line compiler, and tried to read the
I just read Emacs :TODO indicator at left side , and tried it out.
I have just tried adding a WebService (asmx based) from a WebForms project to
I'm trying to just get rid of duplicate consecutive words from a text file,
I have a text read from a XML file stored in UTF8 encoding. C#
I am trying to read a text file using Dynamics AX. However, the following
I want to read a text file from my local directory, I added the
I am exporting UTF-8 text from Excel and I want to read and parse

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.