Advertisement
vietanhlehuu

THÊM PHƯƠNG THỨC NHẬN VÀO TCP SERVER

Oct 25th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package Client;
  2. import java.io.DataInputStream;
  3. import java.io.IOException;
  4. import java.io.PrintStream;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7.  
  8. public class TCPserver {
  9. public final int port = 5000;
  10. private String datas ;
  11. private ServerSocket theServer;
  12. private Socket theConnection;
  13. private PrintStream p;
  14.  
  15. //CÓ DÙNG THÊM CÁI NÀY
  16.  
  17. DataInputStream dataInputStream = null;
  18.  
  19. ////////////////////////////////
  20.  
  21.  
  22. public TCPserver(String data){
  23. this.datas=data;
  24.  
  25. }
  26. public void wakeUp() {
  27. System.out.println("Server started...");
  28. try {
  29. theServer = new ServerSocket(port);
  30. theConnection = theServer.accept();
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. /*
  38. * VÀ NHẬN MESSAGE Ở ĐÂY
  39. *
  40. *
  41. * */
  42. dataInputStream = new DataInputStream(
  43. theConnection.getInputStream());
  44. String messageFromClient = "";
  45. //If no message sent from client, this code will block the program
  46. messageFromClient = dataInputStream.readUTF();
  47. System.out.println(messageFromClient);
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. /*
  55. *
  56. *
  57. * */
  58. p = new PrintStream(theConnection.getOutputStream());
  59. //p.println(datas);
  60. //System.out.println(datas);
  61. datas = "Data 2";
  62. p.println(datas);
  63. System.out.println(datas);
  64. theConnection.close();
  65. theServer.close();
  66. }
  67. catch (IOException e) {
  68. System.err.println(e);
  69. }
  70. }
  71. public void main(String arg[]) throws Exception {
  72.  
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement