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 2 of 2

Thread: Connection refused connect

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

    Default Connection refused connect

    i was just trying a socket programming example; im new to networking and im getting a connection refused error; i have disabled the firewall; im getting output if im running client and server programs on the same machine but if im trying on different machine , i got errors. can anybody help me??? here iam attaching the code


    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
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Connection refused connect

    How many more threads are you planning to post about the same topic?
    http://www.javaprogrammingforums.com...tml#post126326

Similar Threads

  1. DB Connecting Refused for STAGE Environment.
    By rammohan0143 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 7th, 2013, 02:00 AM
  2. type -4 refused with oracle
    By muzammil786 in forum JDBC & Databases
    Replies: 0
    Last Post: February 14th, 2013, 07:57 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. Connection refused: connect
    By kirts in forum Java Networking
    Replies: 0
    Last Post: April 16th, 2010, 11:53 AM
  5. how can i connect with a dial up connection
    By reguapo in forum Java Networking
    Replies: 1
    Last Post: February 2nd, 2010, 06:21 AM