Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. package ip2019;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.net.InetAddress;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. import java.io.DataOutputStream;
  9.  
  10. public class Server {
  11.    
  12.     private ServerSocket server;
  13.    
  14.     public Server(String ipAddress) throws Exception {
  15.         int port = 1234;
  16.         if (ipAddress != null && !ipAddress.isEmpty())
  17.           this.server = new ServerSocket(port, 1, InetAddress.getByName(ipAddress));
  18.         else
  19.           this.server = new ServerSocket(port, 1, InetAddress.getLocalHost());
  20.     }
  21.    
  22.     void listen() throws Exception {
  23.         String data = null;
  24.         Socket client = this.server.accept();
  25.         String clientAddress = client.getInetAddress().getHostAddress();
  26.         System.out.println("\r\nNew connection from " + clientAddress);
  27.        
  28.         BufferedReader in = new BufferedReader(
  29.                 new InputStreamReader(client.getInputStream()));      
  30.         DataOutputStream dos = new DataOutputStream(
  31.                 client.getOutputStream());
  32.        
  33.         while ( (data = in.readLine()) != null ) {
  34.             System.out.println("\r\nMessage from " + clientAddress + ": " + data);
  35.             dos.writeBytes(data);
  36.             System.out.println("Message sent to the client is "+ data);
  37.         }
  38.     }
  39.    
  40.     public InetAddress getSocketAddress() {
  41.         return this.server.getInetAddress();
  42.     }
  43.    
  44.     public int getPort() {
  45.         return this.server.getLocalPort();
  46.     }
  47.    
  48.  
  49. }
  50.  
  51. ------------------------------------------------------------------
  52.  
  53. package ip2019;
  54.  
  55. public class Main {
  56.     public static void main(String[] args) throws Exception {
  57.        
  58.         String adress = "127.0.0.1";
  59.         String port = "1234";
  60.        
  61.         Server server = new Server(adress);
  62.        
  63.         System.out.println("\r\nRunning Server: " +
  64.                 "Host=" + server.getSocketAddress().getHostAddress() +
  65.                 " Port=" + server.getPort());
  66.        
  67.         server.listen();
  68.     }
  69. }
  70.  
  71. ------------------------------------------------------------------
  72.  
  73. package ip2019_client;
  74.  
  75. import java.io.BufferedReader;
  76. import java.io.IOException;
  77. import java.io.InputStreamReader;
  78. import java.io.PrintWriter;
  79. import java.net.InetAddress;
  80. import java.net.Socket;
  81. import java.util.Scanner;
  82.  
  83. public class Client {
  84.     protected Socket socket;
  85.     private Scanner scanner;
  86.     private BufferedReader is = null;
  87.    
  88.     protected Client(InetAddress serverAddress, int serverPort) throws Exception {
  89.         this.socket = new Socket(serverAddress, serverPort);
  90.         this.is = new BufferedReader(new InputStreamReader(
  91.                 this.socket.getInputStream()));
  92.         this.scanner = new Scanner(System.in);
  93.     }
  94.    
  95.     protected void start() throws IOException, InterruptedException {
  96.         String input;
  97.        
  98.         while (true) {
  99.             input = scanner.nextLine();
  100.             PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true);
  101.             out.println(input);
  102.             out.flush();
  103.            
  104.             Thread.sleep(1000);
  105.             System.out.println("waiting for server return");
  106.  
  107.             String responseLine = is.readLine();
  108.             System.out.println("Server returns: " + responseLine);
  109.         }
  110.     }
  111. }
  112.  
  113. ------------------------------------------------------------------
  114.  
  115. package ip2019_client;
  116.  
  117. import java.net.InetAddress;
  118.  
  119. public class Main_client {
  120.     public static void main(String[] args) throws Exception {
  121.         String adress = "127.0.0.1";
  122.         String port = "1234";
  123.        
  124.         Client client = new Client(
  125.                 InetAddress.getByName(adress),
  126.                 Integer.parseInt(port));
  127.    
  128.         System.out.println("\r\nConnected to Server: " + client.socket.getInetAddress());
  129.         client.start();
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement