All pastes #901119 Raw Edit

client.c

public c v1 · immutable
·published 2008-02-12 12:42 UTC
rendered paste body
#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#define PORT 5000int 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;}