Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- /**
- * Created by Ivan on 3/16/2016.
- */
- public class CommunicationServer {
- private static int port = 20222;
- private static ServerSocket listenSock = null;
- private Socket sock = null;
- private static CommunicationServer instance = null;
- protected CommunicationServer() {
- System.out.println("Communication has started");
- }
- public static CommunicationServer getInstance() {
- if (instance == null) {
- instance = new CommunicationServer();
- try{
- listenSock = new ServerSocket(port);
- } catch (IOException ex){
- ex.printStackTrace();
- }
- }
- return instance;
- }
- public void StartCommunicationg(String message) {
- try {
- //while(true) {
- this.sock = listenSock.accept();
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
- writer.write(message + "/n");
- writer.flush();
- writer.close();
- sock.close();
- //}
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement