Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class Test {
- String address = "127.0.0.1";
- int port = 15561;
- String msg = "Hello There";
- private Test() {
- ExecutorService exec = Executors.newCachedThreadPool();
- exec.execute(new Runnable() {
- @Override
- public void run() {
- try {
- new Socket(address, port).getOutputStream().write(msg.getBytes());
- } catch (UnknownHostException ex) {
- System.out.println("[Sender] Error: " + ex);
- } catch (IOException ex) {
- System.out.println("[Sender] Error: " + ex);
- }
- }
- });
- exec.execute(new Runnable() {
- @Override
- public void run() {
- try {
- byte[] buf = new byte[64 * 1024];
- new ServerSocket(port).accept().getInputStream().read(buf);
- System.out.println(new String(buf));
- } catch (IOException ex) {
- System.out.println("[Reciever] Error: " + ex);
- }
- }
- });
- exec.shutdown();
- }
- public static void main(String[] args) {
- new Test();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment