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); } |
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); } |
I am a game designer who recently became interested in coding in a handmade way. I am not that experienced, but I want to learn.
I am a game designer who recently became interested in coding in a handmade way. I am not that experienced, but I want to learn.