Java Client - Serveur

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Server (TCP) Client (TCP)

import java.io.*; import java.net.*;


import java.net.*;
import java.io.*;
public class SimpleServer {
public static final int PORT = 8080; public class SimpleClient {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(PORT); InetAddress addr = InetAddress.getByName(null);
System.out.println("Started: " + s); System.out.println("addr = " + addr);
try { Socket socket = new Socket(addr, SimpleServer.PORT);
Socket socket = s.accept(); try {
try {
System.out.println("Connection accepted: "+ socket); System.out.println("socket = " + socket);
BufferedReader in = BufferedReader in =
new BufferedReader( new BufferedReader(
new InputStreamReader( new InputStreamReader(
socket.getInputStream())); socket.getInputStream()));
PrintWriter out = PrintWriter out =
new PrintWriter( new PrintWriter(
new BufferedWriter(
new OutputStreamWriter( new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
while (true) { socket.getOutputStream())),true);
String str = in.readLine();
if (str.equals("END")) break; for(int i = 0; i < 10; i ++) {
System.out.println("Echoing: " + str); out.println(“ciao " + i);
out.println(str); String str = in.readLine();
}
} finally { System.out.println(str);
System.out.println("closing..."); }
socket.close(); out.println("END");
} } finally {
} finally { System.out.println("closing...");
s.close(); socket.close();
} }
} }
}
}

Network Programming 1 Network Programming 2


Classe Dgram Server (UDP)
import java.net.*; import java.net.*;
import java.io.*;
public class Dgram { import java.util.*;
static DatagramPacket toDatagram(String s,
InetAddress destAddr, int destPort) { public class DgramServer {
byte[] buf = s.getBytes(); static final int INPORT = 1711;

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();
}
}
}

Network Programming 3 Network Programming 4


Client (UDP) Prods & Cons: messaggi
import java.net.*; import java.io.*;
import java.io.*;
public class DgramClient {
public static void main(String[] args) { class Message implements Serializable {
DatagramSocket s; private int type;
InetAddress hostAddress; private String body;
byte[] buf = new byte[1000];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
Message(int aType, String aBody) {
try { type = aType;
s = new DatagramSocket(); body = aBody;
hostAddress = InetAddress.getByName("localhost"); }
System.out.println("Client starting");
for(int i = 0; i < 5; i++) {
String outMessage = "Client #" + ", message #" + i; public int getType() {
s.send(Dgram.toDatagram(outMessage, return type;
hostAddress, }
DgramServer.INPORT));
s.receive(dp);
String rcvd = "Client #" + ", rcvd from " + public String getBody() {
dp.getAddress() + ", " + return body;
dp.getPort() + ": " + Dgram.toString(dp); }
System.out.println(rcvd);
}
} catch(UnknownHostException e) { public String toString() {
System.err.println("Cannot find host"); return "Message[type=" + type + ", body=" + body + "]";
System.exit(1); }
} catch(SocketException e) { }
System.err.println("Can't open socket");
e.printStackTrace();
System.exit(1);
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}

} // 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();
}
}
}

You might also like