Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.util.Arrays;
- import java.util.List;
- public class PictureChatClient
- {
- public static void main(String[] args)
- {
- // System.out.println("***********************************************");
- System.out.println("ECE 309 Lab6 PictureChatClient\nSpring 2017\nAuthor: Nivesh Varma\n");
- // System.out.println("***********************************************");
- if (args.length != 3)
- {
- System.out.println("Restart. Provide your chat name as the first parameter.");
- System.out.println("Provide your password as the second command line parameter.");
- System.out.println("Provide the chat server network address as the third parameter.");
- return;
- }
- String chatName = args[0];
- String password = args[1];
- String serverAddress = args[2];
- if (chatName.contains(" ") || password.contains(" ") || serverAddress.contains(" "))
- {
- System.out.println("Command line parameters cannot contain blanks.");
- return;
- }
- // ************************
- // Connect to Chat Server *
- // ************************
- String newLine = System.lineSeparator();
- int serverPort = 6666;
- System.out.println("Connecting to the chat room server at " + serverAddress
- + " on port " + serverPort);
- Socket s = null;
- try {
- s = new Socket(serverAddress, serverPort);
- System.out.println("Connected to the server.");
- }
- catch (Exception e)
- {
- System.out.println("The Chat Room Server is not responding."
- + newLine + "Either the entered network address is incorrect,"
- + newLine + "or the server computer is not up,"
- + newLine + "or the ChatServer program is not started,"
- + newLine + "or the server is not accepting connections on port " + serverPort);
- return;
- }
- // **********************
- // "JOIN" the chat room *
- // **********************
- ObjectOutputStream oos = null;
- ObjectInputStream ois = null;
- String serverReply = null;
- try {
- System.out.println("Sending join request for " + chatName);
- oos = new ObjectOutputStream(s.getOutputStream());
- oos.writeObject(chatName + " " + password); // 1st message
- ois = new ObjectInputStream (s.getInputStream());
- System.out.println("Waiting for server reply to join request.");
- serverReply = (String) ois.readObject();
- }
- catch(Exception e) // problem sending to/receiving from the server
- {
- System.out.println("The remote computer has rejected our join protocol."
- + newLine + "So the remote application we have connected to is likely not the Chat Room Server!"
- + newLine + "Correct the network address and restart ChatClient.");
- return; // terminate.
- }
- // Show the serverReply to the user.
- System.out.println(serverReply);
- // Check it ourself to see if we should stop
- if (!serverReply.startsWith("Welcome")) return; // stop if error msg.
- // Load the ChatClientGUI
- System.out.println("Loading the ChatClientGUI program.");
- PictureChatClientGUI pccg = new PictureChatClientGUI(chatName, oos);
- // RECEIVE CHAT LOOP
- System.out.println("The main thread is entering the receive loop in ChatClient.");
- try { //A receive error exits the loop and terminates the client.
- while(true)// *Capture* our main thread in a loop
- {
- Object messageFromServer = ois.readObject();
- // System.out.println("Received from server: " + messageFromServer);
- if (messageFromServer instanceof String)
- {
- String incomingMessage = (String) messageFromServer;
- pccg.showIncomingMessage(incomingMessage);
- }
- else if (messageFromServer instanceof String[])//array of Strings
- {
- String[] chatNames = (String[]) messageFromServer;
- List<String> clients = Arrays.asList(chatNames);
- if (clients.contains(chatName.toUpperCase()))
- {
- pccg.showWhosIn(chatNames);
- System.out.println("Currently in the chat room:");
- }
- else
- {
- pccg.showWhosNotIn(chatNames);
- System.out.println("Currently NOT in the chat room:");
- }
- System.out.println(clients);
- }
- else if (messageFromServer instanceof ImageIcon) {
- ImageIcon receivedPicture = (ImageIcon) messageFromServer;
- pccg.showIncomingPicture(receivedPicture);
- }
- else
- {
- System.out.println("An unanticipated type of object has been received!"
- + messageFromServer);
- }
- }// bottom of while(true)
- }// bottom of try
- catch(Exception e)// we're done if connection fails!
- { //(so catch is OUTSIDE the loop)
- String errMsg = "ERROR: ChatServer Connection Failure" + e;
- pccg.showIncomingMessage(errMsg);
- System.out.println(errMsg);
- System.out.println("ChatClient must be restarted to reestablish connection to server.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement