Da ieri il tuo framework è diventata la mia Bibbia, la mia linfa informatica. Con fread e fwrite è possibile trasmettere qualsiasi tipo di file?
Mi consigli qualche libro?
Comunque in tutto questo volevo fare comunicare due processi nella rete.
Il server crea un file vuoto e ci copia quello che manda il client.
---SERVER---
Codice: Seleziona tutto
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#define BACKLOG 5
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
FILE *fp;
int sockfd, new_fd, port, n;
char *nameBuff;
char *recvBuff;
char *path;
struct sockaddr_in serv_addr, clin_addr;
if(argc<2)
{
fprintf(stderr,"MANCA: numero porta, nome file\n");
exit(1);
}
if(argc<3)
{
fprintf(stderr,"MANCA: nome file\n");
exit(1);
}
port=atoi(argv[1]);
nameBuff=argv[2];
/* New File Empty */
path = getenv("HOME");
strcat(path, "/Desktop/");
strcat(path, nameBuff);
//printf("PATH = %s",path);
fp=fopen(path, "wb");
if(fp<0) error("file");
/* Socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) error("socket");
/* Inizializzazione struct socket */
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
/* Bind */
int b;
b=bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if(b<0) error("bind");
/* Listen */
int l;
l=listen(sockfd, BACKLOG);
if(l<0) error("listen");
int sin_size;
sin_size = sizeof(clin_addr);
printf("SERVER ATTIVO\n");
/* Accept */
int flag = 1;
while(flag)
{
new_fd = accept(sockfd, (struct sockaddr *)&clin_addr , &sin_size);
if(new_fd<0) error("accept");
printf("HOST: %d connesso\n",new_fd);
/* recv len file */
char len[20];
read(new_fd, len, 19 );
printf("LEN = %s\n", len);
recvBuff = (char *)malloc(sizeof(char)*atoi(len));
if(recvBuff == NULL) error("malloc");
//fprintf(fp,"%c",new_fd);
int rec;
int ar[atoi(len)];
printf("RICEZIONE FILE...\n");
rec = read(new_fd ,ar, 19);
if(rec<0) error("read");
rec = fwrite(ar,1,19,fp);
if(rec<0) error("write");
fclose(fp);
close(new_fd);
flag=1;
}
close(sockfd);
exit(0);
}
---CLIENT---
Codice: Seleziona tutto
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
FILE *fp;
int sockfd,port, n;
struct hostent *server;
struct sockaddr_in clin_addr;
char *sendBuff;
if(argc<2)
{
fprintf(stderr,"MANCA: nome file, indirizzo IP, numero porta\n");
exit(1);
}
if(argc<3)
{
fprintf(stderr,"MANCA: indirizzo IP, numero porta\n");
exit(1);
}
if(argc<4)
{
fprintf(stderr,"MANCA: numero porta\n");
exit(1);
}
port=atoi(argv[3]);
/* Socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0) error("socket");
/* Controllo server */
server = gethostbyname(argv[2]);
if(server == NULL) error("ERROR opening server");
/* inizializzazione struct socket */
bzero((char *)&clin_addr, sizeof(clin_addr));
bcopy((char *)server->h_addr, (char *)&clin_addr.sin_addr.s_addr,server->h_length);
clin_addr.sin_family = AF_INET;
clin_addr.sin_port = htons(port);
/* Connessione */
int c;
c = connect(sockfd, (struct sockaddr *)&clin_addr, sizeof(clin_addr));
if(c<0) error("connect");
/* Invio File */
char len[20];
int l;
/* Lettura File */
fp = fopen(argv[1], "rb");
if(fp==NULL) error("ERROR opening file");
fseek(fp, 0, SEEK_END);
l = ftell(fp);
char buffer[20];
sprintf(len, "%d",l);
send(sockfd, len, 19, 0);
rewind(fp);
sendBuff = (char *)calloc(l,sizeof(char));
if(sendBuff == NULL)error("malloc");
/* send file */
int ar[l];
int ch, i=0;
n = fread(ar,1,l,fp);
if(n<0) error("fread");
/* while(1) */
/* { */
/* ch=fgetc(fp); */
/* if(feof(fp))break; */
/* *(sendBuff+i)=ch; */
/* i++; */
/* } */
n = send(sockfd, ar, l, 0);
if(n<0) error("send");
fclose(fp);
close(sockfd);
exit(0);
}
« Una volta eliminato l'impossibile, ciò che resta, per quanto improbabile, deve essere la verità. »
(Sherlock Holmes)