Results 1 to 5 of 5
  1. #1
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default Simple TCP utility failing right out the gate

    I'm trying to set up TCP communications between two machines, but for now (in the initial/testing phase) am using localhost.

    Can anybody see why I'm getting these msgs in the Console (all I'm doing is running the app, not even pressing the button:

    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 com.jcp.tds.RTTCPSortSimMain.setUpNetworking(RTTCP SortSimMain.java:104)
    at com.jcp.tds.RTTCPSortSimMain.go(RTTCPSortSimMain.j ava:91)
    at com.jcp.tds.RTTCPSortSimMain.main(RTTCPSortSimMain .java:34)
    Exception in thread "Thread-2" java.lang.NullPointerException
    at com.jcp.tds.RTTCPSortSimMain$IncomingReader.run(RT TCPSortSimMain.java:138)
    at java.lang.Thread.run(Unknown Source)

    with the following code (which is basically ripped straight from "Head First Java"):

    public class RTTCPSortSimMain {

    ...
    BufferedReader reader;
    PrintWriter writer;
    Socket sock;

    public static void main(String[] args) {
    new RTTCPSortSimMain().go();
    }

    public void go() {
    ... (GUI code)
    JButton sendButton = new JButton("Send and Receive");
    sendButton.addActionListener(new SendButtonListener());

    tfHost = new JTextField(16);
    tfHost.setText("127.0.0.1");
    tfPort = new JTextField(4);
    tfPort.setText("4211");

    ... (more GUI code)

    setUpNetworking();

    Thread readerThread = new Thread(new IncomingReader());
    readerThread.start();

    ... (more GUI code)

    }

    private void setUpNetworking() {
    try {
    // for testing (localhost)
    sock = new Socket("127.0.0.1", 5000);
    InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
    reader = new BufferedReader(streamReader);
    writer = new PrintWriter(sock.getOutputStream());
    System.out.println("networking established");
    }
    catch(IOException ex)
    {
    ex.printStackTrace();
    }
    }

    public class SendButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent ev) {
    try {
    writer.println(outgoing.getText());
    writer.flush();

    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
    outgoing.setText("");
    outgoing.requestFocus();
    }
    }

    class IncomingReader implements Runnable {
    @Override
    public void run() {
    String message;
    try {
    while ((message = reader.readLine()) != null) {
    System.out.println("client read " + message);
    incoming.append(message + "\n");
    }
    } catch (IOException ex)
    {
    ex.printStackTrace();
    }
    }
    }

    }

    ?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,886
    Rep Power
    25

    Default

    at java.net.Socket.<init>(Unknown Source)
    at com.jcp.tds.RTTCPSortSimMain.setUpNetworking(RTTCP SortSimMain.java:104)
    What code is at line 104? It is trying to create a new Socket (<init> means in the constructror)

    Is there a server at that IP and port that the program is trying to connect to?
    Java Code:
    sock = new Socket("127.0.0.1", 5000);

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,886
    Rep Power
    25

    Default

    at java.net.Socket.<init>(Unknown Source)
    at com.jcp.tds.RTTCPSortSimMain.setUpNetworking(RTTCP SortSimMain.java:104)
    What code is at line 104? It is trying to create a new Socket (<init> means in the constructror)

    Is there a server at that IP and port that the program is trying to connect to?
    Java Code:
    sock = new Socket("127.0.0.1", 5000);

  4. #4
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What code is at line 104? It is trying to create a new Socket (<init> means in the constructror)

    Is there a server at that IP and port that the program is trying to connect to?
    Java Code:
    sock = new Socket("127.0.0.1", 5000);
    Well, that should have been obvious, I guess - I don't have a server running there waiting to be contacted. So how can I test whether the connection is made. Is there a "test" port I can try that will allow me to bind and perhaps send back an "Ack"?

    To answer your question, the code around line 104 is:
    private void setUpNetworking() {
    try {
    // for testing (localhost)
    sock = new Socket("127.0.0.1", 5000);
    //sock = new Socket(JAYAS_IP_ADDR, 5000);
    InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
    reader = new BufferedReader(streamReader);
    writer = new PrintWriter(sock.getOutputStream());
    System.out.println("networking established");
    }
    catch(IOException ex)
    {
    ex.printStackTrace();
    }
    }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,886
    Rep Power
    25

    Default

    Is there a "test" port I can try that will allow me to bind and perhaps send back an "Ack"?
    There are free servers you can get for testing. For example Tomcat from Apache.
    Or there are some ultra simple ones here on the forum.
    Search for code with ServerSocket.

Similar Threads

  1. Synchronization utility to manage locks
    By JSL5101 in forum Advanced Java
    Replies: 1
    Last Post: 05-07-2011, 06:43 PM
  2. GATE Natural Language Processing
    By MuslimCoder in forum New To Java
    Replies: 0
    Last Post: 11-27-2010, 09:02 PM
  3. Calling a method in a utility class
    By TMobotron in forum New To Java
    Replies: 2
    Last Post: 09-17-2010, 07:39 AM
  4. XML compare utility
    By JMayura in forum Advanced Java
    Replies: 0
    Last Post: 06-08-2010, 02:05 PM
  5. Trying to use RSA encryption, failing...
    By soccermiles in forum New To Java
    Replies: 2
    Last Post: 04-26-2010, 08:53 PM

Posting Permissions

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