Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/socket.h>
- #include <netinet/ip.h>
- #include <string.h>
- #include <stdlib.h>
- static int bind_server_socket(int fd, int port);
- /*
- * create a server socket bound to port
- * and listening.
- *
- * return positive file descriptor
- * or negative value on error
- */
- int create_server_socket(int port)
- {
- int fd = -1;
- if(port < 0 || port > 65535) {
- errno = EINVAL;
- return -1;
- }
- fd = socket(AF_INET,SOCK_STREAM,0);
- if(fd < 0) return fd;
- if(bind_server_socket(fd,port) < 0) return -1;
- if(listen(fd,10)) return -1;
- return fd;
- }
- static int bind_server_socket(int fd, int port){
- struct sockaddr_in addr;
- int val = 1;
- if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))) {
- perror("setsockopt");
- return -1;
- }
- /* see man page ip(7) */
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- addr.sin_addr.s_addr = INADDR_ANY;
- if(bind(fd, (struct sockaddr*) &addr, sizeof(addr))) return -1;
- #ifdef INFO
- printf("simple_tcp_server: bound fd %d to port %d\n",fd,port);
- #endif
- return fd;
- }
- /*
- * Serve one client: send a message and close the socket
- */
- static int do_serve(int fd)
- {
- int clientfd;
- if((clientfd = accept(fd, NULL, NULL)) < 0) return -1;
- #ifdef INFO
- printf("simple_tcp_server: writing msg (len=%lu) to clientfd (%d)\n",len,clientfd);
- #endif
- #ifdef WRITE_LOOP
- size_t written = 0;
- do {
- int res = write(clientfd, msg, len);
- if (res < 0) {
- perror("write to clientfd");
- goto error;
- }
- #ifdef INFO
- printf("simple_tcp_server: write returned %d\n",res);
- #endif
- written += res;
- } while (written < len);
- #else
- {
- while(1==1) {
- /*char run = 1;
- FILE *fileptr;
- char *buffer;
- int filelen;
- int curr = 1;
- char *url = ("/Users/axeldelbom/Downloads/samplevideo/");
- while(run) {
- char *fileUrl = malloc(strlen(url) + 13);
- char *num = malloc(4);
- sprintf (num, "test%04d.jpg", curr);
- strcpy(fileUrl,url);
- strcat(fileUrl,num);
- fileptr = fopen(fileUrl,
- "rb"); // Open the file in binary mode
- fseek(fileptr, 0, SEEK_END); // Jump to the end of the file
- filelen = ftell(fileptr); // Get the current byte offset in the file
- rewind(fileptr); // Jump back to the beginning of the file
- uint32_t filelenWeb = htonl(filelen);
- int written = write(clientfd, &(filelenWeb), 4);
- buffer = (char *) malloc(1024 * sizeof(char) +1 ); // Enough memory for file + \0
- int total = 0;
- int sent = 0;
- int target = 1024;
- while(1)
- {
- total = total + fread(buffer,1,1024,fileptr);
- sent += write(clientfd, buffer, target);
- if(filelen - sent < 1024){
- target = filelen - sent;
- }
- if(total >= filelen){
- break;}
- } // Read in the entire file
- fclose(fileptr); // Close the file
- curr++;
- if(curr > 711) curr = 1;
- }*/
- char *text = "Hello\n";
- write(clientfd, text, strlen(text));
- sleep(2);
- }
- }
- #endif
- error:
- printf("simple_tcp_server: closing clientfd (%d)\n",clientfd);
- return close(clientfd);
- }
- int main()
- {
- int fd = create_server_socket(8888);
- if(fd < 0){
- perror("create_server_socket");
- return 1;
- }
- while(1) {
- do_serve(fd);
- }
- printf("simple_tcp_server: closing socket: %d\n", fd);
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement