Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.8 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <tchar.h>
  5. #include <winsock.h>
  6. #include <bits.h>
  7. #define WINSOCK_PORT 4000
  8. BOOL GetBuffer( SOCKET conn, char * buf, size_t size )
  9. {
  10. int Received = 0;
  11. do
  12. {
  13. int bytes = recv(conn, buf+Received, size-Received, 0);
  14. if (bytes == 0)
  15. {
  16. printf("client closed the socket connection\n");
  17. return FALSE;
  18. }
  19. if (bytes < 0)
  20. {
  21. printf("read error %d\n", WSAGetLastError());
  22. return FALSE;
  23. }
  24. Received += bytes;
  25. }
  26. while ( Received < int(size) );
  27. // printf("read %d\n", size );
  28. // putchar('.');
  29. return TRUE;
  30. }
  31. void __cdecl wmain (int argc, wchar_t *argv[])
  32. {
  33. DWORD err;
  34. WSADATA WsaData = {0};
  35. if ((err = WSAStartup(0x0101, &WsaData)) != NO_ERROR)
  36. {
  37. printf("unable to init winsock: %d\n", err);
  38. }
  39. //
  40. // listen for connections
  41. //
  42. struct sockaddr_in dest;
  43. dest.sin_addr.s_addr = INADDR_ANY;
  44. dest.sin_family = AF_INET;
  45. dest.sin_port = WINSOCK_PORT;
  46. SOCKET s = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  47. if (s == INVALID_SOCKET)
  48. {
  49. printf("unable to create socket, %d\n", WSAGetLastError());
  50. exit(1);
  51. }
  52. if (bind( s, (sockaddr *) &dest, sizeof(dest)))
  53. {
  54. printf("unable to bind, %d\n", WSAGetLastError());
  55. exit(1);
  56. }
  57. if (listen(s, 5))
  58. {
  59. printf("unable to listen, %d\n", WSAGetLastError());
  60. exit(1);
  61. }
  62. printf("listening...\n");
  63. //
  64. // accept new connections
  65. //
  66. do
  67. {
  68. DWORD Sizes[2];
  69. sockaddr_in client_addr;
  70. int size = sizeof(client_addr);
  71. SOCKET conn = accept( s, (sockaddr *) &client_addr, &size );
  72. printf("new connection\n");
  73. while (1)
  74. {
  75. if (!GetBuffer( conn, (char *) Sizes, sizeof(Sizes)))
  76. {
  77. printf("read sizes failed\n");
  78. break;
  79. }
  80. char * buf = (char *) malloc( max(Sizes[0], Sizes[1]));
  81. if (!GetBuffer( conn, buf, Sizes[0]))
  82. {
  83. printf("read failed\n");
  84. free(buf);
  85. break;
  86. }
  87. if (SOCKET_ERROR == send(conn, buf, Sizes[1], 0))
  88. {
  89. printf("unable to send, %d\n", WSAGetLastError());
  90. free(buf);
  91. break;
  92. }
  93. // printf("sent %d\n", Sizes[1]);
  94. // putchar('.');
  95. free(buf);
  96. }
  97. closesocket( conn );
  98. printf("connection closed\n");
  99. }
  100. while ( 1 );
  101. }