Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.nio.charset.Charset;
  7.  
  8. public class Main
  9. {
  10.     public static void main(String[] args) throws IOException
  11.     {
  12.         //Initialize connection
  13.         ServerSocket serverSocket = new ServerSocket(1337);
  14.         System.out.println("waiting for connection....");
  15.         Socket clientSocket = serverSocket.accept();
  16.         System.out.println("Found client!");
  17.         //Setting up data streams
  18.         DataInputStream in = new DataInputStream(clientSocket.getInputStream());
  19.         DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
  20.         System.out.println("Connection initialized... Waiting for data");
  21.         while(true)
  22.         {
  23.             //Read bytes from client
  24.             byte[] bytes = new byte[1024];
  25.             in.read(bytes);
  26.             System.out.println(new String(bytes, Charset.forName("UTF-8")).trim());
  27.             //Send text back
  28.             out.write(new String("biep").getBytes("UTF-8"));
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement