Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: connection refused error java networking please help sir

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default connection refused error java networking please help sir

    iam getting connection refused error. i have disabled firewall and antivirus...please help sir please



    GreetingClient.java

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

    public class GreetingClient
    {
    public static void main(String [] args)
    {
    String serverName = args[0];
    int port = Integer.parseInt(args[1]);
    try
    {
    System.out.println("Connecting to " + serverName
    + " on port " + port);
    Socket client = new Socket(serverName, port);
    System.out.println("Just connected to "
    + client.getRemoteSocketAddress());
    OutputStream outToServer = client.getOutputStream();
    DataOutputStream out =
    new DataOutputStream(outToServer);

    out.writeUTF("Hello from "
    + client.getLocalSocketAddress());
    InputStream inFromServer = client.getInputStream();
    DataInputStream in =
    new DataInputStream(inFromServer);
    System.out.println("Server says " + in.readUTF());
    client.close();
    }catch(IOException e)
    {
    e.printStackTrace();
    }
    }
    }


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

    public class GreetingServer extends Thread
    {
    private ServerSocket serverSocket;

    public GreetingServer(int port) throws IOException
    {
    serverSocket = new ServerSocket(port);
    serverSocket.setSoTimeout(10000);
    }

    public void run()
    {
    while(true)
    {
    try
    {
    System.out.println("Waiting for client on port " +
    serverSocket.getLocalPort() + "...");
    Socket server = serverSocket.accept();
    System.out.println("Just connected to "
    + server.getRemoteSocketAddress());
    DataInputStream in =
    new DataInputStream(server.getInputStream());
    System.out.println(in.readUTF());
    DataOutputStream out =
    new DataOutputStream(server.getOutputStream());
    out.writeUTF("Thank you for connecting to "
    + server.getLocalSocketAddress() + "\nGoodbye!");
    server.close();
    }catch(SocketTimeoutException s)
    {
    System.out.println("Socket timed out!");
    break;
    }catch(IOException e)
    {
    e.printStackTrace();
    break;
    }
    }
    }
    public static void main(String [] args)
    {
    int port = Integer.parseInt(args[0]);
    try
    {
    Thread t = new GreetingServer(port);
    t.start();
    }catch(IOException e)
    {
    e.printStackTrace();
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: connection refused error java networking please help sir

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Please copy the full text of the error message and paste it here.

    Please explain how the code is being executed. What PCs are used and what OSs are used?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: connection refused error java networking please help sir

    Please read
    Announcements - What's Wrong With My Code?
    Pay extra attention to points 1 and 3

Similar Threads

  1. Connection refused connect
    By gopikrishnan in forum Java Networking
    Replies: 1
    Last Post: October 16th, 2013, 07:00 AM
  2. Error in URL connection
    By HardikDobaria in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2012, 07:50 AM
  3. java.net.connectexception connection refused connect
    By gagangujral in forum Java Networking
    Replies: 4
    Last Post: May 6th, 2011, 03:49 AM
  4. JDBC connection error
    By nrao in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 11th, 2010, 12:43 PM
  5. Connection refused: connect
    By kirts in forum Java Networking
    Replies: 0
    Last Post: April 16th, 2010, 11:53 AM