Академический Документы
Профессиональный Документы
Культура Документы
import java.net.*;
import java.io.*;
public class UDPClient {
public static void main(String args[]) {
DatagramSocket aSocket = null;
try {
aSocket = new DatagramSocket();
byte [] m = args[0].getBytes(); // Conversie mesaj in sir de
octeti.
InetAddress aHost = InetAddress.getByName(args[1]); // Get
hostname.
int serverPort = 6789;
DatagramPacket request =new DatagramPacket(m, args[0].length(),
aHost, serverPort);
aSocket.send(request);
byte[] buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket(buffer,
buffer.length);
aSocket.receive(reply);
System.out.println("Reply: " +
new String(reply.getData(),0,reply.getLength()))
} catch (SocketException e)
{System.out.println("Socket: "+e.getMessage());
} catch (IOException e) {System.out.println("IO: " + e.getMessage());
} finally {if (aSocket != null) aSocket.close();}
}
}
Server UDP
import java.net.*;
import java.io.*;
public class UDPServer{
public static void main(String args[]){
DatagramSocket aSocket = null;
try {
aSocket = new DatagramSocket(6789);
byte[] buffer = new byte[1000];
while (true) {
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
aSocket.receive(request);
DatagramPacket reply =new DatagramPacket(request.getData(),
request.getLength(),request.getAddress(),request.getPort());
aSocket.send(reply);
}
} catch
(SocketException e){System.out.println("Socket: " + e.getMessage());
} catch (IOException e) {System.out.println("IO: " + e.getMessage());
} finally {if(aSocket != null) aSocket.close();}
}
}
Client TCP - I
import java.net.*;
import java.io.*;
public class TCPClient {
public static void main (String args[]) {
Socket s = null;
try {
int serverPort = 7896;
s = new Socket(args[1], serverPort);
DataInputStream in =new DataInputStream(s.getInputStream());
DataOutputStream out =
new DataOutputStream(s.getOutputStream());
out.writeUTF(args[0]); // UTF is a string encoding.
String data =in.readUTF();
System.out.println("Received: "+ data) ;
}
Server TCP. Acceptarea unei conexiuni
import java.net.*;
import java.io.*;
public class TCPServer {
public static void main (String args[]) {
try {
int serverPort = 7896;
ServerSocket listenSocket =
new ServerSocket(serverPort);
while (true) {
Socket clientSocket = listenSocket.accept();
Connection c = new Connection(clientSocket);
}
} catch(IOException e) {
System.out.println("Listen socket:"+e.getMessage());
}
}
}
Server TCP. Tratarea unei conexiuni - I
class Connection extends Thread {
DataInputStream in;
DataOutputStream out;
Socket clientSocket;
public Connection(Socket aClientSocket) {
try {
clientSocket = aClientSocket;
in =new DataInputStream(clientSocket.getInputStream());
out =new DataOutputStream(clientSocket.getOutputStream());
this.start();
} catch(IOException e)
System.out.println("Connection:"+e.getMessage());
}
}
Server TCP. Tratarea unei conexiuni - II
public void run() {
try { // An echo server.
String data =
in.readUTF();
out.writeUTF(data);
} catch (EOFException e) {
System.out.println("EOF:"+e.getMessage());
} catch(IOException e) {
System.out.println("readline:"+e.getMessage());
} finally {
try {
clientSocket.close();
}
catch(IOException e) { /* close failed */ }
}
}
}
Program Java pentru trimitere multipla IP – I
import java.net.*;
import java.io.*;
public class MulticastPeer {
public static void main(String args[]) {
MulticastSocket s = null;
try {
InetAddress group =InetAddress.getByName(args[1]);
s = new MulticastSocket(6789);
s.joinGroup(group);
byte [] m = args[0].getBytes();
DatagramPacket messageOut =
new DatagramPacket(m, m.length, group, 6789);
s.send(messageOut);
System.out.println("sent");
http://localhost:8080/mywebapp/PersonalServlet?FirstName=<v
aloare>
Exemplu de miniserver
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet {
XML:
<client>
<nume><prenume>Costin</prenume>
<initiala_tata>P.</initiala_tata>
<familie>Badica</familie>
<porecla>Costu</porecla>
</nume>
<telefon>0251-145190</telefon>
<companie limba="romana">
Universitatea din Craiova
</companie>
<contact type="prim">
<data>20 Ian 1995</data>
</contact>
<contact type="ultim">
<data>19 Dec 2002</data>
</contact>
<atitudine interes="potrivit"/>
<personal> Ii place fotbalul
<data>23 Nov 1995</data> Ii
place mancarea italiana
</personal>
</client>
Remember that the SOAP only handles data in the form of XML.
REST data types:
• JSON @Produces("application/json")
• XML @Produces("application/xml")
• HTML @Produces("text/html")
The actual Web Service
Create a text file with the extension "*.jws" with the java code of the Web
Service
and drop it in the Web Content file of the "axis" dynamic project , or in the root of
the web application.
The HelloWorld.jws source file:
public class HelloWorld {
public HelloWorld() {
}
public String getHelloWorldMessage(String name, String surname) {
return "Hello World to" + name+" "+surname;
}
}
try{
//remember to replace localhost with the IP of the service provider (server)
//remember to use the port of your particular Tomcat installation
//this invokes HelloWorld service, change the URL to invoke other services
String endpointURL = "http://localhost:8080/axis/HelloWorld.jws";
org.apache.axis.client.Service service = new Service();// requires jars from axis
org.apache.axis.client.Call call = (Call) service.createCall();
call.setTargetEndpointAddress (new URL (endpointURL));
call.setOperationName ("getHelloWorldMessage");
// getHelloWorldMessage has two String parameters
String result = (String) call.invoke (new Object[] { "John", "James" } );
System.out.println (result);
}
catch (Exception e) { }
Crearea unui agent (II)
import jade.core.Agent;
import java.util.Iterator;
public class SimpleAgent extends Agent {
protected void setup() {
// Printout a welcome message
System.out.println("Hello World. I’m an agent!");
System.out.println(
"My local-name is "+getAID().getLocalName());
System.out.println("My GUID is "+getAID().getName());
System.out.println("My addresses are:");
Iterator it = getAID().getAllAddresses();
while (it.hasNext()) {
System.out.println("- "+it.next());
}
}
}
Terminarea unui agent
import jade.core.Agent;
public class TerminateAgent extends Agent {
protected void setup() {
// Printout a welcome message
System.out.println("Hello World. I am an agent!");
System.out.println(
"My local-name is "+getAID().getLocalName());
System.out.println("My GUID is "+getAID().getName());
doDelete();
}
protected void takeDown() {
System.out.println("Bye World. I am going off-line ...");
}
}
Transmiterea de parametrii unui agent
import jade.core.Agent;
public class AgentWithArgs extends Agent {
protected void setup() {
// Printout a welcome message
System.out.println("Hello World. I am an agent!");
System.out.println("My local-name is "+getAID().getLocalName());
System.out.println("My GUID is "+getAID().getName());
System.out.println("My arguments are:");
Object[] args = getArguments();
if (args != null) {
for (int i = 0; i < args.length; ++i) {
System.out.println("- "+args[i]);
}
}
}
}
Comportament generic
public class ThreeStepBehaviour extends Behaviour {
private int step = 0;
public void action() {
switch (step) {
case 0:
// perform operation X
step++; break;
case 1:
// perform operation Y
step++; break;
case 2:
// perform operation Z
step++; break;
}
}
public boolean done() {
return step == 3;
}
}
Definiţi SD sincrone.
Timpul necesar executiei unui pas de calcul in cadrul fiecarui proces si intarzierile de transmitere a mesajelor
sunt marginite si limita superiara a lor este cunoascuta deasemenea fiecare proces are un ceas local a carei rata
de deplasare fata de o referinta standard este cunoscuta.
Definiţi SD asincrone.
Timpul necesar executiei unui pas de calcul in fiecare proces, intarzierile de transmitere a mesajelor si
deplasamentele ceasurilor locale fata de o referinta standard sunt finite si arbitrare.