Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. /**
  2. * Class: Java Programming II
  3. * Term: Spring 2018
  4. * Instructor: Ron Enz
  5. * Description: Lab 10
  6. * @author Kazu Yamazaki
  7. * 1. That I have completed the programming assignment independently.
  8. * 2. I have not copied the code from a student or any source.
  9. * 3. I have not given my code to any student.
  10. */
  11.  
  12. import java.io.*;
  13. import java.net.*;
  14. import java.util.Scanner;
  15.  
  16.  
  17. public class ServerString{
  18.  
  19. Socket s1;
  20.  
  21. public static void main(String[] args){
  22. ServerString server = new ServerString();
  23. }
  24.  
  25. public ServerString(){
  26.  
  27. try{
  28. //Create Connection
  29. ServerSocket ss = new ServerSocket(1234);
  30.  
  31. System.out.println("Ready for Connection...");
  32. s1 = ss.accept();
  33. System.out.println("Connected");
  34.  
  35. //getting messages
  36. receiveMsg();
  37.  
  38. //sending messages
  39. PrintStream ps = new PrintStream(s1.getOutputStream());
  40. BufferedReader brout = new BufferedReader(new InputStreamReader(System.in));
  41.  
  42.  
  43. while(true)
  44. {
  45. String msgout = brout.readLine();
  46. ps.println(msgout);
  47. ps.flush();
  48.  
  49. }
  50. }
  51.  
  52. catch(IOException e){}
  53.  
  54.  
  55. }
  56.  
  57.  
  58. public void receiveMsg() {
  59. new Thread(new Runnable() {
  60. @Override
  61. public void run() {
  62. try {
  63. BufferedReader br = new BufferedReader(new InputStreamReader(s1.getInputStream()));
  64.  
  65. while(!s1.isClosed()) {
  66. String in = br.readLine();
  67. System.out.println("Client: "+in);
  68. }
  69. } catch (IOException e) {
  70. // TODO Auto-generated catch block
  71. e.printStackTrace();
  72. }
  73.  
  74. }
  75. }).start();
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement