c++ sockets
Written by E. Lan to provide a mechanism for writing cross platform socket code. This library was originally written to only support blocking TCP sockets. Over the years it has been extended to support UDP and RAW sockets as well. This is the first official release of the library and the following functionality is supported:
* Cross platform socket support.
o Windows 95, Windows 98, Windows XP, Windows 8, Windows 10
o Linux, Unix
o Macintosh OSX
* Support for sychronious, and asychronious sockets
* Supports TCP Streams
* Supports UDP Datagrams
* Supports Raw Sockets
* Thread Safe
* Signal Safe
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | //============================================================================ // Name : SendPacket.cpp // Author : elan // Version : // Copyright : Socket // Description : Ansi-style //============================================================================ #include<iostream> using namespace std; #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include <sys/sendfile.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/stat.h> #include <netinet/in.h> #include <arpa/inet.h> int main(int argc, char **argv) { int port = 5000; /* port number to use */ int sock; /* socket desciptor */ int desc; /* file descriptor for socket */ int fd; /* file descriptor for file to send */ struct sockaddr_in addr; /* socket parameters for bind */ struct sockaddr_in addr1; /* socket parameters for accept */ int addrlen; /* argument to accept */ socklen_t clientLen; struct stat stat_buf; /* argument to fstat */ off_t offset = 0; /* file offset */ char filename[1212]; /* filename to send */ int rc; /* holds return code of system calls */ /* check command line arguments, handling an optional port number */ if (argc == 2) { port = atoi(argv[1]); if (port <= 0) { fprintf(stderr, "invalid port: %s ", argv[1]); exit(1); } } else if (argc != 1) { fprintf(stderr, "usage: %s [port] ", argv[0]); exit(1); } cout << "create Internet domain socket"<<endl; /* create Internet domain socket */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { fprintf(stderr, "unable to create socket: %s ", strerror(errno)); exit(1); } cout << "fill in socket structure"<<endl; /* fill in socket structure */ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("192.168.116.128"); //INADDR_ANY; addr.sin_port = htons(port); cout << "bind socket to the port "<< INADDR_ANY <<endl; /* bind socket to the port */ rc = bind(sock, (struct sockaddr *)&addr, sizeof(addr)); if (rc == -1) { fprintf(stderr, "unable to bind to socket: %s ", strerror(errno)); exit(1); } /* listen for clients on the socket */ rc = listen(sock, 1); if (rc == -1) { fprintf(stderr, "listen failed: %s ", strerror(errno)); exit(1); } while (1) { cout << "wait for a client to connect"<<endl; /* wait for a client to connect */ desc = accept(sock, (struct sockaddr *) &addr1, &clientLen); if (desc == -1) { fprintf(stderr, "accept failed: %s ", strerror(errno)); exit(1); } cout << "get the file name from the client "<<endl; /* get the file name from the client */ rc = recv(desc, filename, sizeof(filename), 0); if (rc == -1) { fprintf(stderr, "recv failed: %s ", strerror(errno)); exit(1); } /* null terminate and strip any and from filename */ filename[rc] = '�'; if (filename[strlen(filename)-1] == ' ') filename[strlen(filename)-1] = '�'; if (filename[strlen(filename)-1] == ' ') filename[strlen(filename)-1] = '�'; /* exit server if filename is "quit" */ if (strcmp(filename, "quit") == 0) { fprintf(stderr, "quit command received, shutting down server "); break; } fprintf(stderr, "received request to send file %s ", filename); /* open the file to be sent */ fd = open(filename, O_RDONLY); if (fd == -1) { fprintf(stderr, "unable to open '%s': %s ", filename, strerror(errno)); exit(1); } /* get the size of the file to be sent */ fstat(fd, &stat_buf); /* copy file using sendfile */ offset = 0; rc = sendfile (desc, fd, &offset, stat_buf.st_size); if (rc == -1) { fprintf(stderr, "error from sendfile: %s ", strerror(errno)); exit(1); } if (rc != stat_buf.st_size) { fprintf(stderr, "incomplete transfer from sendfile: %d of %d bytes ", rc, (int)stat_buf.st_size); exit(1); } /* close descriptor for file that was sent */ close(fd); /* close socket descriptor */ close(desc); } /* close socket */ close(sock); return 0; } |