All pastes #854 Raw Edit

Anonymous

public c v1 · immutable
#854 ·published 2004-09-04 05:17 UTC
rendered paste body
/*I'm trying to pass a 100000 double array through a unix domain socketwhen the size is less than small, it works, but at anything largerand only the first 8192 bytes are read. How do I transfer it all?I know I can adjust the buffer size manually, but that sounds non-portable and I'm sure there's a better way anyway.Do I have to split up the array into chunks by pwrite and pread?or is there something wrong intrinsically with the code?*//* server1.c */#include <sys/types.h>#include <sys/socket.h>#include <stdio.h>#include <sys/un.h>#include <unistd.h>#define DATASIZE 100000int main(){    int server_sockfd, client_sockfd;    int server_len, client_len;    struct sockaddr_un server_address;    struct sockaddr_un client_address;       /* Remove any old socket and create an unnamed socket for the server. */    unlink("server_socket");    server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);    /*  Name the socket.  */    server_address.sun_family = AF_UNIX;    strcpy(server_address.sun_path, "server_socket");    server_len = sizeof(server_address);    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);    /*  Create a connection queue and wait for clients.  */    listen(server_sockfd, 5);    while(1) {	int bytesread;        double * data = (double*) malloc (sizeof(double)*DATASIZE);        printf("server waiting\n");	/*  Accept a connection.  */        client_len = sizeof(client_address);        client_sockfd = accept(server_sockfd,             (struct sockaddr *)&client_address, &client_len);	/*  We can now read/write to client on client_sockfd.  */        bytesread=write(client_sockfd, data, DATASIZE);	printf("bytes written: %d\n", bytesread);        close(client_sockfd);	free(data);    }}/* client1.c */#include <sys/types.h>#include <sys/socket.h>#include <stdio.h>#include <sys/un.h>#include <unistd.h>#define DATASIZE 100000int main(){    int sockfd;    int len;    struct sockaddr_un address;    int result;    int bytesread;    double * data = (double*) malloc (sizeof(double)*DATASIZE);    /*  Create a socket for the client.  */    sockfd = socket(AF_UNIX, SOCK_STREAM, 0);    /*  Name the socket, as agreed with the server.  */    address.sun_family = AF_UNIX;    strcpy(address.sun_path, "server_socket");    len = sizeof(address);    /*  Now connect our socket to the server's socket.  */    result = connect(sockfd, (struct sockaddr *)&address, len);    if(result == -1) {        perror("oops: client1");        exit(1);    }    /*  We can now read/write via sockfd.  */    bytesread = read(sockfd, data, DATASIZE);    printf("bytes read: %d\n",bytesread);    close(sockfd);    free(data);    exit(0);}/* When compiled with DATASIZE as 100000 freebsd% gcc server1.c -o server; gcc client1.c -o client;(server in one term)freebsd% ./server                                         server waitingbytes written: -1(console in another term)freebsd% ./clientbytes read: 8192*//* When compiled with DATASIZE as 100 freebsd% gcc server1.c -o server; gcc client1.c -o client;(server in one term)freebsd% ./server                                         server waitingbytes written: 100(console in another term)freebsd% ./clientbytes read: 100*/