Results 1 to 4 of 4

Thread: Socket help

  1. #1
    80mins is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default Socket help

    Hey there,

    I am writing this code for a project, but have hit a snag.
    Everytime I try to run it it throws a

    java.net.ConnectException: Connection refused: connect Exception.

    the exception is in this line of code

    public void connect(){
    try{
    //error topline
    socket = new Socket(address, Integer.parseInt(port));
    out = new ObjectOutputStream(socket.getOutputStream());
    in = new ObjectInputStream(socket.getInputStream());
    }
    catch(Exception ex){
    System.out.println("Error connect in ClientSocket" + ex.toString());
    }
    }


    I am trying to connect to a server I have created on the same computer.
    this is the code that uses it to connect.
    address = location of the database i am trying to connect to (file type .db)
    port is a port i enter in a GUI = 1064

    try{
    if(txtLocation.getText().length() != 0){
    setDBLocation(txtLocation.getText());
    System.out.println("Sent location to properties" + location);
    setPort(txtPort.getText());
    ServerSocketClass.register(location, port);
    Data data = new Data(location);
    MainGUI m = new MainGUI();
    m.setVisible(true);
    }
    else{
    msgMessage.showMessageDialog(this, "You need to search for a database.",
    "Select a database",
    javax.swing.JOptionPane.ERROR_MESSAGE);
    this.dispose();
    SocketGUI sock = new SocketGUI();
    sock.setVisible(true);
    }
    }
    catch(Exception ex){

    }
    this.dispose();
    }

    Sorry if I have not explained everything clearly I have been trying to complete this for the last 2weeks and just cant figure out why it throws this exception or what the exception is.

    I have tryed turning of my firewalls and security to no prevale.

    Thanks

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Straight from the API documentation :

    "Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port)."

    Read it properly and then crosscheck what might have gone wrong with your code.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    What are the address and port you are using?

    Add some printlns to the code to show the values of the variables just before you use them. Before the following lines

    socket = new Socket(address, Integer.parseInt(port));

    ServerSocketClass.register(location, port);

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    What are the address and port you are using?

    Add some printlns to the code to show the values of the variables just before you use them. Before the following lines

    socket = new Socket(address, Integer.parseInt(port));

    ServerSocketClass.register(location, port);

Similar Threads

  1. Replies: 1
    Last Post: 07-02-2010, 01:18 PM
  2. swt socket
    By cuiran in forum SWT / JFace
    Replies: 2
    Last Post: 01-22-2010, 01:34 AM
  3. Socket
    By Syahrul in forum New To Java
    Replies: 0
    Last Post: 12-20-2009, 01:31 PM
  4. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 07:02 PM
  5. Socket
    By vortex in forum New To Java
    Replies: 2
    Last Post: 05-25-2008, 06:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •