Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.io.ObjectInputStream;
  3. import java.io.ObjectOutputStream;
  4. import java.net.Socket;
  5. import java.util.Arrays;
  6. import java.util.List;
  7.  
  8.  
  9. public class PictureChatClient
  10. {
  11. public static void main(String[] args)
  12. {
  13. // System.out.println("***********************************************");
  14. System.out.println("ECE 309 Lab6 PictureChatClient\nSpring 2017\nAuthor: Nivesh Varma\n");
  15. // System.out.println("***********************************************");
  16.  
  17. if (args.length != 3)
  18. {
  19. System.out.println("Restart. Provide your chat name as the first parameter.");
  20. System.out.println("Provide your password as the second command line parameter.");
  21. System.out.println("Provide the chat server network address as the third parameter.");
  22. return;
  23. }
  24. String chatName = args[0];
  25. String password = args[1];
  26. String serverAddress = args[2];
  27. if (chatName.contains(" ") || password.contains(" ") || serverAddress.contains(" "))
  28. {
  29. System.out.println("Command line parameters cannot contain blanks.");
  30. return;
  31. }
  32.  
  33. // ************************
  34. // Connect to Chat Server *
  35. // ************************
  36. String newLine = System.lineSeparator();
  37. int serverPort = 6666;
  38. System.out.println("Connecting to the chat room server at " + serverAddress
  39. + " on port " + serverPort);
  40. Socket s = null;
  41. try {
  42. s = new Socket(serverAddress, serverPort);
  43. System.out.println("Connected to the server.");
  44. }
  45. catch (Exception e)
  46. {
  47. System.out.println("The Chat Room Server is not responding."
  48. + newLine + "Either the entered network address is incorrect,"
  49. + newLine + "or the server computer is not up,"
  50. + newLine + "or the ChatServer program is not started,"
  51. + newLine + "or the server is not accepting connections on port " + serverPort);
  52. return;
  53. }
  54.  
  55. // **********************
  56. // "JOIN" the chat room *
  57. // **********************
  58. ObjectOutputStream oos = null;
  59. ObjectInputStream ois = null;
  60. String serverReply = null;
  61. try {
  62. System.out.println("Sending join request for " + chatName);
  63. oos = new ObjectOutputStream(s.getOutputStream());
  64. oos.writeObject(chatName + " " + password); // 1st message
  65. ois = new ObjectInputStream (s.getInputStream());
  66. System.out.println("Waiting for server reply to join request.");
  67. serverReply = (String) ois.readObject();
  68. }
  69. catch(Exception e) // problem sending to/receiving from the server
  70. {
  71. System.out.println("The remote computer has rejected our join protocol."
  72. + newLine + "So the remote application we have connected to is likely not the Chat Room Server!"
  73. + newLine + "Correct the network address and restart ChatClient.");
  74. return; // terminate.
  75. }
  76.  
  77. // Show the serverReply to the user.
  78. System.out.println(serverReply);
  79. // Check it ourself to see if we should stop
  80. if (!serverReply.startsWith("Welcome")) return; // stop if error msg.
  81.  
  82. // Load the ChatClientGUI
  83. System.out.println("Loading the ChatClientGUI program.");
  84. PictureChatClientGUI pccg = new PictureChatClientGUI(chatName, oos);
  85.  
  86. // RECEIVE CHAT LOOP
  87. System.out.println("The main thread is entering the receive loop in ChatClient.");
  88. try { //A receive error exits the loop and terminates the client.
  89. while(true)// *Capture* our main thread in a loop
  90. {
  91. Object messageFromServer = ois.readObject();
  92. // System.out.println("Received from server: " + messageFromServer);
  93. if (messageFromServer instanceof String)
  94. {
  95. String incomingMessage = (String) messageFromServer;
  96. pccg.showIncomingMessage(incomingMessage);
  97. }
  98. else if (messageFromServer instanceof String[])//array of Strings
  99. {
  100. String[] chatNames = (String[]) messageFromServer;
  101. List<String> clients = Arrays.asList(chatNames);
  102. if (clients.contains(chatName.toUpperCase()))
  103. {
  104. pccg.showWhosIn(chatNames);
  105. System.out.println("Currently in the chat room:");
  106. }
  107. else
  108. {
  109. pccg.showWhosNotIn(chatNames);
  110. System.out.println("Currently NOT in the chat room:");
  111. }
  112. System.out.println(clients);
  113. }
  114.  
  115. else if (messageFromServer instanceof ImageIcon) {
  116. ImageIcon receivedPicture = (ImageIcon) messageFromServer;
  117. pccg.showIncomingPicture(receivedPicture);
  118. }
  119.  
  120. else
  121. {
  122. System.out.println("An unanticipated type of object has been received!"
  123. + messageFromServer);
  124. }
  125. }// bottom of while(true)
  126. }// bottom of try
  127. catch(Exception e)// we're done if connection fails!
  128. { //(so catch is OUTSIDE the loop)
  129. String errMsg = "ERROR: ChatServer Connection Failure" + e;
  130. pccg.showIncomingMessage(errMsg);
  131. System.out.println(errMsg);
  132. System.out.println("ChatClient must be restarted to reestablish connection to server.");
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement