Java Client - Serveur
Java Client - Serveur
Java Client - Serveur
return new DatagramPacket(buf, buf.length, destAddr, destPort); public static void main(String[] args) {
} byte[] buf = new byte[1000];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
DatagramSocket socket;
static String toString(DatagramPacket p) {
return new String(p.getData(), 0, p.getLength()); try {
} socket = new DatagramSocket(INPORT);
System.out.println("Server started");
}
while(true) {
socket.receive(dp);
String rcvd = Dgram.toString(dp) + ", from address: " +
dp.getAddress() + ",
port: " + dp.getPort();
System.out.println(rcvd);
String echoString = "Echoed: " + rcvd;
DatagramPacket echo = Dgram.toDatagram(echoString,
dp.getAddress(), dp.getPort());
socket.send(echo);
}
} catch(SocketException e) {
System.err.println("Can't open socket");
System.exit(1);
} catch(IOException e) {
System.err.println("Communication error");
e.printStackTrace();
}
}
}
} // main
} // class
Network Programming 5
Prods&Cons: Buffer
import java.util.*; o.writeObject(m);
import java.io.*; } finally {
import java.net.*; appsock.close();
}
public class Buffer { }
public static final int PORT = 8080; else // pronto
public static void main(String[] args) throws IOException, if (msgqueue.isEmpty()) // no available data
ClassNotFoundException { sockqueue.addLast(sock);
LinkedList<Socket> sockqueue = new LinkedList<Socket>(); else { // available data
LinkedList<Message> msgqueue = new LinkedList<Message>(); ObjectOutputStream out = new ObjectOutputStream(
ServerSocket servsock = new ServerSocket(PORT); sock.getOutputStream());
try { Message msg = (Message)msgqueue.removeFirst();
while(true) { out.writeObject(msg);
Socket sock = servsock.accept(); sock.close();
try { }
ObjectInputStream in = new ObjectInputStream( } catch(IOException e) {
sock.getInputStream()); sock.close();
Message m = (Message)in.readObject(); }
if (m.getType() == 0) // dati }
if (sockqueue.isEmpty()) // no waiting cons } finally {
msgqueue.addLast(m); servsock.close();
else { // waiting cons available }
Socket appsock = (Socket)sockqueue.removeFirst(); }
try { }
ObjectOutputStream o = new
ObjectOutputStream(
appsock.getOutputStream());
Prods & Cons&: Producer Prods & Cons: Consumer
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
public class Producer {
public static void main(String[] args) throws IOException, public class Consumer {
ClassNotFoundException {
InetAddress addr = InetAddress.getByName(null); public static void main(String[] args) throws IOException,
Socket socket = new Socket(addr, Buffer.PORT); ClassNotFoundException {
System.out.println("Connected to " + socket); InetAddress addr = InetAddress.getByName(null);
try { Socket socket = new Socket(addr, Buffer.PORT);
ObjectOutputStream out = new ObjectOutputStream( System.out.println("Connected to " + socket);
socket.getOutputStream()); try {
System.out.print("Sending data message... "); ObjectOutputStream out = new ObjectOutputStream(
Message data = new Message(0, "pippo"); socket.getOutputStream());
out.writeObject(data); Message pronto = new Message(1, "ready");
System.out.println("done");
} finally { out.writeObject(pronto);
socket.close(); ObjectInputStream in = new ObjectInputStream(
} socket.getInputStream());
} Message dati = (Message)in.readObject();
System.out.println("received "+ dati);
} } finally {
socket.close();
}
}
}