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: Simple TCP utility failing right out the gate

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    13
    Thanked 1 Time in 1 Post

    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simple TCP utility failing right out the gate

    This thread has been cross posted here:

    http://www.java-forums.org/networking/46210-simple-tcp-utility-failing-right-out-gate.html#post220720

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    When posting on > 1 forum, please provide links to the other forums.


  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Simple TCP utility failing right out the gate

    Was this solved on the other forum?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. [SOLVED] Trying to call web service (and failing)
    By jamesruk21 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 15th, 2011, 12:35 PM
  2. help! simple ATM
    By galva in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 9th, 2010, 01:14 AM
  3. [SOLVED] Should be simple.
    By Time in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 22nd, 2010, 02:23 AM
  4. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM