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?
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 ();
}
}