473,385 Members | 1,782 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,385 software developers and data experts.

Help need with sockets again (nearly there though!)

153 100+
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
1 1233
brendanmcdonagh
153 100+
no worries, solved it
Oct 29 '08 #2

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

Similar topics

1
by: Dfenestr8 | last post by:
Hi. I realise, that there's probably something intrinsic to OOP that I don't understand here. Or maybe it's something to do with sockets. I'm not sure. Basically, I'm trying to hack up an IRC...
4
by: Scott Holland | last post by:
HELP - Need to connect to DB2 database on AIX from NT server. Also AS/400 from NT Server -- I am experienced in ORACLE and a novice at DB2. What tools would be the equivalent of Net*8 or...
5
by: Jason L James | last post by:
Hi all, as a VB.Net programmer I am finding it difficult to find a text on implementing sockets to communicate with an embedded web server. Does anyone know of a good resource, either text...
0
by: Valleriani | last post by:
Right Now I've been working on a text based game for fun. It was working fine but they I relized that you need to shut/close the ports. Before I was just opening ports and not shutting them, no...
3
by: artemio | last post by:
Hello all! I need some help with sockets. I have this code: $Connection = fsockopen( "google.com", 80, $ErrNo, $ErrStr, 30 ); if($Connection){ echo "Connection established\n";
6
by: Burt | last post by:
I need to create a collection of classes (or structures) can be accessed by a string key, eg MyColl("ShortName5").Name for class with key ShortName5. But it also has to be sorted by a second...
0
by: George Burdell | last post by:
I'm pretty new to Windows programming (but not programming in general). I'm trying to write a program that will generate IGMP packets where I can control all of the payload in the packet but I'm...
3
by: danofweaver | last post by:
How can I practice the use of SQL server without changing my girlfriends laptop to XP Pro (which anyway I dont have)? Using (unfortunately) Windows XP Home not Pro, Visual Studio 2005. Trying to...
2
by: manontheedge | last post by:
I've got two separate programs, one being the "server" and the other one (this one) being the "client". I've got the "server" to where it just sits and waits for connections, and sends out whatever...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.