So I have a very simple server/client par which handles sending mouse positions from one client to the others, it seems to work for the little that I tested it, of course many things are done wrong since it's just some quick experimentation to learn about it.
Still, I was wondering, maybe some expert folks out there can give me some advice about which direction I should go from here, what resources I can use to learn the proper way of doing this kind of networking for games.
I have a couple questions about it:
As I understand so far, the recv funcion seems to block (freeze and wait for messages until one is received) unless told otherwise.
The way we tell a connection is to be non-blocking seems to be with the ioctlsocket function using the FIONBIO flag. The select function is then supposed to handle the non-blocking conversation using the FD_ZERO, FD_SET and FD_CLR macros. That is all good (assuming I even got this part correctly) and it seems to work on the server side. However, once I openend a window on the client side and called the recv function to get data from the server the recv call on the client side would still block resulting in the window not responding (as I understand since the message loop from windows were not getting processed anymore). So I tried to find an answer to what I have been doing wrong but couldn't find anything after some time searching. At this point I remembered about the multithreading episode of Handmade Hero and I thought it would be a solution to just launch a thread to call recv and that way it could block but the main thread could still process windows messages and all is good (remember we're talking about the client here, the server still uses select and just one thread). It does work but my question is should the recv function on client side really be blocking when I'm saying the socket is non-blocking? I suspect I might have done something wrong in this code. The full source code is at the end.
I have one other question which is possibly quite abstract and noob-like. So I get we send bytes back and forward. My first thought was to make a struct with the network information I wanted to send but after a quick google it seems that is a bad idea because different endianess and packing of the structs. My question is: what is a good model for sending meaningful game data over tcp/ip from clients to the sever and from server to clients? I could think of some really simple solutions like prefixing numbers with some code like mx100,my200 for mouse positions and stuff like that, but it doesn't seem like a very good approach. So I am hoping there are some experience folks out there who can tell me where to look and what to read from here so I can continue to experiment with this stuff. Anyway, that is it for now, thanks guys!
Full noob source code:
server.cpp
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 | #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <stdio.h> #include <ws2tcpip.h> int WinMain( HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CmdLine, int ShowCmd) { AllocConsole(); freopen("CONOUT$", "w+", stdout); WSADATA WsData; WORD Ver = MAKEWORD(2, 2); int WsOK = WSAStartup(Ver, &WsData); if(WsOK != 0) return -1; SOCKET ListeningSocket = socket(AF_INET, SOCK_STREAM, 0); if(ListeningSocket == INVALID_SOCKET) return -2; sockaddr_in Hint; Hint.sin_family = AF_INET; Hint.sin_port = htons(54000); Hint.sin_addr.S_un.S_addr = INADDR_ANY; u_long Mode = 0; ioctlsocket(ListeningSocket, FIONBIO, &Mode); bind(ListeningSocket, (sockaddr *)&Hint, sizeof(Hint)); listen(ListeningSocket, SOMAXCONN); fd_set MasterSet; FD_ZERO(&MasterSet); FD_SET(ListeningSocket, &MasterSet); while(true) { fd_set CopySet = MasterSet; int SocketCount = select(0, &CopySet, 0, 0, 0); for(int i=0; i<SocketCount; ++i) { SOCKET Socket = CopySet.fd_array[i]; if(Socket == ListeningSocket) { printf("Client connected.\n"); SOCKET ClientSocket = accept(ListeningSocket, 0, 0); int NonBlocokingRequestResult; u_long NonBlockingMode = 0; NonBlocokingRequestResult = ioctlsocket(ClientSocket, FIONBIO, &NonBlockingMode); if(NonBlocokingRequestResult == SOCKET_ERROR) { printf("Error: %d.\n", WSAGetLastError()); } FD_SET(ClientSocket, &MasterSet); char *WelcomeMessage = "Welcome to the test server!"; send(ClientSocket, WelcomeMessage, strlen(WelcomeMessage) + 1, 0); } else { char BufferIn[4096]; ZeroMemory(BufferIn, 4096); int BytesIn = recv(Socket, BufferIn, 4096, 0); if(BytesIn <= 0) { printf("Client disconnected.\n"); closesocket(Socket); FD_CLR(Socket, &MasterSet); } else { for(int i=0; i<MasterSet.fd_count; ++i) { SOCKET SocketOut = MasterSet.fd_array[i]; if(SocketOut != ListeningSocket && SocketOut != Socket) { printf("CLIENT> %s\n", BufferIn); send(SocketOut, BufferIn, BytesIn, 0); } } } } } } WSACleanup(); return(0); } |
client.cpp:
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h> #include <process.h> #include <ws2tcpip.h> int ShouldClose = false; int MousePosX; int MousePosY; int MouseLeftDown; int LastSentMousePosX; int LastSentMousePosY; char DataReceived[4096] = {}; char DataSent[1024] = {}; SOCKET Socket; DWORD NetworkThreadID; DWORD NetworkThreadProc(LPVOID LParam) { DWORD Result = 0; ZeroMemory(DataReceived, 4096); int BytesReceived = 1; while(BytesReceived > 0) { BytesReceived = recv(Socket, DataReceived, 4096, 0); if(BytesReceived < 0) { int Error = WSAGetLastError(); } else if(BytesReceived > 0) { printf("SERVER> %s\n", DataReceived); } else { printf("Disconnected.\n"); } Sleep((1.0f/30)*1000); } return(Result); } LRESULT MainWindowProc( HWND Window, UINT Message, WPARAM WParam, LPARAM LParam) { LRESULT Result = 0; switch(Message) { case WM_LBUTTONDOWN: case WM_LBUTTONUP: { MouseLeftDown = (Message == WM_LBUTTONDOWN); } break; case WM_MOUSEMOVE: { MousePosX = GET_X_LPARAM(LParam); MousePosY = GET_Y_LPARAM(LParam); } break; case WM_CLOSE: case WM_DESTROY: { ShouldClose = true; } break; default: { Result = DefWindowProc(Window, Message, WParam, LParam); } break; } return(Result); } int WinMain( HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CmdLine, int ShowCmd) { AllocConsole(); freopen("CONOUT$", "w+", stdout); char *IpAddress = "127.0.0.1"; int Port = 54000; WSAData Data; WORD Version = MAKEWORD(2, 2); int WsResult = WSAStartup(Version, &Data); if(WsResult != 0) return -1; Socket = socket(AF_INET, SOCK_STREAM, 0); if(Socket == INVALID_SOCKET) { WSACleanup(); return -2; } sockaddr_in Hint; Hint.sin_family = AF_INET; Hint.sin_port = htons(Port); inet_pton(AF_INET, IpAddress, &Hint.sin_addr); int ConnResult = connect(Socket, (sockaddr *)&Hint, sizeof(Hint)); if(ConnResult == SOCKET_ERROR) { closesocket(Socket); WSACleanup(); return -3; } WNDCLASSA WindowClass={}; WindowClass.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW; WindowClass.lpfnWndProc = MainWindowProc; WindowClass.hInstance = Instance; // WindowClass.hCursor; WindowClass.lpszClassName = "MainWindowClassName"; RegisterClassA(&WindowClass); HWND Window = CreateWindowExA( 0, WindowClass.lpszClassName, "Client for Networking Exp 2", // WS_OVERLAPPEDWINDOW, WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 400, 400, 0, 0, Instance, 0 ); HANDLE NetworkThread = CreateThread( 0, 0, NetworkThreadProc, 0, CREATE_SUSPENDED, &NetworkThreadID ); ResumeThread(NetworkThread); while(!ShouldClose) { MSG Message = {}; while(PeekMessageA(&Message, 0, 0, 0, PM_REMOVE)) { TranslateMessage(&Message); DispatchMessageA(&Message); } if(MouseLeftDown) { if(MousePosX != LastSentMousePosX || MousePosY != LastSentMousePosY) { LastSentMousePosX = MousePosX; LastSentMousePosY = MousePosY; ZeroMemory(DataSent, 1024); char XValueStr[32]; char YValueStr[32]; itoa(MousePosX, XValueStr, 10); itoa(MousePosY, YValueStr, 10); strcat(DataSent, XValueStr); strcat(DataSent, ","); strcat(DataSent, YValueStr); ZeroMemory(DataReceived, 4069); int SendResult = send(Socket, DataSent, strlen(DataSent) + 1, 0); if(SendResult == SOCKET_ERROR) { } } } Sleep((1/30.0f)*1000); } TerminateThread(NetworkThread, 0); closesocket(Socket); WSACleanup(); return(0); } |