class HelloServerUI extends JFrame
{
JTextField msgClientField, msgServerField;
JButton sendButton;
JLabel inputLabel, outputLabel;
PrintWriter out;
Socket clientSocket = null;
String inputLine, outputLine;
ServerSocket serverSocket = null;
Container c=getContentPane();
HelloServerUI()
{
super("Chat program - server side");
c.setLayout(new FlowLayout());
//JOptionPane.showMessageDialog(null, "Ytasdfs");
inputLabel = new JLabel("Client says :");
msgClientField = new JTextField(20);
msgClientField.setEditable(false);
outputLabel= new JLabel("Ur message ");
msgServerField = new JTextField(20);
sendButton = new JButton("Send message");
sendButton.addActionListener(new sendButtonListener());
c.add(inputLabel);
c.add(msgClientField);
c.add(outputLabel);
c.add(msgServerField);
c.add(sendButton);
}
public void initComponent()
{
try
{
serverSocket = new ServerSocket(01);
}
catch (IOException e)
{
System.out.println("I cant hear you on port: 4444, Speak
loudly");
}
try
{
while(true)
{
System.out.println("Waiting for a Client");
clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader( new
InputStreamReader( clientSocket.getInputStream()));
/////////////////////////////////
//System.out.println("MEs frm client " + (in.readLine()).toString()
);
msgClientField.setText(in.readLine().toString());
}
}
catch (IOException e)
{
System.out.println("Accept failed: 4444");
}
//sending message to client ->outputstream
//recieving message from client --> input stream
try
{
PrintWriter out = new PrintWriter( clientSocket.getOutputStream(),
true);
//BufferedReader in = new BufferedReader( new InputStreamReader(
clientSocket.getInputStream()));
/////////////////////////////////
//System.out.println("MEs frm client " + (in.readLine()));
}
catch(Exception ex)
{
System.out.println("Some error, go out and play football!");
}
}
private class sendButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s = msgServerField.getText();
out.println(s);
}
}
}
public class HelloServer
{
public static void main(String args[])
{
HelloServerUI h1 = new HelloServerUI();
h1.setSize(370,275);
h1.setVisible(true);
h1.setVisible(true);
h1.initComponent();
h1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}