7 Replies Latest reply: Mar 11, 2007 11:21 PM by EJP RSS

    IOException error while creating a socket

      Hey I am trying to create a simple server client which can exchange text msgs but it always gives me an error with the IOException,

      It never establishes the connection because an IOException is caught each time, can anyone tell me if I am doing something wrong?
      public class cliT3
           private int serverPort = 4444;
           /*Takes the host as the argument and creates the client thread*/
           public cliT3 (String host)
                Socket soc = null;
                     soc = new Socket(host,serverPort);
                catch(UnknownHostException e)
                     System.err.println("The host given is invalid");
                catch(IOException e)
                     System.err.println("ioexception caught in cliT3 constructor");
                new cliThreadT3(soc).run();
      new cliThreadT3(soc).run(); -> just creating a thread for the client and passing the socket to it, I am creating the outputstream and the inputstream in this thread
        • 1. Re: IOException error while creating a socket
          When you catch an exception, don't make up your own text, or at least use the text of the exception's message in addition to your own text. Otherwise you can't possibly find out what the exception really means.
          • 2. Re: IOException error while creating a socket
            Well this is what i got , I dont understand a thing!

            Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at cliThreadT3.run(cliThreadT3.java:55) -> code here -> if(in.ready())
            at cliT3.<init>(cliT3.java:40) -> code here -> A blank line after the catch block
            at MyHandler3.actionPerformed(examp3.java:154)->code here-> String host = "pesonal"; new cliT3(host); creating a client

            the rest below seems to be from the java itself. . .

            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
            at java.awt.Component.processMouseEvent(Component.java:6038)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
            at java.awt.Component.processEvent(Component.java:5803)
            at java.awt.Container.processEvent(Container.java:2058)
            at java.awt.Component.dispatchEventImpl(Component.java:4410)
            at java.awt.Container.dispatchEventImpl(Container.java:2116)
            at java.awt.Component.dispatchEvent(Component.java:4240)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)

            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
            at java.awt.Container.dispatchEventImpl(Container.java:2102)
            at java.awt.Window.dispatchEventImpl(Window.java:2429)
            at java.awt.Component.dispatchEvent(Component.java:4240)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

            at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
            • 3. Re: IOException error while creating a socket
              The "in" above is an variable of BufferedReader type
              • 4. Re: IOException error while creating a socket
                and it is obviously null

                and the exception occurs in code which you haven't showed us

                and whose source code clearly doesn't correspond to the .class file being executed

                and it is not an IOException at all, is it?
                • 5. Re: IOException error while creating a socket
                  The "in" above is an variable of BufferedReader type
                  This is pathetic.

                  Where in the code you posted is the Swing code?

                  You have no socket problem. You might have a Swing programming question. And probably what you really have is a total lack of knowing what you are doing which is a problem you on your own need to solve.
                  • 6. Re: IOException error while creating a socket
                    As it turns out both of you are right.

                    It is a null and the socket program works fine in the dos prompt.

                    The thing is I am trying to take the input from a text area and send it over the socket and display it, a chat box if you may. . .

                    But I am not able to get the text from the text area, it just sets it to null. . .

                    I am using the getText() method.

                    I am sorry if I sound dumb, it s just that I am in a place where I know too much to be a newbie but too less to be a pro like a no mans land, but thanks for all the help guys.
                    • 7. Re: IOException error while creating a socket
                      How about taking some of the implicit advice offered here? Recompile and redeploy your code. Rerun the test. Look at the stack trace. Go to that line in your source code. Fix the NPE or whatever it is. If you can't, post the new stack trace and the block of code it occurs in here.

                      If you don't do all that you can't rationally anybody to be able to help you here.