Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Server - Client Protocol Debugger - ServerClientDebugger.java

ServerClientDebugger.java

Caricato da: Fraioveio
Scarica il programma completo

  1. package com.fraioveio;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import javax.swing.JOptionPane;
  6.  
  7. public class ServerClientDebugger {
  8.     public static void main(String[] args) throws Exception {
  9.         if(System.console() == null) {
  10.             JOptionPane.showMessageDialog(null, "Questo programma deve essere eseguito da console!", "Server - Client Debugger", JOptionPane.ERROR_MESSAGE);
  11.             return;
  12.         }
  13.        
  14.         System.out.println("Client - Server Debugger 1.0\nby FraioVeio\n");
  15.        
  16.         InputStream in = System.in;
  17.         DataInputStream din = new DataInputStream(in);
  18.        
  19.         System.out.print("Scegli la modalita':\n\n1) Client\n2) Server\n\nScelta: ");
  20.         String sc = din.readLine();
  21.        
  22.         try {
  23.             if((!sc.equals("1")) && (!sc.equals("2")))
  24.                 throw new NumberFormatException();
  25.            
  26.             boolean cs = Integer.parseInt(sc) == 1 ? true : false;
  27.            
  28.             try {
  29.                 if (cs) {
  30.                     client();
  31.                 } else {
  32.                     server();
  33.                 }
  34.             } catch (IOException ex) {
  35.                 System.err.println(ex);
  36.             }
  37.         } catch (NumberFormatException ex) {
  38.             System.err.println("Scelta non corretta!");
  39.             return;
  40.         }
  41.     }
  42.    
  43.     public static void client() throws IOException {
  44.         InputStream in = System.in;
  45.         DataInputStream din = new DataInputStream(in);
  46.        
  47.         System.out.println("\n##########\n# Client #\n##########\n");
  48.         System.out.println("Per chiudere la connessione premere TAB e Invio");
  49.        
  50.         System.out.print("IP Server: ");
  51.         String ip = din.readLine();
  52.         System.out.print("Porta: ");
  53.         int port = Integer.parseInt(din.readLine());
  54.        
  55.         System.out.println("Tentetivo di connessione...");
  56.         final Socket s = new Socket(ip, port);
  57.         System.out.println("Connesso.\n\n");
  58.        
  59.         final InputStream sin = s.getInputStream();
  60.         final OutputStream sout = s.getOutputStream();
  61.        
  62.         new Thread() {
  63.             @Override
  64.             public void run() {
  65.                 int n;
  66.                 while(!s.isClosed()) {                    
  67.                     try {
  68.                         n = sin.read();
  69.                         if(n != -1)
  70.                             System.out.write(n);
  71.                         else
  72.                             return;
  73.                     } catch (IOException ex) {}
  74.                 }
  75.             }
  76.         }.start();
  77.        
  78.         int n;
  79.         while(!s.isClosed()) {
  80.             n = in.read();
  81.             if(n != 9)
  82.                 sout.write(n);
  83.             else
  84.                 break;
  85.         }
  86.        
  87.         s.close();
  88.        
  89.         System.out.println("Connessione chiusa.");
  90.     }
  91.    
  92.     public static void server() throws IOException {
  93.         InputStream in = System.in;
  94.         DataInputStream din = new DataInputStream(in);
  95.        
  96.         System.out.println("\n##########\n# Server #\n##########\n");
  97.         System.out.println("Per chiudere la connessione premere TAB e Invio");
  98.        
  99.         System.out.print("Porta: ");
  100.         int port = Integer.parseInt(din.readLine());
  101.        
  102.         ServerSocket ss = new ServerSocket(port);
  103.         System.out.println("Attesa connessioni...");
  104.         final Socket s = ss.accept();
  105.         System.out.println("Connesso " + s.getInetAddress() + "\n\n");
  106.        
  107.         final InputStream sin = s.getInputStream();
  108.         final OutputStream sout = s.getOutputStream();
  109.        
  110.         new Thread() {
  111.             @Override
  112.             public void run() {
  113.                 int n;
  114.                 while(!s.isClosed()) {                    
  115.                     try {
  116.                         n = sin.read();
  117.                         if(n != -1)
  118.                             System.out.write(n);
  119.                         else
  120.                             return;
  121.                     } catch (IOException ex) {}
  122.                 }
  123.             }
  124.         }.start();
  125.        
  126.         int n;
  127.         while(!s.isClosed()) {
  128.             n = in.read();
  129.             if(n != 9)
  130.                 sout.write(n);
  131.             else
  132.                 break;
  133.         }
  134.        
  135.         s.close();
  136.        
  137.         System.out.println("Connessione chiusa.");
  138.     }
  139. }