Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. /*
  2. import javax.xml.crypto.Data;
  3. import java.io.*;
  4. import java.util.*;
  5. import java.net.*;
  6.  
  7. class TCPServer
  8. {
  9.     private ServerSocket serverSocket;
  10.    
  11.     public TCPServer() throws IOException {
  12.         serverSocket=new ServerSocket(9876);
  13.     }
  14.    
  15.     public InetAddress getAdress(){return serverSocket.getInetAddress();}
  16.     public int getPort(){return serverSocket.getLocalPort();}
  17.    
  18.     public void listen() throws IOException {
  19.         String data=null;
  20.         Socket client=serverSocket.accept();
  21.         String clientAdress=client.getInetAddress().getHostAddress();
  22.         int localPort=client.getLocalPort();
  23.  
  24.         System.out.println("Established connection with "+clientAdress);
  25.         DataInputStream inputStream=null;
  26.         try{
  27.             inputStream=new DataInputStream(new BufferedInputStream(
  28.                     client.getInputStream()
  29.             ));
  30.             System.out.println(inputStream.readDouble());
  31.             System.out.println(inputStream.readLong());
  32.             System.out.println(inputStream.readBoolean());
  33.             System.out.println(inputStream.readUTF());
  34.         }
  35.         finally {
  36.             if(inputStream!=null)
  37.                 inputStream.close();
  38.         }
  39.  
  40.     }
  41. }
  42.  
  43. class TCPClient
  44. {
  45.     private Socket server;
  46.     private DataOutputStream out;
  47.     TCPClient(InetAddress serverAdress, int serverPort) throws IOException {
  48.         server=new Socket(serverAdress,serverPort);
  49.         out=null;
  50.     }
  51.  
  52.     public void start() throws IOException {
  53.         try{
  54.             out=new DataOutputStream(server.getOutputStream());
  55.             out.writeDouble(1.25);
  56.             out.writeLong(123584124);
  57.             out.writeBoolean(true);
  58.             out.writeUTF("UTF String");
  59.  
  60.             out.flush();
  61.         } catch (IOException e) {
  62.             e.printStackTrace();
  63.         }
  64.         finally {
  65.             if(out!=null)
  66.                 out.close();
  67.         }
  68.     }
  69.  
  70. }
  71.  
  72. public class Connection{
  73.     public static void main(String[] args) throws IOException {
  74.         TCPServer tcpServer=new TCPServer();
  75.         TCPClient tcpClient=new TCPClient(tcpServer.getAdress(),tcpServer.getPort());
  76.         tcpServer.listen();
  77.         tcpClient.start();
  78.     }
  79. }
  80.  
  81. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement