Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.nio.charset.Charset;
- public class Main
- {
- public static void main(String[] args) throws IOException
- {
- //Initialize connection
- ServerSocket serverSocket = new ServerSocket(1337);
- System.out.println("waiting for connection....");
- Socket clientSocket = serverSocket.accept();
- System.out.println("Found client!");
- //Setting up data streams
- DataInputStream in = new DataInputStream(clientSocket.getInputStream());
- DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
- System.out.println("Connection initialized... Waiting for data");
- while(true)
- {
- //Read bytes from client
- byte[] bytes = new byte[1024];
- in.read(bytes);
- System.out.println(new String(bytes, Charset.forName("UTF-8")).trim());
- //Send text back
- out.write(new String("biep").getBytes("UTF-8"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement