Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class CLIENT extends JFrame {
- //JAVA NETWORK CHAT BY BEN BOLLINGER
- private JTextField userText;
- private JTextArea chatWindow;
- private ObjectOutputStream output;
- private ObjectInputStream input;
- private String message = "";
- private String serverIP;
- private Socket connection;
- //CONSTRUCTOR
- public CLIENT(String host){
- super("CLIENT TEST");
- serverIP = host;
- userText = new JTextField();
- userText.setEditable(false);
- userText.addActionListener(
- new ActionListener(){
- public void actionPerformed(ActionEvent event){
- sendMessage(event.getActionCommand());
- userText.setText("");
- }
- }
- );
- add(userText, BorderLayout.NORTH);
- chatWindow = new JTextArea();
- add(new JScrollPane(chatWindow), BorderLayout.CENTER);
- setSize(300,150);
- setVisible(true);
- }
- //RUN CONNECTION
- public void startRunning(){
- try{
- connectToServer();
- setupStreams();
- whileChatting();
- }catch(EOFException eofException){
- showMessage("\n CLIENT TERMINATED CONNECTION");
- }catch(IOException ioException){
- ioException.printStackTrace();
- }finally{
- closeSocket();
- }
- }
- //CONNECT TO SERVER
- private void connectToServer() throws IOException{
- showMessage("ATTEMPTING CONNECTION... \n");
- connection = new Socket(InetAddress.getByName(serverIP), 6789);
- showMessage(" CONNECTED TO " + connection.getInetAddress().getHostName());
- }
- //SETUP STREAMS
- private void setupStreams() throws IOException{
- output = new ObjectOutputStream(connection.getOutputStream());
- output.flush();
- input = new ObjectInputStream(connection.getInputStream());
- showMessage("\n STREAMS NOW READY \n");
- }
- //WHILE CHATTING
- private void whileChatting() throws IOException{
- ableToType(true);
- do{
- try{
- message = (String) input.readObject();
- showMessage("\n" + message);
- }catch(ClassNotFoundException classNotFoundException){
- showMessage("\n ERROR: UNKOWN OBJECT TYPE");
- }
- }while(!message.equals("SERVER - END"));
- }
- //CLOSE STREAMS AND SOCKETS
- private void closeSocket(){
- showMessage("\n CLOSING STREAMS AND SOCKETS...");
- ableToType(false);
- try{
- output.close();
- input.close();
- connection.close();
- }catch(IOException ioException){
- ioException.printStackTrace();
- }
- }
- //SEND MESSAGES
- private void sendMessage(String message){
- try{
- output.writeObject("CLIENT - " + message);
- output.flush();
- showMessage("\nCLIENT - " + message);
- }catch(IOException ioException){
- chatWindow.append("\n ERROR: UNKNOWN SENDING ERROR");
- }
- }
- //UPDATE CHAT WINDOW
- private void showMessage(final String m){
- SwingUtilities.invokeLater(
- new Runnable(){
- public void run(){
- chatWindow.append(m);
- chatWindow.setEditable(false);
- }
- }
- );
- }
- //ALLOW USER TO TYPE
- private void ableToType(final boolean tof){
- SwingUtilities.invokeLater(
- new Runnable(){
- public void run(){
- userText.setEditable(tof);
- }
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement