more example stubs
more changes to rnet i'm tired, it's 2am why am i doing this
This commit is contained in:
parent
bde0b6cd75
commit
ee85f59643
6 changed files with 1774 additions and 423 deletions
|
|
@ -25,7 +25,7 @@
|
|||
int main()
|
||||
{
|
||||
// Setup
|
||||
int screenWidth = 800;
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
InitWindow(
|
||||
screenWidth, screenHeight, "raylib [network] example - ping pong");
|
||||
|
|
@ -38,23 +38,26 @@ int main()
|
|||
|
||||
// Create the server
|
||||
SocketConfig server_cfg = {
|
||||
.host = "127.0.0.1",
|
||||
.port = 8080,
|
||||
.server = true,
|
||||
.nonblocking = true,
|
||||
.host = "127.0.0.1",
|
||||
.port = "8080",
|
||||
.server = true,
|
||||
.nonblocking = true
|
||||
};
|
||||
|
||||
SocketResult server_res;
|
||||
memset(&server_res, 0, sizeof(SocketResult));
|
||||
{
|
||||
bool ok = SocketOpen(&server_cfg, &server_res);
|
||||
if (!ok) { return false; }
|
||||
if (!ok)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Create the client
|
||||
SocketConfig client_cfg = {
|
||||
.host = "127.0.0.1",
|
||||
.port = 8080,
|
||||
.host = "127.0.0.1",
|
||||
.port = "8080"
|
||||
};
|
||||
|
||||
SocketResult client_res;
|
||||
|
|
@ -63,8 +66,8 @@ int main()
|
|||
bool ok = SocketOpen(&client_cfg, &client_res);
|
||||
if (!ok)
|
||||
{
|
||||
printf("failed to open: status %d, errno %d\n",
|
||||
client_res.status, client_res.saved_errno);
|
||||
printf("failed to open: status %d, errno %d\n", client_res.status,
|
||||
client_res.socket.error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -76,6 +79,8 @@ int main()
|
|||
char recvBuffer[512];
|
||||
bool connected = false;
|
||||
memset(&recvBuffer, 0, 8);
|
||||
char pingmsg[6] = "Ping!";
|
||||
char pongmsg[6] = "Pong!";
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
|
|
@ -91,7 +96,7 @@ int main()
|
|||
{
|
||||
if (SocketAccept(server_res.socket.handle, &connection))
|
||||
{
|
||||
ping = true;
|
||||
ping = true;
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
|
|
@ -99,18 +104,18 @@ int main()
|
|||
// Connected
|
||||
if (connected)
|
||||
{
|
||||
int bytesRecv = SocketReceive(&connection.socket, recvBuffer, 5);
|
||||
int bytesRecv = SocketReceive(&connection.socket, recvBuffer, sizeof(pingmsg));
|
||||
if (bytesRecv > 0)
|
||||
{
|
||||
if (strcmp(recvBuffer, "Ping!") == 0)
|
||||
if (strcmp(recvBuffer, pingmsg) == 0)
|
||||
{
|
||||
pong = true;
|
||||
printf("Ping!\n");
|
||||
printf("%s\n", pingmsg);
|
||||
}
|
||||
if (strcmp(recvBuffer, "Pong!") == 0)
|
||||
if (strcmp(recvBuffer, pongmsg) == 0)
|
||||
{
|
||||
ping = true;
|
||||
printf("Pong!\n");
|
||||
printf("%s\n", pongmsg);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -120,12 +125,12 @@ int main()
|
|||
if (ping)
|
||||
{
|
||||
ping = false;
|
||||
SocketSend(&client_res.socket, "Ping!", 5);
|
||||
SocketSend(&client_res.socket, pingmsg, sizeof(pingmsg));
|
||||
}
|
||||
else if (pong)
|
||||
{
|
||||
pong = false;
|
||||
SocketSend(&client_res.socket, "Pong!", 5);
|
||||
SocketSend(&client_res.socket, pongmsg, sizeof(pongmsg));
|
||||
}
|
||||
elapsed = 0.0f;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,8 +38,8 @@ int main()
|
|||
|
||||
AddressInformation addr;
|
||||
ResolveHost("www.google.com", "80", &addr);
|
||||
// ResolveIP("8.8.8.8", NULL, NAME_INFO_DEFAULT);
|
||||
// ResolveIP("2001:4860:4860::8888", "80", NAME_INFO_NUMERICSERV);
|
||||
ResolveIP("8.8.8.8", NULL, NAME_INFO_DEFAULT);
|
||||
ResolveIP("2001:4860:4860::8888", "80", NAME_INFO_NUMERICSERV);
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
|
|
|
|||
93
examples/network/network_tcp_client.c
Normal file
93
examples/network/network_tcp_client.c
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [network] example - Resolve host
|
||||
*
|
||||
* Welcome to raylib!
|
||||
*
|
||||
* To test examples, just press F6 and execute raylib_compile_execute script
|
||||
* Note that compiled executable is placed in the same folder as .c file
|
||||
*
|
||||
* You can find all basic examples on C:\raylib\raylib\examples folder or
|
||||
* raylib official webpage: www.raylib.com
|
||||
*
|
||||
* Enjoy using raylib. :)
|
||||
*
|
||||
* This example has been created using raylib 2.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
|
||||
*for details)
|
||||
*
|
||||
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define PORT "3490" // the port client will be connecting to
|
||||
#define MAXDATASIZE 100 // max number of bytes we can get at once
|
||||
|
||||
int main()
|
||||
{
|
||||
// Setup
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
InitWindow(
|
||||
screenWidth, screenHeight, "raylib [network] example - ping pong");
|
||||
SetTargetFPS(60);
|
||||
|
||||
SetTraceLogLevel(LOG_DEBUG);
|
||||
|
||||
// Networking
|
||||
InitNetwork();
|
||||
|
||||
int numbytes;
|
||||
char buf[MAXDATASIZE];
|
||||
|
||||
// Create the client
|
||||
SocketConfig client_cfg = {
|
||||
.host = "127.0.0.1",
|
||||
.port = "8080"
|
||||
};
|
||||
SocketResult client_res;
|
||||
memset(&client_res, 0, sizeof(SocketResult));
|
||||
{
|
||||
bool ok = SocketOpen(&client_cfg, &client_res);
|
||||
if (!ok)
|
||||
{
|
||||
printf("Failed to open: status %d, errno %d\n", client_res.status,
|
||||
client_res.socket.error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Draw
|
||||
BeginDrawing();
|
||||
|
||||
// Clear
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// Receive bytes from the server
|
||||
numbytes = SocketReceive(client_res.socket.handle, buf, MAXDATASIZE - 1);
|
||||
if (numbytes == -1)
|
||||
{
|
||||
printf("Client: error recv '%s'\n", buf);
|
||||
break;
|
||||
}
|
||||
buf[numbytes] = '\0';
|
||||
printf("Client: received '%s'\n", buf);
|
||||
break;
|
||||
|
||||
// End draw
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
SocketClose(client_res.socket.handle);
|
||||
CloseNetwork();
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
100
examples/network/network_tcp_server.c
Normal file
100
examples/network/network_tcp_server.c
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [network] example - Resolve host
|
||||
*
|
||||
* Welcome to raylib!
|
||||
*
|
||||
* To test examples, just press F6 and execute raylib_compile_execute script
|
||||
* Note that compiled executable is placed in the same folder as .c file
|
||||
*
|
||||
* You can find all basic examples on C:\raylib\raylib\examples folder or
|
||||
* raylib official webpage: www.raylib.com
|
||||
*
|
||||
* Enjoy using raylib. :)
|
||||
*
|
||||
* This example has been created using raylib 2.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
|
||||
*for details)
|
||||
*
|
||||
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
||||
#include "raylib.h"
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// Setup
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
InitWindow(
|
||||
screenWidth, screenHeight, "raylib [network] example - ping pong");
|
||||
SetTargetFPS(60);
|
||||
|
||||
SetTraceLogLevel(LOG_DEBUG);
|
||||
|
||||
// Networking
|
||||
InitNetwork();
|
||||
|
||||
|
||||
// Create the server
|
||||
SocketConfig server_cfg = {
|
||||
.host = "127.0.0.1",
|
||||
.port = "8080",
|
||||
.server = true,
|
||||
.nonblocking = true
|
||||
};
|
||||
|
||||
SocketResult server_res;
|
||||
memset(&server_res, 0, sizeof(SocketResult));
|
||||
{
|
||||
bool ok = SocketOpen(&server_cfg, &server_res);
|
||||
if (!ok)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SocketResult connection;
|
||||
memset(&connection, 0, sizeof(SocketResult));
|
||||
|
||||
char recvBuffer[512];
|
||||
memset(&recvBuffer, 0, 8);
|
||||
|
||||
bool connected = false;
|
||||
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Draw
|
||||
BeginDrawing();
|
||||
|
||||
// Clear
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// Wait for a valid connection
|
||||
if (!connected)
|
||||
{
|
||||
if (SocketAccept(server_res.socket.handle, &connection))
|
||||
{
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (connected)
|
||||
{
|
||||
SocketSend(&connection, "Hello, world!", 13);
|
||||
}
|
||||
|
||||
// End draw
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
74
examples/network/network_udp_client.c
Normal file
74
examples/network/network_udp_client.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [network] example - Resolve host
|
||||
*
|
||||
* Welcome to raylib!
|
||||
*
|
||||
* To test examples, just press F6 and execute raylib_compile_execute script
|
||||
* Note that compiled executable is placed in the same folder as .c file
|
||||
*
|
||||
* You can find all basic examples on C:\raylib\raylib\examples folder or
|
||||
* raylib official webpage: www.raylib.com
|
||||
*
|
||||
* Enjoy using raylib. :)
|
||||
*
|
||||
* This example has been created using raylib 2.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
|
||||
*for details)
|
||||
*
|
||||
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
||||
#include "raylib.h"
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// Setup
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
InitWindow(
|
||||
screenWidth, screenHeight, "raylib [network] example - ping pong");
|
||||
SetTargetFPS(60);
|
||||
|
||||
SetTraceLogLevel(LOG_DEBUG);
|
||||
|
||||
// Networking
|
||||
InitNetwork();
|
||||
|
||||
unsigned char buf[1024];
|
||||
unsigned char magic;
|
||||
int monkeycount;
|
||||
long altitude;
|
||||
double absurdityfactor;
|
||||
char* s = "Great unmitigated Zot! You've found the Runestaff!";
|
||||
char s2[96];
|
||||
unsigned int packetsize, ps2;
|
||||
|
||||
packetsize = PackData(buf, "CHhlsd", 'B', 0, 37, -5, s, -3490.5);
|
||||
packi16(buf + 1, packetsize); // store packet size in packet for kicks
|
||||
|
||||
printf("packet is %u bytes\n", packetsize);
|
||||
UnpackData(buf, "CHhl96sd", &magic, &ps2, &monkeycount, &altitude, s2, &absurdityfactor);
|
||||
printf("'%c' %hhu %u %ld \"%s\" %f\n", magic, ps2, monkeycount, altitude, s2, absurdityfactor);
|
||||
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Draw
|
||||
BeginDrawing();
|
||||
|
||||
// Clear
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// End draw
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
74
examples/network/network_udp_server.c
Normal file
74
examples/network/network_udp_server.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [network] example - Resolve host
|
||||
*
|
||||
* Welcome to raylib!
|
||||
*
|
||||
* To test examples, just press F6 and execute raylib_compile_execute script
|
||||
* Note that compiled executable is placed in the same folder as .c file
|
||||
*
|
||||
* You can find all basic examples on C:\raylib\raylib\examples folder or
|
||||
* raylib official webpage: www.raylib.com
|
||||
*
|
||||
* Enjoy using raylib. :)
|
||||
*
|
||||
* This example has been created using raylib 2.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
|
||||
*for details)
|
||||
*
|
||||
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
||||
#include "raylib.h"
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// Setup
|
||||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
InitWindow(
|
||||
screenWidth, screenHeight, "raylib [network] example - ping pong");
|
||||
SetTargetFPS(60);
|
||||
|
||||
SetTraceLogLevel(LOG_DEBUG);
|
||||
|
||||
// Networking
|
||||
InitNetwork();
|
||||
|
||||
unsigned char buf[1024];
|
||||
unsigned char magic;
|
||||
int monkeycount;
|
||||
long altitude;
|
||||
double absurdityfactor;
|
||||
char* s = "Great unmitigated Zot! You've found the Runestaff!";
|
||||
char s2[96];
|
||||
unsigned int packetsize, ps2;
|
||||
|
||||
packetsize = PackData(buf, "CHhlsd", 'B', 0, 37, -5, s, -3490.5);
|
||||
packi16(buf + 1, packetsize); // store packet size in packet for kicks
|
||||
|
||||
printf("packet is %u bytes\n", packetsize);
|
||||
UnpackData(buf, "CHhl96sd", &magic, &ps2, &monkeycount, &altitude, s2, &absurdityfactor);
|
||||
printf("'%c' %hhu %u %ld \"%s\" %f\n", magic, ps2, monkeycount, altitude, s2, absurdityfactor);
|
||||
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Draw
|
||||
BeginDrawing();
|
||||
|
||||
// Clear
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// End draw
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue