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) { 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; }
void mainServerLoop(NLsocket sock) { NLsocket client[MAX_CLIENTS]; NLint clientnum = 0; NLbyte string[NL_MAX_STRING_LENGTH]; memset(client, 0, sizeof(client)); while(1) { NLint i; NLbyte buffer[128]; /* check for a new client */ NLsocket newsock = nlAcceptConnection(sock); if(newsock != NL_INVALID) { NLaddress addr; nlGetRemoteAddr(newsock, &addr); client[clientnum] = newsock; printf("Client %d connected from %s\n", clientnum, nlAddrToString(&addr, string)); clientnum++; } else { if(nlGetError() == NL_SYSTEM_ERROR) { printErrorExit(); } } /* loop through the clients and read the packets */ for(i=0;i<clientnum;i++) { if(nlRead(client[i], buffer, sizeof(buffer)) > 0) { NLint j; buffer[127] = 0; /* null terminate the char string */ printf("Client %d sent %s\n", i, buffer); for(j=0;j<clientnum;j++) { if(i != j) nlWrite(client[j], buffer, strlen(buffer)); } } } sleep(0); } }
int main(int argc, char **argv) { NLsocket sock; NLaddress addr; NLbyte buffer[4096]; int f; NLint count, total = 0; NLint crfound = 0; NLint lffound = 0; if (argc != 4) { printf("\nSyntax: getfile ServerName FullPath LocalFile\n"); return 1; } if(nlInit() == NL_FALSE) printErrorExit(); if(nlSelectNetwork(NL_IP) == NL_FALSE) printErrorExit(); nlEnable(NL_SOCKET_STATS); nlEnable(NL_BLOCKING_IO); nlGetAddrFromName(argv[1], &addr); /* use the standard HTTP port */ nlSetAddrPort(&addr, 80); printf("Server address is %s\n\n", nlAddrToString(&addr, buffer)); /* open the socket and connect to the server */ sock = nlOpen(0, NL_TCP); if(sock == NL_INVALID) printErrorExit(); if(nlConnect(sock, &addr) == NL_FALSE) { printErrorExit(); } printf("Connected\n"); /* open the local file */ f = open(argv[3], O_BINARY|O_CREAT|O_TRUNC|O_RDWR, S_IWRITE | S_IREAD); if(f < 0) { printf("Could not open local file\n"); printErrorExit(); } /* now let's ask for the file */ #ifdef TEST_GZIP /* this is for my own personal use to test compressed web pages */ sprintf(buffer, "GET %s HTTP/1.1\r\nHost:%s\r\nAccept: */*\r\nAccept-Encoding: gzip\r\nUser-Agent: HawkNL sample program Getfile\r\n\r\n" , argv[2], argv[1]); #else sprintf(buffer, "GET %s HTTP/1.0\r\nHost:%s\r\nAccept: */*\r\nUser-Agent: HawkNL sample program Getfile\r\n\r\n" , argv[2], argv[1]); #endif if(nlWrite(sock, (NLvoid *)buffer, (NLint)strlen(buffer)) == NL_INVALID) { close(f); printErrorExit(); } /* receive the file and write it locally */ while(1 == 1) { count = nlRead(sock, (NLvoid *)buffer, (NLint)sizeof(buffer) - 1); if(count < 0) { NLint err = nlGetError(); /* is the connection closed? */ if(err == NL_MESSAGE_END) { break; } else { close(f); printErrorExit(); } } total += count; if(count > 0) { /* parse out the HTTP header */ if(lffound < 2) { int i; for(i=0;i<count;i++) { if(buffer[i] == 0x0D) { crfound++; } else { if(buffer[i] == 0x0A) { lffound++; } else { /* reset the CR and LF counters back to 0 */ crfound = lffound = 0; } } if(lffound == 2) { /* i points to the second LF */ /* NUL terminate the header string and print it out */ buffer[i] = buffer[i-1] = 0x0; printf(buffer); /* write out the rest to the file */ write(f, &buffer[i+1], count - i - 1); break; } } if(lffound < 2) { /* we reached the end of buffer, so print it out */ buffer[count + 1] = 0x0; printf(buffer); } } else { write(f, buffer, count); printf("received %d bytes at %d bytes per second\r", total, nlGetSocketStat(sock, NL_AVE_BYTES_RECEIVED)); } } } close(f); 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; }