Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <math.h>
- #include <windows.h>
- #include <winsock.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <io.h>
- #include <fstream>
- #include <vector>
- #define MAX_CONNECTION 100
- #ifndef max
- #define max(a,b) ((a) < (b) ? (a) : (b))
- #endif
- typedef struct connection{
- char ipaddr[16];
- int port;
- int sd;
- } connection;
- static unsigned short SERVER_PORT = 4118;
- int main(int argc, char* argv[])
- {
- int maxfd =-1;
- fd_set rset, allset;
- connection client[MAX_CONNECTION];
- int passiveSock; /* Main Server Socket */
- struct sockaddr_in servSock_in;
- #ifdef WIN32
- WSADATA wsaData;
- WSAStartup(0x0101, &wsaData);
- #endif
- int port;
- if (argc > 1)
- port = atoi(argv[1]);
- else
- port = SERVER_PORT;
- for(int i=0; i < MAX_CONNECTION ; i++)
- client[i].sd = -1;
- memset((char *)&servSock_in, 0, sizeof(servSock_in));
- servSock_in.sin_family = PF_INET;
- servSock_in.sin_addr.s_addr = htonl(INADDR_ANY);
- servSock_in.sin_port = htons((u_short)port);
- passiveSock = socket(PF_INET, SOCK_STREAM, 0);
- if (passiveSock < 0) {
- fprintf(stderr, "I am too tired... I failed to open gate...n");
- return -1;
- }
- if (bind(passiveSock, (struct sockaddr *)&servSock_in, sizeof(servSock_in)) < 0){
- fprintf(stderr, "I couldn't attach gate to port...n");
- return -1;
- }
- if (listen(passiveSock, 5) < 0) {
- fprintf(stderr, "I am not hearing anything...n");
- return -1;
- }
- FD_SET(passiveSock, &allset);
- maxfd = max(maxfd, passiveSock);
- struct sockaddr_in cliSock_in;
- int cliSockLen;
- int connectedSock;
- cliSockLen = sizeof(cliSock_in);
- printf("n Waiting for an incoming connection at port number %d", port);
- int bytesread = 0;
- for (;;)
- {
- //FD_ZERO(&allset);
- rset = allset;
- int nready = select(maxfd+1,&rset, NULL, NULL, NULL);
- if(FD_ISSET(passiveSock, &rset)){
- printf("In the first if");
- connectedSock = accept(passiveSock, (struct sockaddr *)&cliSock_in, &cliSockLen);
- /* if an error occurs while accepting */
- if (connectedSock == -1) {
- printf("n Server: Accept error (errno = %d: %s)n", errno, strerror(errno));
- continue;
- }
- for (int i=0; i<MAX_CONNECTION; i++)
- if (client[i].sd < 0){
- client[i].sd=connectedSock;
- strcpy(client[i].ipaddr, inet_ntoa(cliSock_in.sin_addr));
- client[i].port= ntohs(cliSock_in.sin_port);
- printf("n Server: connection established with %s:%dn",
- client[i].ipaddr, client[i].port);
- break;
- }
- FD_SET(connectedSock, &allset);
- maxfd = max(maxfd, connectedSock);
- }
- else{
- for(int j = 0 ; j < MAX_CONNECTION; j++){
- connectedSock = client[j].sd;
- printf("connectedSock is %d", connectedSock);
- if(connectedSock < 0)
- continue;
- if(FD_ISSET(client[j].sd, &rset)){
- unsigned char buffer[66000];
- int index = 0;
- bytesread = recv(connectedSock, (char *)buffer, 66000, 0);
- int type;
- type = (buffer[0] & 0xE0) >> 5;
- if (type == 0)
- {
- char fname[100];
- int i = 0;
- int length = (buffer[i++] & 0x1F);
- memcpy(&fname[0], &buffer[i], length);
- fname[length] = '