Advertisement
nicb

Untitled

Sep 16th, 2020
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. package it.alten.olympus.poc.referti.server.hl7Manager;
  2.  
  3. import ca.uhn.hl7v2.HL7Exception;
  4. import it.alten.olympus.poc.referti.server.hl7Manager.HL7Decoder;
  5. import lombok.extern.slf4j.Slf4j;
  6.  
  7. import java.io.*;
  8. import java.net.*;
  9.  
  10. @Slf4j
  11. public class TCPServer implements Runnable {
  12.     @Override
  13.     public void run() {
  14.         final int PORT = 1050;
  15.         ServerSocket serverSocket = null;
  16.         try {
  17.             serverSocket = new ServerSocket(PORT);
  18.         } catch (IOException e) {
  19.             e.printStackTrace();
  20.         }
  21.         System.out.println("TCP Server: started ");
  22.         System.out.println("Server Socket: " + serverSocket);
  23.         Socket clientSocket=null;
  24.         BufferedReader in=null;
  25.         PrintWriter out=null;
  26.         try {
  27.             // bloccante finchè non avviene una connessione
  28.             clientSocket = serverSocket.accept();
  29.             System.out.println("Connection accepted: "+ clientSocket);
  30.  
  31.             // creazione stream di input da clientSocket
  32.             InputStreamReader isr = new InputStreamReader(clientSocket.getInputStream());
  33.             in = new BufferedReader(isr);
  34.             // creazione stream di output su clientSocket
  35.             OutputStreamWriter osw = new OutputStreamWriter(clientSocket.getOutputStream());
  36.             BufferedWriter bw = new BufferedWriter(osw);
  37.             out = new PrintWriter(bw, true);
  38.             //ciclo di ricezione dal client e invio di risposta
  39.             String str = "";
  40.             String somma = "";
  41.             CoordinatoreHl7 coordinatoreHl7 = new CoordinatoreHl7();
  42.             while (true) {
  43.                 while (str!=null){
  44.                     str = in.readLine();
  45.                     System.out.println(str);
  46.  
  47.                     if(str!=null) {
  48.                         somma = somma + str + "\r";
  49.                     }
  50.                 }
  51.                 if(!somma.isEmpty()){
  52.                     coordinatoreHl7.acceptHlt(somma);
  53.                 }
  54.                 somma = "";
  55.             }
  56.  
  57.  
  58.  
  59.  
  60.         }
  61.         catch (IOException | HL7Exception e) {
  62.             System.err.println("Accept failed");
  63.             System.exit(1);
  64.         }
  65.         // chiusura di stream e socket
  66.  
  67.         System.out.println("Server: closing...");
  68.         out.close();
  69.         try {
  70.             in.close();
  71.             clientSocket.close();
  72.             serverSocket.close();
  73.         } catch (IOException e) {
  74.             e.printStackTrace();
  75.         }
  76.  
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement