All pastes #564428 Raw Edit

SOE Lab8 sv.c

public c v1 · immutable
·published 2007-06-13 16:16 UTC
rendered paste body
#include <sys/types.h>#include <sys/socket.h>#include <pthread.h>#include <string.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <sys/stat.h>#include <fcntl.h>#include <netinet/in.h>#define BUFSIZE 128void *worker(void *arg){    int socket=*(int *)arg;    char buf[BUFSIZE];    unsigned int count = 0;    char *filename = 0;    while (1) {	ssize_t ret;	ret = read(socket,buf+count,BUFSIZE-count);	printf("Read ret = %d\n",ret);                                                 fflush(stdout);   	if (ret <= 0)	    goto out;	if (ret > 0)	    count+=ret;	if (count > 0) 	    if (memchr(buf,0,count)!=0) {		printf("got fn=(%s),count=%d\n",buf,count);                                             		fflush(stdout);   		filename = (char *)malloc(strlen(buf));		if (filename==0)		    goto out;		strcpy(filename,buf);		break;	    }	if (count == BUFSIZE)	    goto out;	    }        int ret = htonl(-1);    assert(BUFSIZE>(20+sizeof(int)));        memset(buf+sizeof(int),0,20); 	        if (access(filename,R_OK) == 0) {	struct stat st;	int fd;		if (stat(filename,&st) == 0)	    ret = htonl(st.st_size);		if ((fd = open(filename,O_RDONLY)) != -1) {	    read(fd,buf+sizeof(int),20);	    close (fd);	}    }        printf("File access = %d\n",ntohl(ret));    fflush(stdout);        memcpy(buf,&ret,sizeof(int));    sleep(3);    int tosend = 20+sizeof(int);    printf("Tosend = %d\n",tosend);    fflush(stdout);    while (tosend>0) {	int retval = write(socket,buf+((20+sizeof(int))-tosend),tosend);	printf("Write retval = %d\n",retval);	fflush(stdout);	if (retval < 0)	    goto out;	tosend-=retval;        }    out:    printf("Thread bye\n");    fflush(stdout);        close(socket);    if (filename)	free(filename);    free(arg);	    return 0;}int main (int argc, char **argv) {    int sock;        if ((argc != 2)||(atoi(argv[1])==0)) {	fprintf(stderr,"Uzycie: %s port\n",argv[0]);	return -1;    }            sock = socket(PF_INET6,SOCK_STREAM,0);    if (sock == -1){	perror("socket");	return -1;    }    do {	int opt=1;	if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(int))!=0) {	    perror("setsockopt");	    return -1;	}    } while(0);        do {	struct sockaddr_in6 sa;	memset(&sa,0,sizeof(sa));//	sa.sin6_len = sizeof(sa);	sa.sin6_family = AF_INET6;	sa.sin6_port = htons(atoi(argv[1]));	sa.sin6_addr = in6addr_any;		if (bind(sock,(struct sockaddr *) &sa,sizeof(sa))!=0) {	    perror("bind");	    return -1;	}    } while(0);        if (listen(sock,5)!=0) {	    perror("listen");	    return -1;    }        while (1) {	int newsock = accept(sock,0,0);	if (newsock == -1) {	    perror("accept");	    return -1;		}		printf("New connection\n");	fflush(stdout);			int *arg = malloc(sizeof(int));	if (arg == 0) {	    perror("malloc");	    close(newsock);	    continue;	}	*arg=newsock;		pthread_t tid;	if (pthread_create(&tid,0,worker,arg)!=0) {	    perror("pthread_create");	    close(newsock);	    free(arg);	    continue;	}	printf("Tid %d\n",tid);	fflush(stdout);    }    return 0;}