By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

Help need with sockets again (nearly there though!)

100+
P: 153
Hi all,

I have my little message program working until i press a button and then it'll go to actionPerformed method, and stop reading but will keep sending a message which is being received. Obviously, this is because the send is in the actionperformed method but the read is in the main method. Anyone offer any solutions to getting my program reading again after actionmethod() has been called. I ve tried to just create a method for read which is called at the end of main method and then at the end actionperformed() method but got a load of errors to do with static variables and methods.

I ll paste my code, any help is appreciated.

p.s. this is my first attempt at kind of networking so it might not be designed that way, it's just been trial and error for now!

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.     public static JFrame aFrame; 
  24.     public static JButton aButton; 
  25.     public static JButton aButton2; 
  26.     public static JTextField inputArea; 
  27.     public static JTextArea inputArea2; 
  28.  
  29.     static ServerSocket server; 
  30.     static Socket socket; 
  31.     static PrintStream outStream; 
  32.     static BufferedReader inStream; 
  33.  
  34.     public Main() 
  35.     { 
  36. aFrame = new JFrame(); 
  37. aFrame.setSize(500, 500); 
  38. aFrame.setTitle("Input your message"); 
  39. aFrame.setLayout(new BorderLayout()); 
  40. aFrame.setVisible(true); 
  41.  
  42. aButton = new JButton(); 
  43. aButton.setText("Press to send message"); 
  44. aButton.setSize(200, 25); 
  45. aButton.addActionListener(this); 
  46. aFrame.add(aButton, BorderLayout.NORTH); 
  47.  
  48. aButton2 = new JButton(); 
  49. aButton2.setText("Press to clear message"); 
  50. aButton2.setSize(200, 25); 
  51. aFrame.add(aButton2, BorderLayout.EAST); 
  52. aButton2.setVisible(true); 
  53.  
  54.  inputArea = new JTextField(50); 
  55. inputArea.setVisible(true); 
  56. aFrame.add(inputArea, BorderLayout.SOUTH); 
  57.  
  58. inputArea2 = new JTextArea(25, 50); 
  59. inputArea2.setVisible(true); 
  60. aFrame.add(inputArea2, BorderLayout.CENTER); 
  61.  
  62.  
  63.     } 
  64. public static void main(String[] args) 
  65. try 
  66. {     
  67.     Main aMain = new Main(); 
  68.     server = new ServerSocket(4244); 
  69.     //socket = new Socket("192.168.0.6", 4242); 
  70.     socket = server.accept(); 
  71.     inStream = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
  72.     outStream = new PrintStream(socket.getOutputStream()); 
  73.     //String aString = inStream.readLine(); 
  74.     /**while(!aString.equals("exit")) 
  75.     Main.inputArea2.setText(aString); 
  76.     aString = Main.inStream.readLine(); 
  77.     outStream.print(aString);
  78. } */
  79.  
  80.  
  81.     String line;
  82.  
  83.       while  (!(line = inStream.readLine()).equals("exit") )
  84.       {
  85.         Main.inputArea2.append (line + '\n');
  86.  
  87.       }
  88.  
  89.     socket.close(); 
  90. catch (Exception anException) 
  91.     System.out.println("Error: " + anException); 
  92. public void actionPerformed(ActionEvent e) 
  93. String aString = Main.inputArea.getText(); 
  94.     if(aString.equals("exit")) 
  95.         { 
  96.  
  97.             System.exit(1); 
  98.         } 
  99.     else 
  100.     { 
  101.         Main.outStream.println(aString); 
  102.         aString = Main.inputArea.getText(); 
  103.         Main.inputArea.setText(" ");
  104.  
  105.     } 
  106.  
  107.  
  108.  
Oct 29 '08 #1
Share this Question
Share on Google+
1 Reply


100+
P: 153
no worries, solved it
Oct 29 '08 #2

Post your reply

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