int main(int argc, char **argv) { NLsocket sock; NLaddress addr, *alladdr; NLbyte string[NL_MAX_STRING_LENGTH]; NLenum type = NL_IP; /* default network type */ NLint count; if(nlInit() == NL_FALSE) printErrorExit(); printf("nlGetString(NL_VERSION) = %s\n\n", nlGetString(NL_VERSION)); printf("nlGetString(NL_NETWORK_TYPES) = %s\n\n", nlGetString(NL_NETWORK_TYPES)); if (argc == 2) { if(strcmp(argv[1], "NL_IPX") == 0) { type = NL_IPX; } else if(strcmp(argv[1], "NL_LOOP_BACK") == 0) { type = NL_LOOP_BACK; } } if(nlSelectNetwork(type) == NL_FALSE) printErrorExit(); /* list all the local addresses */ printf("local addresses are:\n"); alladdr = nlGetAllLocalAddr(&count); while(count-- > 0) { printf(" %s\n", nlAddrToString(alladdr++, string)); } printf("\n"); nlEnable(NL_SOCKET_STATS); /* enable reuse address to run two or more copies on one machine */ nlHint(NL_REUSE_ADDRESS, NL_TRUE); /* create a client socket */ sock = nlOpen(25000, NL_BROADCAST); if(sock == NL_INVALID) printErrorExit(); nlGetLocalAddr(sock, &addr); printf("socket address is %s\n", nlAddrToString(&addr, string)); mainTestLoop(sock); nlShutdown(); return 0; }
int _tmain(int argc, NLchar **argv) #endif { NLaddress addr1; NLchar string[NL_MAX_STRING_LENGTH]; if(nlInit() == NL_FALSE) printErrorExit(); _tprintf(TEXT("nlGetString(NL_VERSION) = %s\n\n"), nlGetString(NL_VERSION)); if(nlSelectNetwork(NL_IP) == NL_FALSE) printErrorExit(); /* init the string improperly with NO null termination */ memset(string, 'a', sizeof(string)); _tprintf(TEXT("Test nlStringToAddr with unterminated string\n")); if(nlStringToAddr(string, &addr1) == NL_FALSE) printError(); _tprintf(TEXT("Test nlGetAddrFromName with unterminated string\n")); if(nlGetAddrFromName(string, &addr1) == NL_FALSE) printError(); _tprintf(TEXT("Test nlGetAddrFromNameAsync with unterminated string\n")); nlGetAddrFromNameAsync(string, &addr1); printError(); _tprintf(TEXT("Done\n")); nlShutdown(); return 0; }
int main(int argc, char **argv) { NLboolean isserver = NL_FALSE; NLsocket serversock; NLsocket clientsock; NLaddress addr; NLbyte server[] = "127.0.0.1:25000"; NLenum type = NL_UNRELIABLE; /* Change this to NL_RELIABLE for reliable connection */ NLbyte string[NL_MAX_STRING_LENGTH]; if(!nlInit()) printErrorExit(); printf("nlGetString(NL_VERSION) = %s\n\n", nlGetString(NL_VERSION)); printf("nlGetString(NL_NETWORK_TYPES) = %s\n\n", nlGetString(NL_NETWORK_TYPES)); if(!nlSelectNetwork(NL_IP)) printErrorExit(); if(argc > 1) { if(!strcmp(argv[1], "-s")) /* server mode */ isserver = NL_TRUE; } if(isserver) { /* create a server socket */ serversock = nlOpen(25000, type); /* just a random port number ;) */ if(serversock == NL_INVALID) printErrorExit(); if(!nlListen(serversock)) /* let's listen on this socket */ { nlClose(serversock); printErrorExit(); } nlGetLocalAddr(serversock, &addr); printf("Server address is %s\n", nlAddrToString(&addr, string)); mainServerLoop(serversock); } else { /* create a client socket */ clientsock = nlOpen(0, type); /* let the system assign the port number */ nlGetLocalAddr(clientsock, &addr); printf("our address is %s\n", nlAddrToString(&addr, string)); if(clientsock == NL_INVALID) printErrorExit(); /* create the NLaddress */ nlStringToAddr(server, &addr); printf("Address is %s\n", nlAddrToString(&addr, string)); /* now connect */ if(!nlConnect(clientsock, &addr)) { nlClose(clientsock); printErrorExit(); } mainClientLoop(clientsock); } nlShutdown(); return 0; }
int _tmain(int argc, NLchar **argv) #endif { NLbyte buffer[NL_MAX_STRING_LENGTH]; NLint count = 0; NLbyte b = (NLbyte)99; NLushort s = 0x1122; NLulong l = 0x11223344; NLfloat f = 12.3141592651f; NLdouble d = 123.12345678901234; NLchar string[NL_MAX_STRING_LENGTH] = TEXT("Hello"); NLbyte block[] = {9,8,7,6,5,4,3,2,1,0}; if(nlInit() == NL_FALSE) printErrorExit(); nlEnable(NL_BIG_ENDIAN_DATA); _tprintf(TEXT("nl_big_endian_data = %d\n"), nlGetBoolean(NL_BIG_ENDIAN_DATA)); _tprintf(TEXT("nlGetString(NL_VERSION) = %s\n\n"), nlGetString(NL_VERSION)); _tprintf(TEXT("nlGetString(NL_NETWORK_TYPES) = %s\n\n"), nlGetString(NL_NETWORK_TYPES)); if(nlSelectNetwork(NL_IP) == NL_FALSE) printErrorExit(); _tprintf(TEXT("Short number: %#x, "), s); s = nlSwaps(s); _tprintf(TEXT("swapped: %#x, "), s); s = nlSwaps(s); _tprintf(TEXT("swapped back: %#x\n"), s); _tprintf(TEXT("Long number: %#lx, "), l); l = nlSwapl(l); _tprintf(TEXT("swapped: %#lx, "), l); l = nlSwapl(l); _tprintf(TEXT("swapped back: %#lx\n"), l); _tprintf(TEXT("Float number: %.10f, "), f); f = nlSwapf(f); _tprintf(TEXT("swapped: %.10f, "), f); f = nlSwapf(f); _tprintf(TEXT("swapped back: %.10f\n"), f); _tprintf(TEXT("Double number: %.14f, "), d); d = nlSwapd(d); _tprintf(TEXT("swapped: %.24f, "), d); d = nlSwapd(d); _tprintf(TEXT("swapped back: %.14f\n"), d); _tprintf(TEXT("\n")); _tprintf(TEXT("write byte %d to buffer\n"), b); _tprintf(TEXT("write short %#x to buffer\n"), s); _tprintf(TEXT("write long %#lx to buffer\n"), l); _tprintf(TEXT("write float %f to buffer\n"), f); _tprintf(TEXT("write double %.14f to buffer\n"), d); _tprintf(TEXT("write string %s to buffer\n"), string); _tprintf(TEXT("write block %d%d%d%d%d%d%d%d%d%d to buffer\n"), block[0] , block[1], block[2], block[3], block[4], block[5], block[6] , block[7], block[8], block[9]); _tprintf(TEXT("\n")); writeByte(buffer, count, b); writeShort(buffer, count, s); writeLong(buffer, count, l); writeFloat(buffer, count, f); writeDouble(buffer, count, d); writeString(buffer, count, string); writeBlock(buffer, count, block, 10); /* reset count to zero to read from start of buffer */ count = 0; readByte(buffer, count, b); readShort(buffer, count, s); readLong(buffer, count, l); readFloat(buffer, count, f); readDouble(buffer, count, d); readString(buffer, count, string); readBlock(buffer, count, block, 10); _tprintf(TEXT("read byte %d from buffer\n"), b); _tprintf(TEXT("read short %#x from buffer\n"), s); _tprintf(TEXT("read long %#lx from buffer\n"), l); _tprintf(TEXT("read float %f from buffer\n"), f); _tprintf(TEXT("read double %.14f from buffer\n"), d); _tprintf(TEXT("read string %s from buffer\n"), string); _tprintf(TEXT("read block %d%d%d%d%d%d%d%d%d%d from buffer\n"), block[0] , block[1], block[2], block[3], block[4], block[5], block[6] , block[7], block[8], block[9]); nlShutdown(); return 0; }