![]() |
Pages (7): « 1 [2] 3 4 5 6 » ... Last » Show 150 posts per page |
.dsy:it. (http://www.dsy.it/forum/)
- Reti di calcolatori (http://www.dsy.it/forum/forumdisplay.php?forumid=68)
-- [LABORATORIO PRINI CASSIANO] Esame Marzo 06 (http://www.dsy.it/forum/showthread.php?threadid=24451)
Vodoo ha messo un link a cui puoi trovare gli esercizi d'esempio, proprio in questo thread! altri non ce ne sono penso.....caso mai inventali! eheh
ma l'orario dell'esame non è uscito?! o almeno il giorno!!!!
__________________
۞_Rag Doll_۞
x quanto mi riguarda la vedo grigia!! provo a postare 4 esempi che mi sono arrivati per le mani...fatemi sapere che ne pensate
e questo
vi posto un esempio su cui ho studiato molto e sul quale ho imparato le funzioni per la programmazione. Una volta imparato bene questo esempio poi ho spaziato e aggiunto una serie di dialoghi client/server per vedere se avevo capito. Ve lo posto semplice, è quello che gira sui siti. Se lo copiate tale e quale in blocco note e lo compilate funziona.
Si tratta di un client che chiede in input una frase, la spedisce al server, il server la trasforma in maiuscolo con una semplice funzione (toUpperCase) e la rispedisce al client, che la visualizza.
L'esempio è fatto sia in TCP che in UDP, così vedete la stessa cosa fatta nei 2 metodi...
CLIENT UDP
import java.io.*;
import java.net.*;
class clientUDP
{
public static void main(String args[]) throws Exception
{
DatagramSocket client = null;
DatagramPacket pacchetto_send, pacchetto_recv;
InetAddress indirizzo;
String frase_client, frase_server;
byte[] buffer;
client = new DatagramSocket();
buffer = new byte[1024];
System.out.println("Inserisci la frase da modificare: ");
BufferedReader from_client = new BufferedReader(new InputStreamReader(System.in));
frase_client = from_client.readLine();
buffer = frase_client.getBytes();
indirizzo = InetAddress.getByName("localhost");
pacchetto_send = new DatagramPacket(buffer, buffer.length, indirizzo, 7000);
client.send(pacchetto_send);
pacchetto_recv = new DatagramPacket(buffer, buffer.length);
client.receive(pacchetto_recv);
frase_server = new String(pacchetto_recv.getData());
System.out.println("La frase modificata è: " + frase_server);
client.close();
}
}
SERVER UDP
import java.io.*;
import java.net.*;
class serverUDP
{
public static void main(String args[]) throws Exception
{
ThreadUDP t;
t = new ThreadUDP();
t.start();
}
}
class ThreadUDP extends Thread
{
DatagramSocket server;
DatagramPacket pacchetto_send, pacchetto_recv;
byte[] dati_send = new byte[1024];
byte[] dati_recv = new byte[1024];
String frase_client, frase_server;
InetAddress indirizzo;
int porta_client;
public void run()
{
try
{
while(true)
{
server = new DatagramSocket(7000);
pacchetto_recv = new DatagramPacket(dati_recv, dati_recv.length);
server.receive(pacchetto_recv);
frase_client = new String (pacchetto_recv.getData());
frase_server = frase_client.toUpperCase();
dati_send = frase_server.getBytes();
porta_client = pacchetto_recv.getPort();
indirizzo = pacchetto_recv.getAddress();
pacchetto_send = new DatagramPacket(dati_send, dati_send.length, indirizzo, porta_client);
server.send(pacchetto_send);
}
}
catch(IOException e) {}
}
}
CLIENT TCP
import java.io.*;
import java.net.*;
class clientTCP {
public static void main (String args[]) throws Exception {
String frase_client, frase_server;
Socket client = null;
client = new Socket("localhost",7000);
System.out.println("Inserisci la frase da modificare:");
BufferedReader from_client = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream client_out = new DataOutputStream(client.getOutputStream());
frase_client = from_client.readLine();
client_out.writeBytes(frase_client + '\n');
BufferedReader from_server = new BufferedReader(new InputStreamReader(client.getInputStream()));
frase_server = from_server.readLine();
System.out.println("La stringa modificata e': " + frase_server);
client.close();
}
}
SERVER TCP
import java.io.*;
import java.net.*;
class serverTCP {
public static void main (String args[]) throws Exception {
ServerSocket server = null;
Socket client = null;
ThreadTCP t;
try
{
server = new ServerSocket(7000);
}
catch (IOException e){}
while(true)
{
client = server.accept();
t = new ThreadTCP(client);
t.start();
}
}
}
class ThreadTCP extends Thread {
Socket thread_client = null;
public ThreadTCP(Socket client)
{
this.thread_client = client;
}
public void run()
{
try
{
String frase_client, frase_server;
BufferedReader from_client = new BufferedReader(new InputStreamReader(thread_client.getInputStream()));
frase_client = from_client.readLine();
frase_server = frase_client.toUpperCase();
DataOutputStream server_out = new DataOutputStream(thread_client.getOutputStream());
server_out.writeBytes(frase_server + '\n');
}
catch (IOException e) {}
}
}
__________________
In un bagno in via Celoria c'è scritto: "Se entri con il sedere che prude, esci con il dito che puzza!!"
Grazie x gli esempi, sono effettivamente molto utili!!!
__________________
"Anche stanotte le nostre chitarre morderanno e a chi sta per fare rock io rendo onore............"
"And be a simple kind of man.
Be something you love and understand"
LONG LIVE ROCK 'N' ROLL!!!!!!!!!
data e orari (apro altro solo x fare "pulizia"
)
http://www.dsy.it/forum/showthread....&threadid=24476
i programmi sono ottimi!!! "basta impararli a memoria"....l'esame consiste in una cosa simile vero? non bisogna gestire più richieste multithread o cose varie? cmq xkè udp chiude la connessione server e tcp no?
a me cmq non vanno nessuno dei 4 programmi di vodoo...
cioè, 3 su 4 li compila e nesusno poi parte, è un problema del mio pc?
__________________
http://www.myspace.com/bruzzband
va che spacchiamo i culicchi!
dice sempre Exception in thread "main" java.lang.noclassfounderror blablabla
__________________
http://www.myspace.com/bruzzband
va che spacchiamo i culicchi!
se intendi i 4 postati da glucks sembra strano a me vanno tutti e 4...ma tu come gestisci la cosa? io facci opartire il server da dos e il client da gel (o programmi simili come jcreator o robe varie) e funziona tutto alla perfezione.
da quello che hai scritto ti lancia l'eccezione evidentemente la connessione server si blocca prima che parte il client.
in quello che non ti compila che errore ti da?
nono, sono riuscito a compilarli tutti e 4, cmq io li lancio tutti da dos, prima i server poi i client e tutti mi danno quel problema...
vabè oh, a sto punto me li imparo a memoria e spero che in laboratorio funzionino...
__________________
http://www.myspace.com/bruzzband
va che spacchiamo i culicchi!
potrebbe essere perchè non hai chiamato il file con il nome della classe...
se la classe si chiama
class ClientTCP .........
il file da compilare si dovrà chiamare ClientTCP.java (con la maiuscola)
Quel problema me lo dava quando non trovava il file... o non l'avevo chiamato bene (attenzione perchè lo compila senza problemi, ma se ne va a male quando viene eseguito)
Spero sia questo il problema, giuro che a me vanno !!! Ed anche a qualcun altro che ha postato...
__________________
In un bagno in via Celoria c'è scritto: "Se entri con il sedere che prude, esci con il dito che puzza!!"
eh, purtroppo no, vabè pazienza... sono troppo depresso per perderci ancora tempo, java non fa certo bene alla salute
__________________
http://www.myspace.com/bruzzband
va che spacchiamo i culicchi!
scusa ma come fai a far partire 2 prog da dos contemporanemente?
uso il prompt dei comandi ![]()
__________________
http://www.myspace.com/bruzzband
va che spacchiamo i culicchi!
| All times are GMT. The time now is 11:09. | Pages (7): « 1 [2] 3 4 5 6 » ... Last » Show all 96 posts from this thread on one page |
Powered by: vBulletin Version 2.3.1
Copyright © Jelsoft Enterprises Limited 2000 - 2002.