Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.alten.olympus.poc.referti.server.hl7Manager;
- import ca.uhn.hl7v2.HL7Exception;
- import it.alten.olympus.poc.referti.server.hl7Manager.HL7Decoder;
- import lombok.extern.slf4j.Slf4j;
- import java.io.*;
- import java.net.*;
- @Slf4j
- public class TCPServer implements Runnable {
- @Override
- public void run() {
- final int PORT = 1050;
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(PORT);
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("TCP Server: started ");
- System.out.println("Server Socket: " + serverSocket);
- Socket clientSocket=null;
- BufferedReader in=null;
- PrintWriter out=null;
- try {
- // bloccante finchè non avviene una connessione
- clientSocket = serverSocket.accept();
- System.out.println("Connection accepted: "+ clientSocket);
- // creazione stream di input da clientSocket
- InputStreamReader isr = new InputStreamReader(clientSocket.getInputStream());
- in = new BufferedReader(isr);
- // creazione stream di output su clientSocket
- OutputStreamWriter osw = new OutputStreamWriter(clientSocket.getOutputStream());
- BufferedWriter bw = new BufferedWriter(osw);
- out = new PrintWriter(bw, true);
- //ciclo di ricezione dal client e invio di risposta
- String str = "";
- String somma = "";
- CoordinatoreHl7 coordinatoreHl7 = new CoordinatoreHl7();
- while (true) {
- while (str!=null){
- str = in.readLine();
- System.out.println(str);
- if(str!=null) {
- somma = somma + str + "\r";
- }
- }
- if(!somma.isEmpty()){
- coordinatoreHl7.acceptHlt(somma);
- }
- somma = "";
- }
- }
- catch (IOException | HL7Exception e) {
- System.err.println("Accept failed");
- System.exit(1);
- }
- // chiusura di stream e socket
- System.out.println("Server: closing...");
- out.close();
- try {
- in.close();
- clientSocket.close();
- serverSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement