Hi all,
I am learning java and have developed a very basic message app which entails having a serversocket on one pc and a socket on another. This is working fine but now i want to go further and have them both to have the abiliy to wake the other up, so a kind of serversoket and socket on both. i ll paste my code for the client class and would really appreciate a pointer in which direction to go cos my head is a bit baffled wih this one!
/*
* Main.java
*
* Created on 28 October 2008, 13:19
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package clientside;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main implements ActionListener
{
public static JFrame aFrame;
public static JButton aButton;
public static JButton aButton2;
public static JTextField inputArea;
public static JTextArea output;
static ServerSocket server;
static Socket socket;
static PrintStream outStream;
static BufferedReader inStream;
static String aName;
static String address;
static int port;
public Main()
{
aFrame = new JFrame();
aFrame.setSize(500, 500);
aFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
aFrame.setTitle("Input your message");
aFrame.setLayout(new BorderLayout());
aFrame.setVisible(true);
aButton = new JButton();
aButton.setText("Press to send message");
aButton.setSize(200, 25);
aButton.addActionListener(this);
aFrame.add(aButton, BorderLayout.NORTH);
inputArea = new JTextField(50);
inputArea.setVisible(true);
inputArea.addActionListener(this);
aFrame.add(inputArea, BorderLayout.SOUTH);
output = new JTextArea(25, 50);
output.setVisible(true);
aFrame.add(output, BorderLayout.CENTER);
}
public static void main(String[] args)
{
try
{
Main aMain = new Main();
aName = JOptionPane.showInputDialog(null, "Enter Your Name : ",
"Input Box", 1);
address = JOptionPane.showInputDialog(null, "Enter IP or address : ",
"Input Box", 1);
String aPort = JOptionPane.showInputDialog(null, "Enter port to connect through : ",
"Input Box", 1);
port = Integer.parseInt(aPort);
socket = new Socket(address, port);
inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outStream = new PrintStream(socket.getOutputStream());
String line;
while (!(line = inStream.readLine()).equals("exit") )
{
Main.output.append (line + '\n');
}
socket.close();
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
public void actionPerformed(ActionEvent e)
{
String aString = Main.inputArea.getText();
if((e.getSource() == aButton) || (e.getSource() == inputArea))
{
if((aString.equals("exit")) || (aString.equals(" exit")))
{
try
{
System.exit(1);
socket.close();
}
catch(Exception anException)
{
}
}
else
{
Main.output.append (aName + " says " + aString + '\n');
Main.inputArea.setText(" ");
Main.outStream.println(aName + " says " + aString);
}
}
}
}
ps. anyone answer this question as i ve been searching all day for an answer, how can i have text already in the inputdialog box which is a kind of default??
Kind Regards
Brendan