#include #include #include #include #include #include #include #include #define PORT 5000 int serve (int sd); void main () { int sd; struct sockaddr_in server; struct hostent * server_host; char buf[1024]; if ((sd=socket(AF_INET,SOCK_STREAM,0))<0) { fprintf(stderr,"client cannot create socket\n"); perror(0); exit(1); } if ((server_host=gethostbyname("serifos"))==NULL) { fprintf(stderr,"unkown host\n"); exit(1); } server.sin_family=AF_INET; server.sin_port=htons(PORT); bcopy((char *)server_host->h_addr,(char *)&server.sin_addr,server_host->h_length); if (connect(sd, &server, sizeof(server)) <0) { fprintf(stderr, "cannot connect to server\n"); perror(0); exit(1); } printf("client requesting permission\n"); read(sd,&buf,strlen(buf)); if (!(strcmp(buf,"accept"))) { printf("Connection established\n"); serve(sd); } if (!(strcmp(buf,"quit"))) printf("connection refused by user\n"); close(sd); exit(0); } int serve(int sd) { char c; FILE *ifp; ifp=fopen("sendme","r"); if (ifp==NULL) { printf("sfalma sto anoigma arxeiou\n"); return -1; } c=getc(ifp); printf("sending...\n"); while (c!=EOF) { write(sd,&c,sizeof(char)); c=getc(ifp); } printf("file sent\n"); fclose(ifp); return 0; }