Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <math.h>
  5. #include <windows.h>
  6. #include <winsock.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <io.h>
  12. #include <fstream>
  13. #include <vector>
  14. #define MAX_CONNECTION 100
  15. #ifndef max
  16. #define max(a,b) ((a) < (b) ? (a) : (b))
  17. #endif
  18. typedef struct connection{
  19. char ipaddr[16];
  20. int port;
  21. int sd;
  22. } connection;
  23.  
  24. static unsigned short SERVER_PORT = 4118;
  25. int main(int argc, char* argv[])
  26. {
  27. int maxfd =-1;
  28. fd_set rset, allset;
  29. connection client[MAX_CONNECTION];
  30.  
  31. int passiveSock; /* Main Server Socket */
  32. struct sockaddr_in servSock_in;
  33. #ifdef WIN32
  34. WSADATA wsaData;
  35. WSAStartup(0x0101, &wsaData);
  36. #endif
  37. int port;
  38. if (argc > 1)
  39. port = atoi(argv[1]);
  40. else
  41. port = SERVER_PORT;
  42.  
  43. for(int i=0; i < MAX_CONNECTION ; i++)
  44. client[i].sd = -1;
  45. memset((char *)&servSock_in, 0, sizeof(servSock_in));
  46. servSock_in.sin_family = PF_INET;
  47. servSock_in.sin_addr.s_addr = htonl(INADDR_ANY);
  48. servSock_in.sin_port = htons((u_short)port);
  49.  
  50. passiveSock = socket(PF_INET, SOCK_STREAM, 0);
  51. if (passiveSock < 0) {
  52. fprintf(stderr, "I am too tired... I failed to open gate...n");
  53. return -1;
  54. }
  55. if (bind(passiveSock, (struct sockaddr *)&servSock_in, sizeof(servSock_in)) < 0){
  56. fprintf(stderr, "I couldn't attach gate to port...n");
  57. return -1;
  58. }
  59. if (listen(passiveSock, 5) < 0) {
  60. fprintf(stderr, "I am not hearing anything...n");
  61. return -1;
  62. }
  63.  
  64. FD_SET(passiveSock, &allset);
  65. maxfd = max(maxfd, passiveSock);
  66.  
  67. struct sockaddr_in cliSock_in;
  68. int cliSockLen;
  69. int connectedSock;
  70. cliSockLen = sizeof(cliSock_in);
  71. printf("n Waiting for an incoming connection at port number %d", port);
  72. int bytesread = 0;
  73. for (;;)
  74. {
  75. //FD_ZERO(&allset);
  76.  
  77. rset = allset;
  78. int nready = select(maxfd+1,&rset, NULL, NULL, NULL);
  79. if(FD_ISSET(passiveSock, &rset)){
  80. printf("In the first if");
  81.  
  82. connectedSock = accept(passiveSock, (struct sockaddr *)&cliSock_in, &cliSockLen);
  83. /* if an error occurs while accepting */
  84. if (connectedSock == -1) {
  85. printf("n Server: Accept error (errno = %d: %s)n", errno, strerror(errno));
  86. continue;
  87. }
  88.  
  89.  
  90.  
  91.  
  92. for (int i=0; i<MAX_CONNECTION; i++)
  93. if (client[i].sd < 0){
  94. client[i].sd=connectedSock;
  95. strcpy(client[i].ipaddr, inet_ntoa(cliSock_in.sin_addr));
  96. client[i].port= ntohs(cliSock_in.sin_port);
  97. printf("n Server: connection established with %s:%dn",
  98. client[i].ipaddr, client[i].port);
  99. break;
  100. }
  101. FD_SET(connectedSock, &allset);
  102. maxfd = max(maxfd, connectedSock);
  103.  
  104. }
  105.  
  106. else{
  107. for(int j = 0 ; j < MAX_CONNECTION; j++){
  108. connectedSock = client[j].sd;
  109. printf("connectedSock is %d", connectedSock);
  110. if(connectedSock < 0)
  111. continue;
  112. if(FD_ISSET(client[j].sd, &rset)){
  113. unsigned char buffer[66000];
  114. int index = 0;
  115. bytesread = recv(connectedSock, (char *)buffer, 66000, 0);
  116. int type;
  117.  
  118.  
  119.  
  120.  
  121. type = (buffer[0] & 0xE0) >> 5;
  122.  
  123. if (type == 0)
  124. {
  125. char fname[100];
  126. int i = 0;
  127. int length = (buffer[i++] & 0x1F);
  128. memcpy(&fname[0], &buffer[i], length);
  129. fname[length] = '';
  130. i += length;
  131. int fs = 0;
  132. fs += (buffer[i++] << 8) & 0xff00;
  133. fs += buffer[i++];
  134. char* filedata = (char*)malloc(fs*sizeof(char));
  135. memcpy(&filedata[0], &buffer[i], fs);
  136. filedata[fs] = '';
  137. for (int i = 0; i < fs; i++)
  138. printf("%c", filedata[i]);
  139. printf("type=%d,length=%d,data=%s,fs=%d,filedata=%s", type, length, fname, fs, filedata);
  140. std::ofstream of;
  141. of.open(fname, std::ios::binary);
  142. for (int i = 0; i < fs; i++)
  143. of.write(filedata + i, 1);
  144. of.close();
  145. unsigned char rep;
  146. int reptype = 0;
  147. rep = (unsigned char)(reptype & 0x07);
  148. send(connectedSock, (char*)(&rep), 1, 0);
  149. }
  150. else if (type == 1)
  151. {
  152. char fname[100];
  153. int i = 0;
  154. int length = (buffer[i++] & 0x1F);
  155. memcpy(&fname[0], &buffer[i], length);
  156. fname[length] = '';
  157. i += length;
  158. std::ifstream t;
  159. int fs;
  160. t.open(fname, std::ios::binary);
  161. std::vector<char> vec((
  162. std::istreambuf_iterator<char>(t)),
  163. (std::istreambuf_iterator<char>()));// open input file
  164. t.close();
  165. fs = vec.size();
  166. char* filedata = (char*)malloc(fs*sizeof(char)); // allocate memory for a buffer of appropriate dimension
  167. filedata = &vec[0];
  168. filedata[fs] = '';
  169. i = 0;
  170. unsigned char* repbuffer = (unsigned char*)malloc(3 + length + fs);
  171. repbuffer[i] = (unsigned char)(type & 0x07);
  172. repbuffer[i] = repbuffer[i] << 5;
  173. repbuffer[i] = repbuffer[i] | (length & 0x0000003F);
  174. i++;
  175. memcpy(&repbuffer[i], fname, length);
  176. i = i + length;
  177. printf("sizeof fs=%d", sizeof(fs));
  178. repbuffer[i++] = (unsigned char)((fs & 0xff00) >> 8);
  179. repbuffer[i++] = (unsigned char)(fs & 0xff);
  180. memcpy(&repbuffer[i], filedata, fs);
  181. printf("sizeof buffer=%d", sizeof(repbuffer));
  182. i = i + fs;
  183. // printf("the buffer contains %sn",&repbuffer[11]);
  184. if (send(connectedSock, (char*)repbuffer, i, 0) == -1)
  185. {
  186. printf("A local error was detected while sending data! (errno = %d: %s)n", errno, strerror(errno));
  187. return -1;
  188. }
  189. }
  190. else if (type == 2)
  191. {
  192. char fname[100],nfname[100];
  193. int i = 0;
  194. int length = (buffer[i++] & 0x1F);
  195. memcpy(&fname[0], &buffer[i], length);
  196. fname[length] = '';
  197. i += length;
  198. int nlength = (buffer[i++] & 0x1F);
  199. memcpy(&nfname[0], &buffer[i], nlength);
  200. nfname[nlength] = '';
  201. rename(fname,nfname);
  202.  
  203. }
  204. else if (type == 3)
  205. {
  206. char rep[32];
  207. strcpy(rep, "bye change get help put");
  208. int length = strlen(rep);
  209. unsigned char* repbuffer = (unsigned char*)malloc(1 + length);
  210. int type = 6;
  211. repbuffer[0] = (unsigned char)(type & 0x07);
  212. repbuffer[0] = repbuffer[0] << 5;
  213. repbuffer[0] = repbuffer[0] | (length & 0x0000003F);
  214. memcpy(&repbuffer[1], rep, length);
  215. if (send(connectedSock, (char*)repbuffer, length+1, 0) == -1)
  216. {
  217. perror("A local error was detected while sending data!!");
  218. return -1;
  219. }
  220. }
  221. else if (type == 4)
  222. {
  223. closesocket(connectedSock);
  224. }
  225.  
  226. break;
  227. }
  228. }
  229. }
  230. }
  231. closesocket(passiveSock);
  232. WSACleanup();
  233. return 0;
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement