c - Client server implementation -


after research found code server side.

#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h>  int main(void) {   int listenfd = 0,connfd = 0;    struct sockaddr_in serv_addr;    char sendbuff[1025];     int numrv;      listenfd = socket(af_inet, sock_stream, 0);   printf("socket retrieve success\n");    memset(&serv_addr, '0', sizeof(serv_addr));   memset(sendbuff, '0', sizeof(sendbuff));    serv_addr.sin_family = af_inet;       serv_addr.sin_addr.s_addr = htonl(inaddr_any);    serv_addr.sin_port = htons(5000);        bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));    if(listen(listenfd, 10) == -1){       printf("failed listen\n");       return -1;   }     while(1)     {        connfd = accept(listenfd, (struct sockaddr*)null ,null); // accept awaiting request        strcpy(sendbuff, "message server");       write(connfd, sendbuff, strlen(sendbuff));        close(connfd);           sleep(1);     }     return 0; } 

the thing server, want send file client, in chunks of 256 bytes.

what confusing me in code above, can see accept call made inside loop. way it? if should in loop, put logic of consecutively reading file, , sending in chunks? seems need embed loop inside while loop - after accept call?

seems need embed loop inside while loop - after accept call?

yes -- you'll need keep calling write() until bytes have been sent. keep in mind write() may write fewer bytes asked to, you'll need keep track of write()'s return value know arguments pass next write() call.

the example code ignoring return value of write() call, can sort of away when you're writing small number of bytes, isn't you'd want in real code.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -