main.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. //*****************************************************************************
  19. //
  20. // Copyright (c) 2000 Westwood Studios. All Rights Reserved.
  21. //
  22. // localhost.cpp
  23. //
  24. // Created on 12 Apr 2001 by Tom Spencer-Smith (Westwood/Vegas)
  25. //
  26. // Description:
  27. // Just a wee test to see if I can use localhost addressing (127.0.0.1)
  28. // on non-networked systems...
  29. //
  30. //*****************************************************************************
  31. #include <stdio.h>
  32. #include <conio.h>
  33. #include <winsock.h>
  34. #include <assert.h>
  35. //---------------------------------------------------------------------------
  36. void main(void)
  37. {
  38. WSADATA winsock_data;
  39. int rc_wsastartup = ::WSAStartup(MAKEWORD(1, 1), &winsock_data);
  40. assert(rc_wsastartup == 0);
  41. SOCKET sock = ::socket(AF_INET, SOCK_DGRAM, 0);
  42. if (sock == INVALID_SOCKET)
  43. {
  44. ::printf("::socket failed with error code %d\n", ::WSAGetLastError());
  45. }
  46. assert(sock != INVALID_SOCKET);
  47. ULONG is_nonblocking = TRUE;
  48. int rc_ioctl = ::ioctlsocket(sock, FIONBIO, &is_nonblocking);
  49. assert(rc_ioctl == 0);
  50. SOCKADDR_IN local_address;
  51. local_address.sin_family = AF_INET;
  52. local_address.sin_addr.s_addr = ::inet_addr("127.0.0.1"); // localhost
  53. local_address.sin_port = ::htons(5555); // arbitrary
  54. int rc_bind = ::bind(sock, reinterpret_cast<const SOCKADDR *>(&local_address),
  55. sizeof(local_address));
  56. assert(rc_bind == 0);
  57. while (!::kbhit())
  58. {
  59. //
  60. // Send a packet periodically
  61. //
  62. static DWORD last_send_time_ms = 0;
  63. DWORD time_now_ms = ::timeGetTime();
  64. const DWORD SEND_INTERVAL_MS = 500;
  65. if (time_now_ms - last_send_time_ms > SEND_INTERVAL_MS)
  66. {
  67. last_send_time_ms = time_now_ms;
  68. static int send_count = 0;
  69. char send_data[200];
  70. ::sprintf(send_data, "packet_%d", send_count++);
  71. int to_len = sizeof(local_address);
  72. int rc_send = ::sendto(sock, send_data, ::strlen(send_data), 0,
  73. (LPSOCKADDR) &local_address, to_len);
  74. assert(rc_send != SOCKET_ERROR);
  75. ::printf("Sent %d bytes (%s) on socket %d\n", rc_send, send_data, sock);
  76. }
  77. //
  78. // Receive all data available
  79. //
  80. int rc_recv = 0;
  81. do
  82. {
  83. char recv_data[200];
  84. ::memset(recv_data, 0, sizeof(recv_data));
  85. SOCKADDR_IN from_address;
  86. int from_len = sizeof(from_address);
  87. rc_recv = ::recvfrom(sock, recv_data, sizeof(recv_data), 0,
  88. (LPSOCKADDR) &from_address, &from_len);
  89. if (rc_recv > 0)
  90. {
  91. ::printf("Recd %d bytes (%s) on socket %d\n", rc_recv, recv_data, sock);
  92. }
  93. else
  94. {
  95. assert(::WSAGetLastError() == WSAEWOULDBLOCK);
  96. }
  97. } while (rc_recv > 0);
  98. }
  99. int rc_cs = ::closesocket(sock);
  100. assert(rc_cs == 0);
  101. int rc_wsacleanup = ::WSACleanup();
  102. assert(rc_wsacleanup == 0);
  103. }