.dsy:it.
Show 150 posts per page

.dsy:it. (http://www.dsy.it/forum/)
- Reti di calcolatori (http://www.dsy.it/forum/forumdisplay.php?forumid=68)
-- Soluzione lab client server febbraio 2014 (http://www.dsy.it/forum/showthread.php?threadid=43889)


Posted by Vagabond on 17-12-2014 16:20:

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


Posted by Vagabond on 17-12-2014 16:22:

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

}


Posted by Vagabond on 17-12-2014 16:23:

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


Posted by Alew92 on 29-12-2014 17:40:

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


All times are GMT. The time now is 03:49.
Show all 4 posts from this thread on one page

Powered by: vBulletin Version 2.3.1
Copyright © Jelsoft Enterprises Limited 2000 - 2002.