Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.52 KB | None | 0 0
  1. BufferedReader BufReader1 = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
  2. String MessageFromClient = BufReader1.readLine();
  3.  
  4. import java.io.*;
  5. import java.net.*;
  6. import java.util.*;
  7. import static java.nio.charset.StandardCharsets.*;
  8. public class ChatServer
  9. {
  10. ChatServer chatserver = new ChatServer();
  11. private static Socket socket;
  12.  
  13. public static void main(String args[])
  14. {
  15. Thread ChatServer1 = new Thread ()
  16. {
  17. public void run ()
  18. {
  19. System.out.println("Server thread is now running");
  20. try
  21. {
  22. int port_number1 = 0;
  23. int numberOfClients = 0;
  24. boolean KeepRunning = true;
  25. if(args.length>0)
  26. {
  27. port_number1 = Integer.valueOf(args[0]);
  28. }
  29. System.out.println("Waiting for connections on port " + port_number1);
  30.  
  31. try
  32. {
  33. ServerSocket serverSocket = new ServerSocket(port_number1);
  34. socket = serverSocket.accept();
  35. }
  36. catch (IOException e)
  37. {
  38. e.printStackTrace();
  39. }
  40. System.out.println( "Listening for connections on port: " + ( port_number1 ) );
  41. while(KeepRunning)
  42. {
  43. //create a list of clients
  44. ArrayList<String> ListOfClients = new ArrayList<String>();
  45.  
  46. //connect to client
  47. // socket = serverSocket.accept();
  48.  
  49. //add new client to the list, is this the right way to add a new client? or should it be in a for loop or something?
  50. ListOfClients.add("new client");
  51. numberOfClients += 1;
  52.  
  53. System.out.println("A client has connected. Waiting for message...");
  54. ListOfClients.add("new client" + numberOfClients);
  55.  
  56. //reading encoded utf-8 message from client, decoding from utf-8 format
  57.  
  58. BufferedReader BufReader1 = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
  59. String MessageFromClient = BufReader1.readLine();
  60. // System.out.println("The message is currently encoded UTF-8");
  61. //byte[] bytes = MessageFromClientDecodedFromUTF8.getBytes("UTF-8");
  62. // String MessageFromClientDecoded = new String(bytes, "UTF-8");
  63.  
  64. System.out.println("Message received from client (decoded utf-8): "+ MessageFromClient);
  65.  
  66.  
  67. //relaying message to every other client besides the one it was from
  68. //
  69. // for (int i = 0; i < ListOfClients.size(); i++)
  70. // {
  71. //if(ListOfClients.get(i)!="new client")
  72. //{
  73. String newmessage = null;
  74. String returnMessage = newmessage;
  75. OutputStream os = socket.getOutputStream();
  76. OutputStreamWriter osw = new OutputStreamWriter(os);
  77. BufferedWriter bw = new BufferedWriter(osw);
  78.  
  79. if(returnMessage != null)
  80. {
  81. bw.write(returnMessage + "n");
  82. System.out.println("Message sent to all other clients: "+returnMessage);
  83.  
  84. }
  85. bw.flush();
  86. bw.close();
  87. // out.close();
  88. os.close();
  89. //}
  90. //}
  91.  
  92. }
  93. }
  94. catch (IOException e)
  95. {
  96. e.printStackTrace();
  97. }
  98. finally
  99. {
  100. try
  101. {
  102. if (socket != null)
  103. {
  104. socket.close ();
  105. }
  106.  
  107. }
  108. catch (IOException e)
  109. {
  110. e.printStackTrace();
  111. }
  112. }
  113.  
  114. }
  115. };
  116. ChatServer1.start();
  117. }
  118. }
  119.  
  120. import java.io.*;
  121. import java.net.*;
  122. import java.util.*;
  123. import static java.nio.charset.StandardCharsets.*;
  124. public class ChatClient
  125. {
  126.  
  127.  
  128. final int numberOfClients = 0;
  129. public static void main(String args[])
  130. {
  131. ChatClient chatclient = new ChatClient();
  132.  
  133. //If I wanted to create multiple clients, would this code go here? OR should the new thread creation be outside the while(true) loop?
  134.  
  135. String host = "localhost";
  136. int numberOfClients = 0;
  137. Thread ChatClient1 = new Thread ()
  138. {
  139. public void run()
  140. {
  141. boolean KeepRunning = true;
  142. while(KeepRunning == true && args.length > 0)
  143. {
  144.  
  145. try
  146. {
  147.  
  148. //Client begins, gets port number, listens, connects, prints out messages from other clients
  149. int port = 0;
  150. int port_1number1 = 0;
  151. int numberofmessages = 0;
  152.  
  153.  
  154. System.out.println("Try block begins..");
  155. System.out.println("Chat client is running");
  156. System.out.println("Port number is: " + args[0]);
  157. if(args.length>0)
  158. {
  159. port = Integer.valueOf(args[0]);
  160. }
  161. System.out.println("Listening for connections..");
  162. System.out.println( "Listening on port: " + args[0] );
  163. // System.out.println(messagessentbyotherclients);
  164. try
  165. {
  166. Socket clientSocket = new Socket("localhost", port);
  167. InetAddress inetlocalhost = InetAddress.getByName("localhost");
  168. SocketAddress localhost = new InetSocketAddress(inetlocalhost, port);
  169. clientSocket.connect(localhost, port);
  170. System.out.println("Client has connected");
  171. //client creates new message from standard input
  172. OutputStream os = clientSocket.getOutputStream();
  173. OutputStreamWriter osw = new OutputStreamWriter(os);
  174. BufferedWriter bw = new BufferedWriter(osw);
  175.  
  176.  
  177. }
  178. catch (IOException e)
  179. {
  180. e.printStackTrace();
  181. }
  182. //creating message to send from standard input
  183. String newmessage = "";
  184. try
  185. {
  186. int k = 3;
  187. if(k==3)
  188. {
  189. // input the message from standard input encoded in UTF-8 string format
  190. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  191. String line = "";
  192. System.out.println( "Standard input (press enter then control D when finished): " );
  193. while( (line= input.readLine()) != null && k==3 )
  194. {
  195. newmessage += line + " ";
  196. k=4;
  197. }
  198. k=6;
  199. }
  200. }
  201. catch ( Exception e )
  202. {
  203. System.out.println( e.getMessage() );
  204. }
  205. //Sending the message to server
  206. String sendMessage = newmessage;
  207. try
  208. {
  209. Socket clientSocket = new Socket("localhost", port);
  210. InetAddress addr = InetAddress.getByName("localhost");
  211. SocketAddress sockaddr = new InetSocketAddress(addr, port);
  212. OutputStream os = clientSocket.getOutputStream();
  213. OutputStreamWriter osw = new OutputStreamWriter(os);
  214. BufferedWriter bw = new BufferedWriter(osw);
  215. bw.write(sendMessage + "n");
  216. bw.flush();
  217. bw.close();
  218. //out.close();
  219. os.close();
  220.  
  221. }
  222. catch (IOException e)
  223. {
  224. e.printStackTrace();
  225. }
  226. System.out.println("Message sent to server: "+sendMessage);
  227.  
  228. }
  229.  
  230. finally
  231. {
  232.  
  233. }
  234. KeepRunning = false;
  235. }
  236.  
  237. }
  238.  
  239. };
  240. ChatClient1.start();
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement