Ejemplo n.º 1
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;
}
Ejemplo n.º 2
0
bool NetAddrToString(const NetworkAddr& addr, std::string& string) {
	// TODO: safty here for buffer
	char buf[256];
	NLchar* res = nlAddrToString(getNLaddr(addr), buf);
	if(res) {
		fix_markend(buf);
		string = buf;
		return true;
	} else
		return false;
}
Ejemplo n.º 3
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);
    }
}
Ejemplo n.º 4
0
static void mainTestLoop(NLsocket sock)
{
    while(1 == 1)
    {
        NLbyte      buffer[100];
        NLbyte      string[NL_MAX_STRING_LENGTH];
        NLaddress   addr;
        char        hello[] = "Hello";

        nlWrite(sock, (NLvoid *)hello, (NLint)sizeof(hello));

        while(nlRead(sock, (NLvoid *)buffer, (NLint)sizeof(buffer)) > 0)
        {
            nlGetRemoteAddr(sock, &addr);
            buffer[99] = (NLbyte)0;
            printf("received %s from %s, packet #%d\n", buffer, nlAddrToString(&addr, string), nlGetInteger(NL_PACKETS_RECEIVED));
        }
        sleep(1);
    }
}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
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;
}