Guest User

Untitled

a guest
Apr 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. class Server {
  5.  
  6. public static ServerSocket servSock;
  7. public static Socket s = new Socket();
  8. public static BufferedReader input;
  9. public static PrintWriter printwriter ;
  10.  
  11.  
  12. public static void main(String []args)
  13. {
  14. try
  15. {
  16. awaitConnection();
  17. setupReader();
  18. }
  19. catch(Exception e)
  20. {
  21. System.out.println(e.getMessage());
  22. }
  23. }
  24.  
  25. private static void awaitConnection() throws IOException {
  26.  
  27. ServerSocket servSock = new ServerSocket(8080);
  28. System.out.println("Awaiting connection...");
  29. s = servSock.accept();
  30. InetAddress obj = s.getInetAddress();
  31. System.out.println("Connection established with " + obj);
  32. }
  33.  
  34. private static void setupReader() throws IOException {
  35. while(true) {
  36. input = new BufferedReader(new InputStreamReader(s.getInputStream()));
  37. String msg = input.readLine();
  38. if(msg.contains("bye")) {
  39. break;
  40. }
  41. else {
  42. printwriter.println("this is a message from the server");
  43. }
  44.  
  45. printwriter.flush();
  46. }
  47. s.close();
  48.  
  49. }
  50. }
  51.  
  52. import java.net.*;
  53. import java.io.*;
  54. import java.util.*;
  55.  
  56. class Client
  57. {
  58. static Socket clientSocket = null;
  59. static InetAddress obj = null;
  60. static PrintWriter output;
  61. static String message;
  62. static BufferedReader br;
  63.  
  64. public static void main(String []args)
  65. {
  66. try
  67. {
  68. obj = InetAddress.getByName(args[0]);
  69. connect();
  70. send();
  71.  
  72.  
  73. }
  74. catch(Exception e)
  75. {
  76. System.out.println(e.getMessage());
  77. }
  78. }
  79. private static void connect() throws IOException {
  80. System.out.println("Trying to connect...");
  81. clientSocket = new Socket(obj, 8080);
  82. System.out.println("Your are connected to the server! nEnter Message:");
  83. }
  84. private static void send() throws IOException {
  85.  
  86. while(true) {
  87. System.out.println(br.readLine());
  88. output = new PrintWriter(clientSocket.getOutputStream(), true);
  89. message = new Scanner(System.in).nextLine();
  90. output.println(message);
  91. if(message.contains("bye"))
  92. {
  93. break;
  94. }
  95. }
  96. output.close();
  97. clientSocket.close();
  98.  
  99. }
  100. }
Add Comment
Please, Sign In to add comment