i have created the socket, checked if the socket is ok, binded the socket, checked if the binding is ok, listen for connection, checked if the listning is ok, and accept connection and check if the accepting is ok.
now i want to send my html file thats on my filesystem through the socket and i do not know how to go about that. i know i have to accept read then write but i do not know how to put that in practice.Code:#include<stdio.h> #include<stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define PEND 10 int main(int argc, char *argv[]) { /* listen on listen_fd, new connection on new_fd */ int listen_fd, new_fd; /* my address information, address where I run this program */ struct sockaddr_in my_addr; /* remote address information */ struct sockaddr_in there_addr; int size; int port; if (argc < 2) { printf(stderr,"ERROR, no port provided\n"); exit(1) } lisen_fd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd == -1) { perror("socket error!"); exit(1); } else printf("socket is OK\n"); /* pass port through command line */ port = atoi(argv[1]); /* host byte order */ my_addr.sin_family = AF_INET; /* network byte order */ my_addr.sin_port = htons(port); /* use my IP address */ my_addr.sin_addr.s_addr = INADDR_ANY; memset(&(my_addr.sin_zero), 0, 8); if(bind(listen_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind error!"); exit(1); } else printf("bind is OK...\n"); if(listen(listen_fd, PEND) == -1) { perror("listen error!"); exit(1); } else printf("listen is OK...\n"); size = sizeof(struct sockaddr_in); new_fd = accept(listen_fd, (struct sockaddr *)&there_addr, &size); if(new_fd == -1) perror("accept error!"); else printf("accept is OK...\n"); close(new_fd); close(listen_fd); return 0; }



2Likes
LinkBack URL
About LinkBacks


