Advertisement
murkata86

Server

Mar 16th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.IOException;
  5. import java.io.OutputStreamWriter;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8.  
  9. /**
  10.  * Created by Ivan on 3/16/2016.
  11.  */
  12. public class CommunicationServer {
  13.     private static int port = 20222;
  14.     private static ServerSocket listenSock = null;
  15.     private Socket sock = null;
  16.     private static CommunicationServer instance = null;
  17.  
  18.     protected CommunicationServer() {
  19.         System.out.println("Communication has started");
  20.     }
  21.  
  22.     public static CommunicationServer getInstance() {
  23.         if (instance == null) {
  24.             instance = new CommunicationServer();
  25.             try{
  26.                 listenSock = new ServerSocket(port);
  27.             } catch (IOException ex){
  28.                 ex.printStackTrace();
  29.             }
  30.         }
  31.  
  32.         return instance;
  33.     }
  34.  
  35.     public void StartCommunicationg(String message) {
  36.         try {
  37.  
  38.  
  39.  
  40.             //while(true) {
  41.             this.sock = listenSock.accept();
  42.  
  43.             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
  44.  
  45.             writer.write(message + "/n");
  46.             writer.flush();
  47.             writer.close();
  48.             sock.close();
  49.             //}
  50.         } catch (IOException ex) {
  51.             ex.printStackTrace();
  52.         }
  53.  
  54.  
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement