473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Why does this work but this doesn't? ServerSocket

153 100+
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....

Expand|Select|Wrap|Line Numbers
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4.  
  5. public class ConnectTest
  6. {
  7. public static void main(String[] args)
  8. {
  9. try
  10. {
  11.  
  12. ServerSocket server;
  13. Socket socket = null;
  14. BufferedReader inStream = null;
  15.  
  16.  
  17.  
  18. server = new ServerSocket(4242);
  19. socket = server.accept();
  20. inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  21. String aString = inStream.readLine();
  22. while(aString != "exit")
  23. {
  24. System.out.println(aString);
  25. aString = inStream.readLine();
  26. }
  27. socket.close();
  28. }
  29. catch(Exception anException)
  30. {
  31. }
  32. }
  33. }
  34.  
and my code that doesn't work when trying to have a gui with netbeans...

Expand|Select|Wrap|Line Numbers
  1.  
  2. /*
  3.  * Main.java
  4.  *
  5.  * Created on 28 October 2008, 13:19
  6.  *
  7.  * To change this template, choose Tools | Options and locate the template under
  8.  * the Source Creation and Management node. Right-click the template and choose
  9.  * Open. You can then make changes to the template in the Source Editor.
  10.  */
  11.  
  12. package clientside;
  13.  
  14. import java.io.*;
  15. import java.net.*;
  16. import java.util.*;
  17. import javax.swing.*;
  18. import java.awt.*;
  19. import java.awt.event.*;
  20.  
  21.  
  22. public class Main implements ActionListener
  23. {
  24.     public static JFrame aFrame;
  25.     public static JButton aButton;
  26.     public static JButton aButton2;
  27.     public static JTextField inputArea;
  28.     public static JLabel inputArea2;
  29.  
  30.     static ServerSocket server;
  31.     static Socket socket;
  32.     static PrintStream outStream;
  33.     static BufferedReader inStream;
  34.  
  35.     public Main()
  36.     {
  37. aFrame = new JFrame();
  38. aFrame.setSize(500, 500);
  39. aFrame.setTitle("Input your message");
  40. aFrame.setLayout(new FlowLayout());
  41. aFrame.setVisible(true);
  42.  
  43. aButton = new JButton();
  44. aButton.setText("Press to send message");
  45. aButton.setSize(200, 25);
  46. aButton.addActionListener(this);
  47. aFrame.add(aButton);
  48.  
  49. aButton2 = new JButton();
  50. aButton2.setText("Press to clear message");
  51. aButton2.setSize(200, 25);
  52. aFrame.add(aButton2);
  53. aButton2.setVisible(true);
  54.  
  55.  inputArea = new JTextField(50);
  56. inputArea.setVisible(true);
  57. aFrame.add(inputArea);
  58.  
  59. inputArea2 = new JLabel("hello");
  60. inputArea2.setVisible(true);
  61. aFrame.add(inputArea2);
  62.  
  63.  
  64.     }
  65. public static void main(String[] args)
  66. {
  67. try
  68. {    
  69.     Main aMain = new Main();
  70.     server = new ServerSocket(4244);
  71.     //socket = new Socket("192.168.0.6", 4242);
  72.     socket = server.accept();
  73.     inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  74.     //outStream = new PrintStream(socket.getOutputStream());
  75.     String aString = inStream.readLine();
  76.     while(aString.equals("exit"))
  77. {
  78.     Main.inputArea2.setText(aString);
  79.     aString = Main.inStream.readLine();
  80. }
  81. }
  82. catch (Exception anException)
  83. {
  84.     System.out.println("Error: " + anException);
  85. }
  86. }
  87. public void actionPerformed(ActionEvent e)
  88. {
  89. String aString = Main.inputArea.getText();
  90.     if(aString.equals("exit"))
  91.         {
  92.             //socket.close();
  93.             System.exit(1);
  94.         }
  95.     else
  96.     {
  97.         Main.outStream.println(aString);
  98.         aString = Main.inputArea.getText();
  99.     }
  100. }
  101.  
  102. }
  103.  
  104.  
  105.  
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..

Expand|Select|Wrap|Line Numbers
  1.  
  2. server = new ServerSocket(4244);
  3. socket = new Socket("192.168.0.6", 4242);
  4. socket = server.accept();
  5. inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  6. outStream = new PrintStream(socket.getOutputStream());
  7. String aString = inStream.readLine();
  8.  
  9.  
Oct 28 '08 #1
3 2133
brendanmcdonagh
153 100+
i have just noticed this exception when i press to send message
Expand|Select|Wrap|Line Numbers
  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  2.         at clientside.Main.actionPerformed(Main.java:77)
  3.         at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
  4.         at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
  5.         at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
  6.         at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
  7.         at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
  8.         at java.awt.Component.processMouseEvent(Component.java:5488)
  9.         at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
  10.         at java.awt.Component.processEvent(Component.java:5253)
  11.         at java.awt.Container.processEvent(Container.java:1966)
  12.         at java.awt.Component.dispatchEventImpl(Component.java:3955)
  13.         at java.awt.Container.dispatchEventImpl(Container.java:2024)
  14.         at java.awt.Component.dispatchEvent(Component.java:3803)
  15.         at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
  16.         at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
  17.         at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
  18.         at java.awt.Container.dispatchEventImpl(Container.java:2010)
  19.         at java.awt.Window.dispatchEventImpl(Window.java:1774)
  20.         at java.awt.Component.dispatchEvent(Component.java:3803)
  21.         at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
  22.         at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
  23.         at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
  24.         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
  25.         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
  26.         at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Oct 28 '08 #2
JosAH
11,448 Expert 8TB
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
Oct 28 '08 #3
brendanmcdonagh
153 100+
I worked you good today Jos!!!

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

Good night

Brendan
Oct 28 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Paul THompson | last post by:
I have a piece of code something like the following: START OF CODE <html><head><title>The Wizard</title></head> <body> <h1>Welcome to Joe's Vet Clinic</h1> <div id="part1"...
22
by: Robert Bralic | last post by:
CAN anybody tell me any address where I can download some small(1000-2000) lines C++ proghram source. Or send me ,a small(1000-2000) lines C++ program source that I can compille with gpp under...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
17
by: StevePBurgess | last post by:
A really simple script is driving me up the wall. I have a very simply facility on a website to allow the user to reorder items in a database table as she wishes by clicking a link that (in this...
0
by: watashi | last post by:
Hello, This is on socket. i m using tcpclient and tcplistener task is when the "Active " button is pressed according to user choice tool should work as server or client. when "Inactive"...
6
by: Samuel | last post by:
I need an open source socket components library like Delphi's ClientSocket and ServerSocket,who can tell me?if not found, give me some other suggestion(simple is best, i only need tcp).
2
by: brendanmcdonagh | last post by:
import java.io.*; import java.net.*; import java.util.*; public class SocketConnect { public static void main(String args) { try {
0
sreekandank
by: sreekandank | last post by:
Hi..... I have done the program for simple chat application in java....as given below.... server.java import java.net.*; import java.io.*; public class server { public static void...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.