Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Java Chat ...................


  • Please log in to reply

#1
nimzyjava

nimzyjava

    New Member

  • Member
  • Pip
  • 2 posts
/*Author : Nimesh Oza
*Company : Synergetics India
*Code Name : Java Chat
*Author : Nimesh Oza
*Company : Synergetics India
*Code Name : Java Chat
* This Program allows to chat on a Interanet...... all one has to do
* is Enter the ipAddress of machine one wants to connect
* to and Click Connect Button and use the TextField b4 Send Button to send messages
*/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class NewChat extends Frame {

private static final long serialVersionUID = 1L;

TextField senderTextField = new TextField(40);

TextField connectTextField = new TextField(40);

TextArea senderTextArea = new TextArea(40, 40);

TextArea connectTextArea = new TextArea(40, 40);

Button sendButton = new Button("Send");

Button connectButton = new Button("Connect");

Panel upperPanel = new Panel();

Panel lowerPanel = new Panel();

Panel upperSouthPanel = new Panel();

Panel lowerSouthPanel = new Panel();

Socket clientSocket;

ServerSocket connectSocket;

DataInputStream dis;

DataOutputStream dos;

String str;

public void addGUI() {
setLayout(new GridLayout(2, 1));
upperPanel.setLayout(new BorderLayout());
upperPanel.add("Center", connectTextArea);
upperPanel.add("South", upperSouthPanel);
upperSouthPanel.add(connectTextField);
upperSouthPanel.add(connectButton);
add(upperPanel);

lowerPanel.setLayout(new BorderLayout());
lowerPanel.add("Center", senderTextArea);
lowerPanel.add("South", lowerSouthPanel);
lowerSouthPanel.add(senderTextField);
lowerSouthPanel.add(sendButton);
add(lowerPanel);
}

public void addListeners() {
addWindowListener(new CloseWindowListener());
connectButton.addActionListener(new ConnectButtonListener());
sendButton.addActionListener(new SendButtonListener());
}

public NewChat() {
addGUI();
addListeners();
setSize(400, 400);
setVisible(true);
startServer();
}

class ServerThread extends Thread {
public void run() {
try {
System.out.println("Inside Serverthread run");
connectSocket = new ServerSocket(10000);
clientSocket = connectSocket.accept();
startChat();
} catch (IOException e) {
e.printStackTrace();
}


}
}

public void startChat() {
try {
System.out.println("Inside startChat");
dis = new DataInputStream(clientSocket.getInputStream());

dos = new DataOutputStream(clientSocket.getOutputStream());

ReaderThread reader = new ReaderThread();

reader.start();
} catch (IOException e) {
e.printStackTrace();
}

}

class ConnectButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
try {
System.out.println("Inside Try in ConnectButtonListener");
clientSocket = new Socket(connectTextField.getText(), 10000);
startChat();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void startServer() {
System.out.println("Inside StartServer");
ServerThread serverThread = new ServerThread();
serverThread.start();
}

class SendButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
try {
System.out.println("Inside SendButtonListener");
dos.writeBytes(senderTextField.getText() + "\n");
senderTextArea.append(senderTextField.getText() + "\n");
senderTextField.setText("");
} catch (IOException e) {
e.printStackTrace();
}
}
}

class ReaderThread extends Thread {
public void run() {
while (true) {
try {
System.out.println("Inside ReaderThread");
str = dis.readLine();
connectTextArea.appendText(str + "\n");
} catch (IOException e) {
Thread.currentThread().stop();
e.printStackTrace();
}
}
}
}

class CloseWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}

public static void main(String[] args) {
NewChat nc = new NewChat();
}
}
  • 0

Advertisements







Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP