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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:11:50+00:00 2026-05-20T05:11:50+00:00

I am unable to do any networking in Java when i run a simple

  • 0

I am unable to do any networking in Java when i run a simple networking program i am unable to get a connection, even when using the local host,
The Error:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at EchoClient.main(EchoClient.java:8)

This is the program:

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

public class EchoClient{
    public static void main(String[] args) {
        try{
            Socket client = new Socket(InetAddress.getLocalHost(), 1234);
            InputStream clientIn = client.getInputStream();
            OutputStream clientOut = client.getOutputStream();
            PrintWriter pw = new PrintWriter(clientOut);
            BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));
            Scanner stdIn = new Scanner(System.in);
            System.out.println("Input Message:");
            pw.println(stdIn.nextLine());
            System.out.println("Recieved Message:");
            System.out.println(br.readLine());
            pw.close();
            br.close();
            client.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

I use windows 7, i have turned of windows firewall and i dont have an anti virus.

  • 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-20T05:11:50+00:00Added an answer on May 20, 2026 at 5:11 am

    As I wrote in my comment, check that the server (something like EchoServer?) is up and running.


    But there are other problems when you succeed connecting. pw.println(stdIn.nextLine()); might not send the content to the server, you need to do a pw.flush(); to really send the content or you can create your PrintWriter with autoflushing:

     pw = new PrintWriter(clientOut, true);
    

    If you need a EchoServer I just wrote one that works with your client if you add the flushing that I described above:

    public class EchoServer {
        public static void main(String[] args) throws IOException {
            ServerSocket ss = new ServerSocket(1234);
    
            while (true) {
                // accept the connection 
                Socket s = ss.accept();
    
                try {
                    Scanner in = new Scanner(s.getInputStream());
                    PrintWriter out = new PrintWriter(s.getOutputStream(), true);
    
                    // read a line from the client and echo it back
                    String line;
                    while ((line = in.nextLine()) != null) 
                        out.println(line);
    
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    s.close();
                }
            }
        }
    }
    

    You can try it with telnet localhost 1234.

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

Sidebar

Related Questions

I am unable to get any test cases that extend ServiceTestCase to run. There
I'm using MySQL, of course. However, I'm unable to run any while loop in
I'm unable to run any selenium tests since I updated Firefox to 3.6. Is
This is a simple question yet I was unable to find any information at
Any ideas why does the entity framework in LINQ gives following strange error: Unable
Trying to find some information on this but am unable to get any results
For whatever reason, I have been unable to get any table cells to have
I found this page and was unable to get any useful information out of
I am unable to get any feedback from my Facebook FB.UI() method. I am
I'm using an AdMob view in an android app, but I'm unable to get

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.