Why does this work but this doesn't? ServerSocket

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    Why does this work but this doesn't? ServerSocket

    Hi all

    I have managed to suss out Sockets today and i have got a gui going with netbeans that sends text on button press to a serversocket on another computer. I receive the messages when i open serversocket on simple notepad/cmd. Now i am trying to design a gui for my serversocket. my code that works....

    Code:
    import java.net.*;
    import java.io.*;
    
    
    public class ConnectTest
    {
    public static void main(String[] args)
    {
    try
    {
    
    ServerSocket server;
    Socket socket = null;
    BufferedReader inStream = null;
    
    
    
    server = new ServerSocket(4242);
    socket = server.accept();
    inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    String aString = inStream.readLine();
    while(aString != "exit")
    {
    System.out.println(aString);
    aString = inStream.readLine();
    }
    socket.close();
    }
    catch(Exception anException)
    {
    }
    }
    }
    and my code that doesn't work when trying to have a gui with netbeans...

    Code:
    /*
     * Main.java
     *
     * Created on 28 October 2008, 13:19
     *
     * To change this template, choose Tools | Options and locate the template under
     * the Source Creation and Management node. Right-click the template and choose
     * Open. You can then make changes to the template in the Source Editor.
     */
    
    package clientside;
    
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class Main implements ActionListener
    {
        public static JFrame aFrame;
        public static JButton aButton;
        public static JButton aButton2;
        public static JTextField inputArea;
        public static JLabel inputArea2;
        
        static ServerSocket server;
        static Socket socket;
        static PrintStream outStream;
        static BufferedReader inStream;
        
        public Main()
        {
    aFrame = new JFrame();
    aFrame.setSize(500, 500);
    aFrame.setTitle("Input your message");
    aFrame.setLayout(new FlowLayout());
    aFrame.setVisible(true);
    
    aButton = new JButton();
    aButton.setText("Press to send message");
    aButton.setSize(200, 25);
    aButton.addActionListener(this);
    aFrame.add(aButton);
    
    aButton2 = new JButton();
    aButton2.setText("Press to clear message");
    aButton2.setSize(200, 25);
    aFrame.add(aButton2);
    aButton2.setVisible(true);
    
     inputArea = new JTextField(50);
    inputArea.setVisible(true);
    aFrame.add(inputArea);
    
    inputArea2 = new JLabel("hello");
    inputArea2.setVisible(true);
    aFrame.add(inputArea2);
    
    
        }
    public static void main(String[] args)
    {
    try
    {    
        Main aMain = new Main();
        server = new ServerSocket(4244);
        //socket = new Socket("192.168.0.6", 4242);
        socket = server.accept();
        inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //outStream = new PrintStream(socket.getOutputStream());
        String aString = inStream.readLine();
        while(aString.equals("exit"))
    {
        Main.inputArea2.setText(aString);
        aString = Main.inStream.readLine();
    }
    }
    catch (Exception anException)
    {
        System.out.println("Error: " + anException);
    }
    }
    public void actionPerformed(ActionEvent e)
    {
    String aString = Main.inputArea.getText();
        if(aString.equals("exit"))
            {
                //socket.close();
                System.exit(1);
            }
        else
        {
            Main.outStream.println(aString);
            aString = Main.inputArea.getText();
        }
    }
    
    }
    You may have noticed that i ave shaded a few lines of code out, that's for once i have got the text sent from socket displaying on my serversocket gui. However, if you can answer this, can this kind of code work..

    Code:
    server = new ServerSocket(4244);
    socket = new Socket("192.168.0.6", 4242);
    socket = server.accept();
    inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    outStream = new PrintStream(socket.getOutputStream());
    String aString = inStream.readLine();
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    #2
    i have just noticed this exception when i press to send message
    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at clientside.Main.actionPerformed(Main.java:77)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
            at java.awt.Component.processMouseEvent(Component.java:5488)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
            at java.awt.Component.processEvent(Component.java:5253)
            at java.awt.Container.processEvent(Container.java:1966)
            at java.awt.Component.dispatchEventImpl(Component.java:3955)
            at java.awt.Container.dispatchEventImpl(Container.java:2024)
            at java.awt.Component.dispatchEvent(Component.java:3803)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
            at java.awt.Container.dispatchEventImpl(Container.java:2010)
            at java.awt.Window.dispatchEventImpl(Window.java:1774)
            at java.awt.Component.dispatchEvent(Component.java:3803)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
            at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
    Last edited by Nepomuk; Oct 28 '08, 09:54 PM. Reason: Added [CODE] tags, although not stricktly needed here, but it's much easier to read.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      I started reading but near the begining I found this again: aString != "exit" which
      is a brain dead nono. (it probably won't solve your problem but it was worth to
      mention again). It's 10:00pm overhere; tomorrow is another day ;-)

      kind regards,

      Jos

      Comment

      • brendanmcdonagh
        New Member
        • Nov 2007
        • 153

        #4
        I worked you good today Jos!!!

        I ve just solved it so maybe i can say enough is enough for today!!!

        Good night

        Brendan

        Comment

        Working...