fixed some more examples
fixed the ResolveHost method added more network specific defines
This commit is contained in:
parent
867e29b6a4
commit
7c2f816852
7 changed files with 271 additions and 148 deletions
|
|
@ -36,8 +36,8 @@ bool client_connected = false;
|
|||
const char * pingmsg = "Ping!";
|
||||
const char * pongmsg = "Pong!";
|
||||
int msglen = 0;
|
||||
SocketConfig server_cfg = {.host = "127.0.0.1", .port = "8080", .type = SOCKET_TCP, .server = true, .nonblocking = true};
|
||||
SocketConfig client_cfg = {.host = "127.0.0.1", .port = "8080", .type = SOCKET_TCP, .nonblocking = true};
|
||||
SocketConfig server_cfg = {.host = "127.0.0.1", .port = "4950", .type = SOCKET_TCP, .server = true, .nonblocking = true};
|
||||
SocketConfig client_cfg = {.host = "127.0.0.1", .port = "4950", .type = SOCKET_TCP, .nonblocking = true};
|
||||
SocketConfig connection_cfg = {.nonblocking = true};
|
||||
SocketResult *server_res = NULL;
|
||||
SocketResult *client_res = NULL;
|
||||
|
|
@ -100,14 +100,14 @@ void NetworkUpdate()
|
|||
int bytesRecv = 0;
|
||||
if (server_cfg.type == SOCKET_UDP && client_cfg.type == SOCKET_UDP) {
|
||||
if (IsSocketReady(client_res->socket)) {
|
||||
bytesRecv = SocketReceive(client_res->socket, recvBuffer, msglen, 0);
|
||||
bytesRecv = SocketReceive(client_res->socket, recvBuffer, msglen);
|
||||
}
|
||||
if (IsSocketReady(server_res->socket)) {
|
||||
bytesRecv = SocketReceive(server_res->socket, recvBuffer, msglen, 0);
|
||||
bytesRecv = SocketReceive(server_res->socket, recvBuffer, msglen);
|
||||
}
|
||||
} else {
|
||||
if (IsSocketReady(connection)) {
|
||||
bytesRecv = SocketReceive(connection, recvBuffer, msglen, 0);
|
||||
bytesRecv = SocketReceive(connection, recvBuffer, msglen);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,9 +36,22 @@ int main()
|
|||
// Networking
|
||||
InitNetwork();
|
||||
|
||||
ResolveHost("www.google.com", "80");
|
||||
ResolveIP("8.8.8.8", NULL, NAME_INFO_DEFAULT);
|
||||
ResolveIP("2001:4860:4860::8888", "80", NAME_INFO_NUMERICSERV);
|
||||
AddressInformation* addr = NULL;
|
||||
int count = ResolveHost(
|
||||
NULL,
|
||||
"5210",
|
||||
ADDRESS_TYPE_IPV4,
|
||||
0 // Uncomment any of these flags
|
||||
// ADDRESS_INFO_NUMERICHOST // or try them in conjunction to
|
||||
// ADDRESS_INFO_NUMERICSERV // specify custom behaviour from
|
||||
// ADDRESS_INFO_DNS_ONLY // the function getaddrinfo()
|
||||
// ADDRESS_INFO_ALL //
|
||||
// ADDRESS_INFO_FQDN // e.g. ADDRESS_INFO_CANONNAME | ADDRESS_INFO_NUMERICSERV
|
||||
,
|
||||
addr
|
||||
);
|
||||
ResolveIP("8.8.8.8", NULL, NAME_INFO_DEFAULT, NULL, NULL);
|
||||
ResolveIP("2001:4860:4860::8888", "80", NAME_INFO_NUMERICSERV, NULL, NULL);
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ bool connected = false;
|
|||
const char * pingmsg = "Ping!";
|
||||
const char * pongmsg = "Pong!";
|
||||
int msglen = 0;
|
||||
SocketConfig client_cfg = {.host = "127.0.0.1", .port = "8080", .type = SOCKET_TCP, .nonblocking = true};
|
||||
SocketConfig client_cfg = {.host = "127.0.0.1", .port = "4950", .type = SOCKET_TCP, .nonblocking = true};
|
||||
SocketResult *client_res = NULL;
|
||||
SocketSet * socket_set = NULL;
|
||||
char recvBuffer[512];
|
||||
|
|
@ -48,7 +48,7 @@ void NetworkUpdate()
|
|||
// If the socket is ready, attempt to receive data from the socket
|
||||
int bytesRecv = 0;
|
||||
if (IsSocketReady(client_res->socket)) {
|
||||
bytesRecv = SocketReceive(client_res->socket, recvBuffer, msglen, 0);
|
||||
bytesRecv = SocketReceive(client_res->socket, recvBuffer, msglen);
|
||||
}
|
||||
|
||||
// If we received data, was that data a "Ping!" or a "Pong!"
|
||||
|
|
@ -77,7 +77,7 @@ int main()
|
|||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
InitWindow(
|
||||
screenWidth, screenHeight, "raylib [network] example - ping pong");
|
||||
screenWidth, screenHeight, "raylib [network] example - tcp client");
|
||||
SetTargetFPS(60);
|
||||
SetTraceLogLevel(LOG_DEBUG);
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ bool connected = false;
|
|||
const char * pingmsg = "Ping!";
|
||||
const char * pongmsg = "Pong!";
|
||||
int msglen = 0;
|
||||
SocketConfig server_cfg = {.host = "127.0.0.1", .port = "8080", .type = SOCKET_TCP, .server = true, .nonblocking = true};
|
||||
SocketConfig server_cfg = {.host = "127.0.0.1", .port = "4950", .type = SOCKET_TCP, .server = true, .nonblocking = true};
|
||||
SocketConfig connection_cfg = {.nonblocking = true};
|
||||
SocketResult *server_res = NULL;
|
||||
SocketSet * socket_set = NULL;
|
||||
|
|
@ -56,7 +56,7 @@ void NetworkUpdate()
|
|||
// If the socket is ready, attempt to receive data from the socket
|
||||
int bytesRecv = 0;
|
||||
if (IsSocketReady(connection)) {
|
||||
bytesRecv = SocketReceive(connection, recvBuffer, msglen, 0);
|
||||
bytesRecv = SocketReceive(connection, recvBuffer, msglen);
|
||||
}
|
||||
|
||||
// If we received data, was that data a "Ping!" or a "Pong!"
|
||||
|
|
@ -85,7 +85,7 @@ int main()
|
|||
int screenWidth = 800;
|
||||
int screenHeight = 450;
|
||||
InitWindow(
|
||||
screenWidth, screenHeight, "raylib [network] example - ping pong");
|
||||
screenWidth, screenHeight, "raylib [network] example - tcp server");
|
||||
SetTargetFPS(60);
|
||||
SetTraceLogLevel(LOG_DEBUG);
|
||||
|
||||
|
|
|
|||
|
|
@ -28,39 +28,40 @@ void test_resolve_ip()
|
|||
const char *host = "8.8.8.8";
|
||||
const char *port = "8080";
|
||||
char ip[ADDRESS_IPV6_ADDRSTRLEN];
|
||||
char service[ADDRESS_MAXSERV];
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip);
|
||||
ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "8.8.8.8") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_DEFAULT, ip);
|
||||
ResolveIP(host, port, NAME_INFO_DEFAULT, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_NOFQDN, ip);
|
||||
ResolveIP(host, port, NAME_INFO_NOFQDN, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "google-public-dns-a") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip);
|
||||
ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "8.8.8.8") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_NAMEREQD, ip);
|
||||
ResolveIP(host, port, NAME_INFO_NAMEREQD, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_NUMERICSERV, ip);
|
||||
ResolveIP(host, port, NAME_INFO_NUMERICSERV, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_DGRAM, ip);
|
||||
ResolveIP(host, port, NAME_INFO_DGRAM, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
|
||||
}
|
||||
|
|
@ -75,7 +76,7 @@ void test_resolve_host()
|
|||
assert(GetAddressFamily(addr[1]) == ADDRESS_TYPE_IPV4);
|
||||
assert(GetAddressSocketType(addr[0]) == 0);
|
||||
assert(GetAddressProtocol(addr[0]) == 0);
|
||||
for (size_t i = 0; i < count; i++) { PrintAddressInfo(addr[i]); }
|
||||
// for (size_t i = 0; i < count; i++) { PrintAddressInfo(addr[i]); }
|
||||
}
|
||||
|
||||
void test_address()
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ void NetworkUpdate()
|
|||
// If the socket is ready, attempt to receive data from the socket
|
||||
int bytesRecv = 0;
|
||||
if (IsSocketReady(client_res->socket)) {
|
||||
bytesRecv = SocketReceive(client_res->socket, recvBuffer, msglen, 0);
|
||||
bytesRecv = SocketReceive(client_res->socket, recvBuffer, msglen);
|
||||
}
|
||||
|
||||
// If we received data, was that data a "Ping!" or a "Pong!"
|
||||
|
|
|
|||
|
|
@ -35,9 +35,9 @@ void NetworkUpdate()
|
|||
// If the socket is ready, attempt to receive data from the socket
|
||||
// int bytesRecv = 0;
|
||||
// if (IsSocketReady(server_res->socket)) {
|
||||
// bytesRecv = SocketReceive(server_res->socket, recvBuffer, msglen, 0);
|
||||
// bytesRecv = SocketReceive(server_res->socket, recvBuffer, msglen);
|
||||
// }
|
||||
int bytesRecv = SocketReceive(server_res->socket, recvBuffer, msglen, 0);
|
||||
int bytesRecv = SocketReceive(server_res->socket, recvBuffer, msglen);
|
||||
|
||||
// If we received data, was that data a "Ping!" or a "Pong!"
|
||||
if (bytesRecv > 0) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue