Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Sony WF-1000X M6: le cuffie in-ear di riferimento migliorano ancora
Sony WF-1000X M6: le cuffie in-ear di riferimento migliorano ancora
WF-1000X M6 è la sesta generazione di auricolare in-ear sviluppata da Sony, un prodotto che punta a coniugare facilità di utilizzo con una elevata qualità di riproduzione dei contenuti audio e una cura nella riduzione del rumore ambientale che sia da riferimento
Snowflake porta l'IA dove sono i dati, anche grazie a un accordo con OpenAI
Snowflake porta l'IA dove sono i dati, anche grazie a un accordo con OpenAI
Snowflake ha presentato diverse novità per la sua piattaforma legate all'intelligenza artificiale. Quella forse più eclatante è una collaborazione con OpenAI, ma non mancano diverse nuove funzionalità che rendono la piattaforma più flessibile e in grado di rispondere meglio alle esigenze in continuo cambiamento delle aziende
Sistema Mesh Roamii BE Pro: il Wi-Fi 7 secondo MSI
Sistema Mesh Roamii BE Pro: il Wi-Fi 7 secondo MSI
Con velocità teoriche fino a 11 Gbps, gestione tramite app intelligente e protezione avanzata dei dispositivi, Roamii BE Pro porta il Wi‑Fi 7 tri‑band nelle abitazioni più esigenti. Un sistema Wi-Fi Mesh proposto da MSI allo scopo di garantire agli utenti una rete fluida e continua capace di sostenere streaming 8K, gaming competitivo e le applicazioni moderne più esigenti in termini di banda
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 17-05-2011, 08:37   #1
Gimmy2
Member
 
L'Avatar di Gimmy2
 
Iscritto dal: Jan 2008
Città: Paderno Dugnano
Messaggi: 116
Client - Server MultiThreading

Ciao a tutti! Premetto che sono nuovo riguardo la programmazione java e ad oggetti. Ho la seguente situazione: ho un server che quando viene contattato dal client genera un thread e una socket e scambia dati con il client. Fin qui tutto ok, pero ora vorrei che quando il client contatta il server, quest'ultimo generi 3 o piu thread ognuno dei quali calcola dei dati e li passa, sempre sulla stessa socket in modo concorrente al client.

Server.java

Codice:
import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;
 
/**
* This is to help people to write Client server application
*  I tried to make it as simple as possible... the client connect to the server
*  the client send a String to the server the server returns it in UPPERCASE thats all
*/
public class Server {
 
        // the socket used by the server
        private ServerSocket serverSocket;
		public int i=0;
        // server constructor
        Server(int port) {
               
                /* create socket server and wait for connection requests */
                try
                {
                        serverSocket = new ServerSocket(port);
                        System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());
 
                        while(true)
                        {
                                Socket socket = serverSocket.accept();  // accept connection
                                System.out.println("New client asked for a connection");
								for(i=0; i<3; i++){
									TcpThread t = new TcpThread(socket);    // make a thread of it
							//		TcpThread t2 = new TcpThread(socket);    // make a thread of it
							//		TcpThread t3 = new TcpThread(socket);    // make a thread of it
									System.out.println("Starting thread " + i);
									t.start();
							//		t2.start();
							//		t3.start();
								}
								
                        }
                }
                catch (IOException e) {
                        System.out.println("Exception on new ServerSocket: " + e);
                }
        }               
 
//        you must "run" server to have the server run as a console application
        public static void main(String[] arg) {
                // start server on port 1500
                new Server(1500);
        }
       
        /** One instance of this thread will run for each client */
        class TcpThread extends Thread {
                // the socket where to listen/talk
                Socket socket;
                DataInputStream Sinput;
                DataOutputStream Soutput;
               
                TcpThread(Socket socket) {
                        this.socket = socket;
                }
                synchronized public void run() {
                        /* Creating both Data Stream */
                        System.out.println("Thread " + i + " trying to create Object Input/Output Streams");
                        try
                        {
                                // create output first
                                Soutput = new DataOutputStream(socket.getOutputStream());
                        //        Soutput.flush();
                                Sinput  = new DataInputStream(socket.getInputStream());
                        }
                        catch (IOException e) {
                                System.out.println("Exception creating new Input/output Streams: " + e);
                                return;
                        }
                        System.out.println("Thread " + i + " waiting for a String from the Client");
                        // read a String (which is an object)
                        try {		
						
							// creazione vettore di interi
							double[] vettore = new double[10];
							for(int i=0; i<10; i++){
								vettore[i] = Math.random()*Integer.MAX_VALUE;
							}
							// individuazione del valore var1
							double var1=0;
							for(int i=0; i<10; i++){
								if(i==0)
									var1=vettore[i];
								if(var1>vettore[i])
									var1=vettore[i];
							}
						// mi faccio inviare il var2 dal professor
                            double var2 = (double) Sinput.readDouble();
                        //        var1 = var1.toUpperCase();
							System.out.println("Ricevuto var2 = " + var2);
						// se il mio var1 è > del suo var2 allora glielo invio
							if(var1>var2){
								System.out.println("var1 = " + var1 + " > var2 = " + var2 + " => INVIO\n\n");
								Soutput.writeDouble(var1);
                        //      Soutput.flush();
							}else{
								System.out.println("var1 = " + var1 + " < var2 = " + var2 + " => NON INVIO\n\n");
							}
                        }
                        catch (IOException e) {
                                System.out.println("Exception reading/writing Streams: " + e);
                                return;                               
                        }
                        // will surely not happen with a String
                    //    catch (ClassNotFoundException o) {                               
                    //    }
                        finally {
                                try {
                                        Soutput.close();
                                        Sinput.close();
                                }
                                catch (Exception e) {                                       
                                }
                        }
                }
        }
		
}

Client.java

Codice:
import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;
 
public class Client {
 
		public static double var2=0;
 
        DataInputStream Sinput;                // to read the socker
        DataOutputStream Soutput;              // to write on the socket
        Socket socket;
 
        // Constructor connection receiving a socket number
        Client(int port) {
                // we use "localhost" as host name, the server is on the same machine
                // but you can put the "real" server name or IP address
                try {
                        socket = new Socket("localhost", port);
                }
                catch(Exception e) {
                        System.out.println("Error connectiong to server:" + e);
                        return;
                }
                System.out.println("Connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
 
                /* Creating both Data Stream */
                try
                {
                        Sinput  = new DataInputStream(socket.getInputStream());
                        Soutput = new DataOutputStream(socket.getOutputStream());
                }
                catch (IOException e) {
                        System.out.println("Exception creating new Input/output Streams: " + e);
                        return;
                }
                // now that I have my connection
    //            String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
	

	
                // send the string to the server
                System.out.println("Client sending \"" + var2 + "\" to server");
                try {
                        Soutput.writeDouble(var2);
                //        Soutput.flush();
                }
                catch(IOException e) {
                        System.out.println("Error writing to the socket: " + e);
                        return;
                }
                // read back the answer from the server
                double response;
                try {
                        response = (double) Sinput.readDouble();
                        System.out.println("Read back from server: " + response);
						var2=response;
                }
                catch(Exception e) {
                        System.out.println("Problem reading back from server: " + e);
                }
               
                try{
                        Sinput.close();
                        Soutput.close();
                }
                catch(Exception e) {}
        } 
 
        public static void main(String[] arg) throws IOException {
				
				BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
				String sceltaString;
				int scelta=0;
				do{
					System.out.println("\nCosa vuoi fare?");
					System.out.println("1) Contattare i Telescopi. ( var2 attuale = " + var2 + " )");
					System.out.println("2) Terminare l'esperimento.");
					System.out.print("Scelta: ");
					
					System.out.flush();
					sceltaString = in.readLine();
					
					scelta = Integer.parseInt(sceltaString);
					
					
					switch (scelta){
						case 1:
							new Client(1500);
						break;
						
						case 2:
							System.exit(0);
 
						default:
							System.out.println("\n!! Scegli il numero 1 o il numero 2 !! \n");
					}	
					
				}while(scelta!=2);
        }
}
Grazi a chiunque possa aiutarmi!
__________________
Intel Core Duo E8400 - Asus P5Q - Corsair XMS2 DHX CM2X2048-6400C4DHX- ATI Radeon HD 4850 - Samsung SyncMaster 206BW - Gigabyte GZ-X5 - Corsair 550VXEU - Microsoft Windows 7 Professional
Gimmy2 è offline   Rispondi citando il messaggio o parte di esso
Old 17-05-2011, 18:28   #2
Gimmy2
Member
 
L'Avatar di Gimmy2
 
Iscritto dal: Jan 2008
Città: Paderno Dugnano
Messaggi: 116
Scusate il doppio post, ma sono incappato in un nuovo problema: in questo codice (che il server postato in precedenza modificato) dichiaro "public synchronized void run() { ... }", quindi teoricamente in quella parte di codice dovrebbe entre un thread alla volta finchè non esce giusto? Invece nell'esecuzione vedo che gli n thread che faccio partire entrano quando vogliono loro in run(), come se synchronized non ci fosse... come mai??

Codice:
import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;
 
/**
* This is to help people to write Client server application
*  I tried to make it as simple as possible... the client connect to the server
*  the client send a String to the server the server returns it in UPPERCASE thats all
*/
public class Server {

		public static int i=1;
		public static double[][] vettore = new double[i][10];
		public static double[] var1 = new double[i];
		public static int numt = 0;
		
        // the socket used by the server
        private ServerSocket serverSocket;
		public int j=0;
        // server constructor
        Server(int port){
               
                /* create socket server and wait for connection requests */
                try
                {
                        serverSocket = new ServerSocket(port);
                        System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());
 
                        while(true)
                        {
                                Socket socket = serverSocket.accept();  // accept connection
                                System.out.println("New client asked for a connection");
								for(j=0; j<i; j++){
									TcpThread t = new TcpThread(socket, j);    // make a thread of it
							//		System.out.println("Starting thread 1");
							//		TcpThread t2 = new TcpThread(socket, 2);    // make a thread of it
							//		System.out.println("Starting thread 2");
							//		TcpThread t3 = new TcpThread(socket, 3);    // make a thread of it
							//		System.out.println("Starting thread 3");
									System.out.println("Starting thread " + j);
									t.start();
							//		t2.start();
							//		t3.start();
								}
								
                        }
                }
                catch (IOException e) {
                        System.out.println("Exception on new ServerSocket: " + e);
                }
        }               
 
//        you must "run" server to have the server run as a console application
        public static void main(String[] arg) {
                // start server on port 1500
                new Server(1500);
        }
       
        /** One instance of this thread will run for each client */
        class TcpThread extends Thread {
                // the socket where to listen/talk
				int n;
                Socket socket;
                DataInputStream Sinput;
                DataOutputStream Soutput;
               
                TcpThread(Socket socket, int n) {
                        this.socket = socket;
						this.n = n;
                }
                public synchronized void run() {
				//	synchronized (this){
						numt++;
                        /* Creating both Data Stream */
                        System.out.println("Thread " + n + " trying to create Object Input/Output Streams");
                        try
                        {
                                // create output first
                                Soutput = new DataOutputStream(socket.getOutputStream());
                        //        Soutput.flush();
                                Sinput  = new DataInputStream(socket.getInputStream());
                        }
                        catch (IOException e) {
                                System.out.println("Exception creating new Input/output Streams: " + e);
                                return;
                        }
                        System.out.println("Thread " + n + " waiting for a String from the Client");
                        // read a String (which is an object)
                        try {		
						
						// creazione vettore di interi						
							for(int j=0; j<10; j++){
								vettore[n][j] = Math.random()*Integer.MAX_VALUE;
							}
							
						// individuazione del valore var1[n]						
							for(int j=0; j<10; j++){
								if(j==0)
									var1[n]=vettore[n][j];
								if(var1[n]>vettore[n][j])
									var1[n]=vettore[n][j];
							}
							
						// mi faccio inviare il var2 dal professor
                            double var2 = (double) Sinput.readDouble();
							System.out.println("Ricevuto var2 = " + var2);
							
						// se il mio var1 è > del suo var2 allora glielo invio...
							if(var1[n]>var2){
								double kessvar=1;
								System.out.println("Avviso il Professor che gli dto per inviare var1");
								Soutput.writeDouble(kessvar);
								System.out.println("var1[n] = " + var1[n] + " > var2 = " + var2 + " => INVIO\n\n");
								Soutput.writeDouble(var1[n]);
							}else{
						// ...altrimenti comunico che non invio niente							
								double kessvar=0;
								System.out.println("Avviso il Professor che rimango in attesa");
								Soutput.writeDouble(kessvar);							
								System.out.println("var1[n] = " + var1[n] + " < var2 = " + var2 + " => NON INVIO\n\n");
							//	Soutput.writeline("NON INVIO UN BEL NIENTE");
							}
                        }
                        catch (IOException e) {
                                System.out.println("Exception reading/writing Streams: " + e);
                                return;                               
                        }
                        // will surely not happen with a String
                    //    catch (ClassNotFoundException o) {                               
                    //    }
						if(numt==i){			
							try {
									Soutput.close();
									Sinput.close();
							}catch (Exception e) {}
							
							numt=0;
						}
				//	}
                }
		/*	public synchronized void exit(){
				int newValue = c - 1;
				c = newValue;
				try {
                    Soutput.close();
                    Sinput.close();
                }catch (Exception e) {}
			}	*/
			
        }
		
	/* class funzione {
		synchronized void printThis(char c, int times) {
			for (int j=0; j<times; j++) {
				try{Thread.sleep(1);}
				catch(Exception ex) {ex.printStackTrace();}
				System.out.print(c);
			}
		}

	} //end class */
}
__________________
Intel Core Duo E8400 - Asus P5Q - Corsair XMS2 DHX CM2X2048-6400C4DHX- ATI Radeon HD 4850 - Samsung SyncMaster 206BW - Gigabyte GZ-X5 - Corsair 550VXEU - Microsoft Windows 7 Professional
Gimmy2 è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


Sony WF-1000X M6: le cuffie in-ear di riferimento migliorano ancora Sony WF-1000X M6: le cuffie in-ear di riferiment...
Snowflake porta l'IA dove sono i dati, anche grazie a un accordo con OpenAI Snowflake porta l'IA dove sono i dati, anche gra...
Sistema Mesh Roamii BE Pro: il Wi-Fi 7 secondo MSI Sistema Mesh Roamii BE Pro: il Wi-Fi 7 secondo M...
Recensione HUAWEI Mate X7: un foldable ottimo, ma restano i soliti problemi Recensione HUAWEI Mate X7: un foldable ottimo, m...
Nioh 3: souls-like punitivo e Action RPG Nioh 3: souls-like punitivo e Action RPG
Meta lavora a un sistema di riconoscimen...
Il mercato smartphone potrebbe registrar...
Apple punterà sull'architettura c...
NASA Curiosity: i processi non biologici...
Sega conferma l'arrivo di tanti nuovi gi...
La serie POCO X8 è pronta al debu...
Apple conferma che l'arrivo della 'nuova...
Le vendite di Square Enix sono in netto ...
iPhone 17e si mostra in un video 'first ...
Il nuovo Xiaomi Watch 5 è pronto ...
Steam Deck è out of stock in dive...
Le migliori offerte Amazon del weekend, ...
PC più potente, meno spesa: su Amazon ta...
Amazon Haul: come fare acquisti 'pazzi' ...
Threads permetterà agli utenti di...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 22:24.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Served by www3v