Results 1 to 1 of 1
- 08-04-2009, 09:52 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 7
- Rep Power
- 0
Mini network application test and help required
Can you people please help me with this one... I have written a small LAN chat-room.
The reason for this is for me to learn networking. This little program seems to look fine but I don't have the equipment to test it. (2 PC's on LAN)
Can someone please try to test it and please tell me what I am doing wrong?
Java Code:import java.io.*; import java.net.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Main implements ActionListener { static JTextArea ta1 = new JTextArea (10, 10); static JButton btn1 = new JButton ("Click me"); static JTextField txt1 = new JTextField (10); static String newMessage = "\n" + " ------------------------------------------------" + "\n"; static int socketNumber = 3333; static String IP; static ServerSocket mySocket; static Socket connectionSocket; static DataOutputStream outStream; static DataInputStream inStream; static BufferedReader inReader; static BufferedReader outReader; Main () { try { mySocket = new ServerSocket (socketNumber); } catch (IOException abc) { ta1.append (" Can't connect!" + "\n " + abc + newMessage); } // Die GUI se uitleg JFrame myframe = new JFrame ("Chatr BOX"); myframe.setSize (500, 500); myframe.getContentPane ().setBackground (Color.red); BorderLayout border = new BorderLayout (); myframe.getContentPane ().setLayout (border); btn1.setBackground (Color.blue); btn1.setForeground (Color.white); btn1.addActionListener (this); txt1.addActionListener (this); ta1.setEditable (false); ta1.setText (" Type \"host\" to host a session\n Type \"join\" to join a session" + newMessage); myframe.getContentPane ().add (btn1); myframe.getContentPane ().add (txt1); myframe.getContentPane ().add (ta1); myframe.getContentPane ().add ("North", txt1); myframe.getContentPane ().add ("South", btn1); myframe.getContentPane ().add ("Center", ta1); myframe.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); myframe.setLocationRelativeTo (null); myframe.setVisible (true); // Einde van die GUI se uitleg } public void actionPerformed (ActionEvent e) { if (e.getSource () == btn1) { String text = txt1.getText (); if (text.equals ("join")) { try { IP = JOptionPane.showInputDialog ("Type the IP you wish to connect to (e.g. 192.168.0.15)"); connectionSocket = new Socket (IP, socketNumber); outStream = new DataOutputStream (connectionSocket.getOutputStream ()); inReader = new BufferedReader (new InputStreamReader (connectionSocket.getInputStream ())); ta1.append (" Succesfully connected to:\n " + IP + newMessage); } catch (IOException abc) { ta1.append (" Can't connect!" + "\n " + abc + newMessage); } } else if (text.equals ("host")) { try { connectionSocket = mySocket.accept (); outStream = new DataOutputStream (connectionSocket.getOutputStream ()); inReader = new BufferedReader (new InputStreamReader (connectionSocket.getInputStream ())); ta1.append (" Succesfully hosted" + newMessage); } catch (IOException abc) { ta1.append (" Can't connect!" + "\n " + abc + newMessage); } } else if (connectionSocket == null) { ta1.append (" You are not connected to any chat sessions\n Type \"host\" to host a session\n Type \"join\" to join a session" + newMessage); } else { try { outStream.writeBytes (text); } catch (IOException abc) { ta1.append (" Unable to send!" + "\n " + abc + newMessage); } ta1.append (" Sent: " + text + newMessage); } txt1.setText (""); } } static void receiveText () { while (true) { if (connectionSocket != null) { try { inReader = new BufferedReader (new InputStreamReader (connectionSocket.getInputStream ())); ta1.append (" Received: " + inReader.readLine () + newMessage); } catch (IOException abc) { ta1.append (" Can't connect!" + "\n " + abc + newMessage); } } } } public static void main (String[] args) throws Exception { Main xyz = new Main (); receiveText (); } }
Similar Threads
-
Java Based Application to Map Network Drive
By Wraithier in forum New To JavaReplies: 15Last Post: 05-24-2012, 11:09 AM -
Test JEE application (already tetsed under tomcat6) under JBoss Eclipse
By kapusta117 in forum Web FrameworksReplies: 0Last Post: 07-23-2009, 03:37 PM -
stand alone application network change
By Java.child in forum New To JavaReplies: 3Last Post: 09-07-2008, 11:11 AM -
A simple application to test the functionality of the OvalIcon class
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:16 PM -
Java application help and suggestions required
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-18-2007, 03:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks