Visualizzazione dei risultati da 1 a 5 su 5

Hybrid View

  1. #1
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Ho riguardato un attimo l'altra tua discussione in cui hai postato il codice. Non ho tempo purtroppo adesso di valutare quel tuo codice ma ho visto ora che usi MulticastSocket. Il "multicast" è più particolare rispetto ad una normale comunicazione con i socket. Per il multicast va usato un indirizzo IP in un range ben preciso (e che non centra niente con il IP delle interfacce di rete della macchina!).
    Valuta (o rivaluta se non l'hai fatto a sufficienza) il concetto di "multicast" e come usare gli indirizzi IP.
    Per dubbi, ovviamente chiedi pure. Appena posso sono sempre ben disponibile (premettendo che il multicast non è certo la cosa che uso tutti i giorni ....).
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2011
    Messaggi
    339
    Quote Originariamente inviata da andbin Visualizza il messaggio
    Ho riguardato un attimo l'altra tua discussione in cui hai postato il codice. Non ho tempo purtroppo adesso di valutare quel tuo codice ma ho visto ora che usi MulticastSocket.
    No no , non centra l'altro post...

    Non sto utilizzando il multicast; quello di cui ti parlo è un altro esercizio.

    Se hai un pò di tempo potresti dargli un occhiata , Mi faresti un grande favore ; sto sclerando per riuscire a correggerlo.

    ti posto il codice, magari lo fai partire sulla tua macchina e mi fai sapere

    Client :

    codice:
    package appello19Febbraio2014;
    
    
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.security.*;
    import java.security.spec.*;
    import javax.crypto.*;
    
    
    public class Client{
    	
    	InetAddress serverAddress;
    	int serverPort;
    	Socket socket;
    	int UDPPort;
    	DatagramSocket dSocket;
    	Socket gkSocket;
    	int gkPort;
    	Cipher cipher , decipher;
    	
    	public Client(InetAddress serverAddress , int serverPort , int UDPPort , int gkPort){
    		
    		this.serverAddress = serverAddress;
    		this.serverPort = serverPort;
    		this.UDPPort = UDPPort;
    		this.gkPort = gkPort;
    		
    		try{
    			socket = new Socket(serverAddress , serverPort);
    			dSocket = new DatagramSocket(UDPPort);
    			gkSocket = new Socket(serverAddress , gkPort);
    			
    		}catch(Exception err){
    			err.printStackTrace();
    		}
    		
    	}//COSTRUTTORE
    	
    	public void InizializzaCriptazione(){
    		
    		try{
    			
    				
    				//RICHIESTA DELLA CHIAVE PUBBLICA :
    				OutputStream out = gkSocket.getOutputStream();
    				out.write(1);
    				
    				//RICEZIONE DELLA CHIAVE PUBBLICA :
    				byte [] publicKeyByte = new byte [1000];
    				InputStream in = gkSocket.getInputStream();
    				
    				//SALVATAGGIO DELLA CHIAVE PUBBLICA :
    				int numByte = in.read(publicKeyByte);
    				publicKeyByte = Arrays.copyOf(publicKeyByte , numByte);
    				
    				X509EncodedKeySpec ks = new X509EncodedKeySpec(publicKeyByte);
    				KeyFactory kf = KeyFactory.getInstance("RSA");
    				PublicKey publicKey = kf.generatePublic(ks);
    				
    				//INIZIALIZZAZIONE CIFRARI :
    				cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    				cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    				decipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    				decipher.init(Cipher.DECRYPT_MODE, publicKey);
    				
    				in.close();
    				out.close();
    			
    		}catch(Exception err){
    			try{
    				gkSocket.close();
    			}catch(Exception err2){
    				err2.printStackTrace();
    			}
    		}
    	}//RUN
    	
    	public void InviaRichiestaEdAttendiPacchettoUDP(String richiesta){
    		
    		InizializzaCriptazione();
    		
    		try{
    			//INVIO DELLA RICHIESTA CIFRATA TRAMITE TCP :
    		OutputStream out = socket.getOutputStream();
    		out.write(cipher.doFinal(richiesta.getBytes()));
    		
    		//ATTESA DEL PACCHETTO UDP :
    		byte [] buf = new byte [128];
    		DatagramPacket packet = new DatagramPacket(buf , buf.length);
    		dSocket.receive(packet);
    		
    		//DECIFRAZIONE E STAMPA DEL MESSAGGIO RICEVUTO :
    		byte [] messaggioByte = decipher.doFinal(packet.getData());
    		String messaggio = new String(messaggioByte);
    		System.out.println(messaggio);
    		
    		
    		}catch(Exception err){
    			err.printStackTrace();
    		}
    		
    	}// InviaRichiestaEdAttendiPacchettoUDP
    	
    	public static void main (String [] args){
    		
    		try{
    			InetAddress serverAddress = InetAddress.getByName("127.0.0.1"); //SULL'APPELLO : 150.2.66.55
    			int serverPort = 6800;
    			int UDPPort = 7700;
    			int gkPort = 7800;
    			
    			Client client = new Client(serverAddress , serverPort , UDPPort , gkPort);
    			client.InviaRichiestaEdAttendiPacchettoUDP("127.0.0.1@Ciao");
    			
    		}catch(Exception err){
    			err.printStackTrace();
    		}
    	}//MAIN
    
    
    }// THREAD CLIENT
    Host :

    codice:
    package appello19Febbraio2014;
    
    
    import java.util.*;
    import java.io.*;
    import java.net.*;
    import java.security.*;
    import java.security.spec.*;
    import javax.crypto.*;
    
    
    public class Host extends Thread {
    	
    	ServerSocket sSocket;
    	int skPort;
    	KeyPair kp;
    	Cipher cipher , decipher;
    	
    	public Host(int skPort){
    		
    		this.skPort = skPort;
    		
    		try{
    			sSocket = new ServerSocket(skPort);
    		}catch(Exception err){
    			err.printStackTrace();
    		}
    		
    	}//COSTRUTTORE
    	
    	public void run(){
    		
    		try{
    			while(true){
    				
    				Socket s = sSocket.accept();
    				InputStream in = s.getInputStream();
    				in.read();
    				
    				KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    				kpg.initialize(1024);
    				kp = kpg.generateKeyPair();
    				
    				OutputStream out = s.getOutputStream();
    				out.write(kp.getPublic().getEncoded());
    				
    			}
    		}catch(Exception e){
    			try{
    				sSocket.close();
    			}catch(Exception e2){
    				e2.printStackTrace();
    			}
    		}
    	}//RUN
    	
    	class gestisciRichieste extends Thread{
    		
    		Socket s;
    		int UDPPort;
    		DatagramSocket dSocket;
    		
    		public gestisciRichieste(Socket s , int UDPPort){
    			
    			this.s = s;
    			this.UDPPort = UDPPort;
    	
    			try{
    			dSocket = new DatagramSocket(UDPPort);
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    			this.start();
    			
    		}//COSTRUTTORE
    		
    		public void run(){
    			
    			try{
    				while(true){
    					
    					decipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    					decipher.init(Cipher.DECRYPT_MODE, kp.getPrivate());
    					cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    					cipher.init(Cipher.ENCRYPT_MODE, kp.getPrivate());
    					
    					InputStream in = s.getInputStream();
    					byte [] richiestaByte = new byte [1000];
    					int numByte = in.read(richiestaByte);
    					richiestaByte = Arrays.copyOf(richiestaByte, numByte);
    					String richiesta = new String(decipher.doFinal(richiestaByte));
    					
    					int pos = richiesta.indexOf("@");
    					String ip = richiesta.substring(0,pos);
    					String messaggio = richiesta.substring(pos+1);
    					System.out.println(ip);
    					
    					InetAddress clientAddress = InetAddress.getByName(ip);
    					
    					byte [] buf = new byte [1000];
    					buf = cipher.doFinal(messaggio.getBytes());
    					DatagramPacket packet = new DatagramPacket(buf,buf.length,clientAddress,UDPPort);
    					dSocket.send(packet);
    					
    					in.close();
    				}
    			}catch(Exception e){
    				try{
    					dSocket.close();
    				}catch(Exception e2){
    					e2.printStackTrace();
    				}
    			}
    		}//RUN
    		
    		
    	}//THREAD GESTISCI RICHIESTE
    	
    	public static void main (String [] args){
    		
    		try{
    		int skPort = 7800;
    		int UDPPort = 7700;
    		ServerSocket servSocket = new ServerSocket(6800);
    		
    		Host host = new Host(skPort);
    		host.start();
    		
    		while(true){
    			
    			Socket s = servSocket.accept();
    			gestisciRichieste gr = host.new gestisciRichieste(s,UDPPort);
    		 }
    		
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}//MAIN
    	
    }//THREAD HOST
    Per favore guardalo un attimo.
    Ho messo come indirizzo l' ip di localHost 127.0.0.1
    Ma mi da quella cacchio di eccezione
    java.net.BindException: Address already in use: Cannot bind


    at java.net.DualStackPlainDatagramSocketImpl.socketBi nd(Native Method)
    at java.net.DualStackPlainDatagramSocketImpl.bind0(Un known Source)
    at java.net.AbstractPlainDatagramSocketImpl.bind(Unkn own Source)
    at java.net.DatagramSocket.bind(Unknown Source)
    at java.net.DatagramSocket.<init>(Unknown Source)
    at java.net.DatagramSocket.<init>(Unknown Source)
    at java.net.DatagramSocket.<init>(Unknown Source)
    at appello19Febbraio2014.Client.<init>(Client.java:30 )
    at appello19Febbraio2014.Client.main(Client.java:112)
    java.lang.NullPointerException
    at appello19Febbraio2014.Client.InizializzaCriptazion e(Client.java:71)
    at appello19Febbraio2014.Client.InviaRichiestaEdAtten diPacchettoUDP(Client.java:80)
    at appello19Febbraio2014.Client.main(Client.java:113)
    java.lang.NullPointerException
    at appello19Febbraio2014.Client.InviaRichiestaEdAtten diPacchettoUDP(Client.java:85)
    at appello19Febbraio2014.Client.main(Client.java:113)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.