Dsy Network www | forum | my | didattica | howto | wiki | el goog | stats | blog | dona | rappresentanti
Homepage
 Register   Calendar   Members  Faq   Search  Logout 
.dsy:it. : Powered by vBulletin version 2.3.1 .dsy:it. > Didattica > Corsi N - Z > Reti di calcolatori > Soluzione lab client server febbraio 2014
  Last Thread   Next Thread
Author
Thread    Expand all | Contract all    Post New Thread    Post A Reply
Collapse
Vagabond
.primate.

User info:
Registered: Apr 2013
Posts: 74 (0.02 al dì)
Location:
Corso: F1X
Anno:
Time Online: 11:57:29 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Soluzione lab client server febbraio 2014

Posto qui un mio tentativo di soluzione di questo tema d'esame. Premetto che è il primo tentativo di soluzione che ho fatto, è incompleto e probabilmente sbagliato. Se qualcuno ha voglia di dargli un'occhiata ci si può ragionare insieme e capire cosa va e cosa non va. Sono bene accetti commenti sia da parte di chi sa farlo e vuole dare una mano, sia da parte di chi, come me, sta cercando di fare l'esame ed è in difficoltà.

La parte della codifica non l'ho implementata perchè non ho ben capito cosa si dovesse fare e perchè non è quella la parte che mi interessava, ma quello che voglio capire è se la struttura della gestione client server va fatta così.

Parte Server C:

#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>

#define MYPORT "55555"
#define BACKLOG 10

int main(){
int distr = 1;
int prezzo = 9999;

struct sockaddr_storage theiraddr;
socklen_t addr_size;
struct addrinfo hints, *res;
int sk_fd, newfd, pid;

memset(&hints, 0, sizeof hints);
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AI_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

getaddrinfo(NULL, MYPORT, &hints, &res);

sk_fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
bind(sk_fd, res->ai_addr, res->ai_addrlen);
listen(sk_fd, BACKLOG);

addr_size = sizeof(theiraddr);

for(;;){
newfd = accept(sk_fd, (struct sockaddr*) &theiraddr, &addr_size);
pid=fork();

if(pid != 0){
pid = waitpid(0,&rs,NULL);
result = WEXITSTATUS(rs);
//manca la decodifica e l'aggiornamento delle variabili

close(newfd);
}
else{
close(sk_fd);
int bytes;
char[6] msg;
char[0]=distr;
char[2]=prezzo;
bytes = send(newfd, msg, sizeof(msg),0);

while(true){
char[6] rec;
int rs;
int rbytes = recv(newfd, rec, sizeof(rec), 0);
if(rec == "exit"){
close(newfd);
exit(rs);
}
rs = strtol(rec);
//manca la codifica e l'aggiornamento delle variabili
}
}
}

return 0;
}

Attachment: serverbenzina.c
This has been downloaded 8 time(s).

17-12-2014 16:20
Click Here to See the Profile for Vagabond Click here to Send Vagabond a Private Message Find more posts by Vagabond Add Vagabond to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Vagabond
.primate.

User info:
Registered: Apr 2013
Posts: 74 (0.02 al dì)
Location:
Corso: F1X
Anno:
Time Online: 11:57:29 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Parte client java:

Client:
import java.net.Socket;


public class Client {

public static void main(String[] args) {

int porta = 55555;
String hostname = "local_host";

if(args.length == 1){
hostname = args[0];
}

try {
Socket sock = new Socket(hostname, porta);
new Servizio(sock);
sock.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}


Servizio:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

public class Servizio {

public Servizio(Socket sock) throws IOException {

BufferedReader fromServer = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedReader fromUser = new BufferedReader(new InputStreamReader(System.in));
OutputStream toServer = sock.getOutputStream();

String stringa = fromServer.readLine();
stringa += "/r/n";
System.out.println("Il distributore più conveniente è " + stringa);
String newstringa = "";

while (true) {
stringa = fromServer.readLine();
stringa += "/r/n";
System.out.println("Il distributore più conveniente è " + stringa);

System.out.println("Inserire nuovo distributore e prezzo");
newstringa = fromUser.readLine();
if(newstringa=="exit")
break;
newstringa += "/r/n";
toServer.write(newstringa.getBytes(), 0, newstringa.length());

try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

Attachment: servizio.java
This has been downloaded 2 time(s).

17-12-2014 16:22
Click Here to See the Profile for Vagabond Click here to Send Vagabond a Private Message Find more posts by Vagabond Add Vagabond to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Vagabond
.primate.

User info:
Registered: Apr 2013
Posts: 74 (0.02 al dì)
Location:
Corso: F1X
Anno:
Time Online: 11:57:29 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

Ho allegato i file perchè copiando il codice la formattazione sparisce e diventa incomprensibile.

Attachment: client.java
This has been downloaded 5 time(s).

17-12-2014 16:23
Click Here to See the Profile for Vagabond Click here to Send Vagabond a Private Message Find more posts by Vagabond Add Vagabond to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
Alew92
.simpatizzante.

User info:
Registered: Mar 2012
Posts: 11 (0.00 al dì)
Location:
Corso: informatica
Anno: 1
Time Online: 5:20:22 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged

mmm per quanto riguarda la parte server non dovresti chiamare anche la sigaction?

29-12-2014 17:40
Click Here to See the Profile for Alew92 Click here to Send Alew92 a Private Message Find more posts by Alew92 Add Alew92 to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
All times are GMT. The time now is 13:16.    Post New Thread    Post A Reply
  Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread | Add to Bookmarks

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
 

Powered by: vBulletin v2.3.1 - Copyright ©2000 - 2002, Jelsoft Enterprises Limited
Mantained by dsy crew (email) | Collabora con noi | Segnalaci un bug | Archive | Regolamento | Licenze | Thanks | Syndacate
Pagina generata in 0.031 seconds (72.60% PHP - 27.40% MySQL) con 28 query.