Advertisement
Guest User

Untitled

a guest
May 30th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. SERVER
  2.  
  3.  
  4. package server;
  5. import java.io.*;
  6. import java.net.*;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. public class Server {
  10.  
  11.     ServerSocket server;
  12.     Socket socket;
  13.     int porta;
  14.     DataInputStream in;
  15.     DataOutputStream out;
  16.    
  17.     public Server() throws Exception  {
  18.         try {
  19.            
  20.             server = new ServerSocket(7777);
  21.         } catch (Exception e) {
  22.             System.err.println ("[#] Server non creato ");
  23.         }
  24.         System.out.println ("[#] Server creato");
  25.         socket = server.accept();
  26.        
  27.        
  28.  
  29.     }
  30.     public void comunica() throws Exception {
  31.         in = new DataInputStream(socket.getInputStream());
  32.         out = new DataOutputStream(socket.getOutputStream());
  33.         try {
  34.         System.out.println ("Messaggio: ");    
  35.         String messaggioLetto = in.readLine();
  36.         System.out.println ("Messaggio ricevuto: "+ messaggioLetto);
  37.         System.out.println ("Rispondo con:  "+messaggioLetto);
  38.         out.writeBytes(messaggioLetto+"\n"); // Inviamo il messaggio
  39.         } catch(IOException e ) {
  40.             e.printStackTrace();
  41.         }
  42.      
  43.     }
  44.     public void chiudi () {
  45.         try {
  46.             socket.close();
  47.         } catch (IOException ex) {
  48.             Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
  49.         }
  50.     }
  51.    
  52.     public static void main(String[] args) {
  53.         Server server=null;
  54.         try {
  55.             server = new Server();
  56.         } catch (Exception ex) {
  57.             Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
  58.         }
  59.         try {
  60.             server.comunica();
  61.         } catch (Exception ex) {
  62.             Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
  63.         }
  64.         server.chiudi();
  65.     }
  66.  
  67.    
  68.    
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. CLIENT
  77.  
  78. package client;
  79. import java.io.*;
  80. import java.net.*;
  81. import java.util.logging.Level;
  82. import java.util.logging.Logger;
  83. public class Client {
  84.  
  85.     Socket client;
  86.     DataInputStream in;
  87.     DataOutputStream out;
  88.     BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
  89.     public Client() throws IOException {
  90.         try {
  91.             client = new Socket(InetAddress.getLocalHost(),7777);
  92.         } catch (Exception e) {
  93.             System.err.println("[#]Client non connesso");
  94.         }
  95.         System.out.println ("[#]Client connesso");
  96.        
  97.         client.close();
  98.     }
  99.     public void comunica() throws IOException {
  100.        
  101.         System.out.println ("Messaggio da inviare: ");
  102.         String messaggio = input.readLine();
  103.         out.writeBytes(messaggio+ "/n");
  104.         String ricevuto = in.readLine();
  105.         System.out.println ("Messaggio ricevuto: "+ricevuto);
  106.     }
  107.    
  108.     public static void main(String[] args) throws Exception {
  109.        
  110.         Client client = new Client();
  111.         client.comunica();
  112.      
  113.        
  114.     }
  115.    
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement