Please help me.. invalid char...

This is a discussion on Please help me.. invalid char... within the C++ Programming forums, part of the General Programming Boards category; I'm getting invalid chars.. can you help me fix it? Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    Question Please help me.. invalid char...

    I'm getting invalid chars.. can you help me fix it?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdint.h>
    #include <time.h>
    
    #ifdef WIN32
        #include <winsock.h>
        #include "winerr.h"
    
        #define close   closesocket
        #define sleep   Sleep
        #define ONESEC  1000
    #else
        #include <unistd.h>
        #include <sys/socket.h>
        #include <sys/types.h>
        #include <arpa/inet.h>
        #include <netinet/in.h>
        #include <netdb.h>
    
        #define ONESEC  1
    #endif
    
    
    
    #define VER         "0.1"
    #define PORT        7777
    #define BUFFSZ      8192
    
    
    
    void sampinfo(u_char *data, int size);
    int put08(u_char *data, int num);
    int get08(u_char *data, int *num);
    int put16(u_char *data, int num);
    int get16(u_char *data, int *num);
    int put32(u_char *data, int num);
    int get32(u_char *data, int *num);
    int putsc(u_char *data, u_char *src);
    int getsc(u_char *data, u_char **dst);
    int putmm(u_char *data, u_char *src, int len);
    int getmm(u_char *data, u_char *dst, int len);
    
    void delimit(u_char *data);
    int send_recv(int sd, u_char *in, int insz, u_char *out, int outsz, int err);
    int timeout(int sock, int secs);
    u_int resolv(char *host);
    void std_err(void);
    
    
    
    struct  sockaddr_in peer;
    
    
    
    int main(int argc, char *argv[]) {
        struct  sockaddr_in peerl;
        int     sd,
                pl,
                len;
        u_short port  = PORT;
        u_char  buff[BUFFSZ],
                pass[64],
                *p;
    
    #ifdef WIN32
        WSADATA    wsadata;
        WSAStartup(MAKEWORD(1,0), &wsadata);
    #endif
    
        setbuf(stdout, NULL);
    
        fputs("\n"
            "Hey this is the test "VER"\n"
            "\n", stdout);
    
        if(argc < 2) {
            printf("\n"
                "Usage: %s <host> [port(%hu)]\n"
                "\n"
                "\n", argv[0], port);
            exit(1);
        }
    
        if(argc > 2) port = atoi(argv[2]);
        peer.sin_addr.s_addr  = resolv(argv[1]);
        peer.sin_port         = htons(port);
        peer.sin_family       = AF_INET;
    
        peerl.sin_addr.s_addr = INADDR_ANY;
        peerl.sin_port        = htons(time(NULL));
        peerl.sin_family      = AF_INET;
    
        printf("- target   %s : %hu\n",
            inet_ntoa(peer.sin_addr), ntohs(peer.sin_port));
    
        sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if(sd < 0) std_err();
        p = buff;
        p += putmm(p, "SAMP", 4);
        p += put32(p, peer.sin_addr.s_addr);
        p += put16(p, ntohs(peer.sin_port));
        p += put08(p, 'i');
        len = send_recv(sd, buff, p - buff, buff, BUFFSZ, 1);
        close(sd);
        sampinfo(buff, len);
    
        pass[0] = 0;
    
        printf("- start thread:");
        for(;;) {
            pl = 1;
            for(;;) {
                printf("\n  chat: ");
    
                sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
                if(sd < 0) std_err();
                do {
                    peerl.sin_port++;
                } while(bind(sd, (struct sockaddr *)&peerl, sizeof(peerl)) < 0);
    
                p = buff;
                p += put08(p, 3);
                if(pass[0]) {
                    p += putsc(p, pass);
                }
    
                len = send_recv(sd, buff, p - buff, buff, BUFFSZ, 1);
    
                close(sd);
    
                if(buff[0] == 20) break;
                if(buff[0] != 18) {
                    if((buff[0] == 19) || (buff[0] == 25)) {
                    //             \wrong             \not set
                        printf("\n- server is protected, insert the password:\n  ");
                        fflush(stdin);
                        fgets(pass, sizeof(pass), stdin);
                        delimit(pass);
                        continue;
                    }
                    printf("\nError: unknown packet reply (%d)\n", buff[0]);
                    exit(1);
                }
                printf("%d ", pl);
                pl++;
                if(pl > 9999) pl = 0;   // lame slow-networks work-around
            }
    
            printf("Full chat");
            sleep(ONESEC);
        }
    
        return(0);
    }
    
    
    
    void sampinfo(u_char *data, int size) {
        int     info,
                namelen,
                modelen,
                maplen,
                pwd,
                pl,
                maxpl;
        u_char  *p,
                *name,
                *mode,
                *map;
    
        p = data;
        p += 4 + 4 + 2;
        p += get08(p, &info);
        if(info != 'i') return;
        p += get08(p, &pwd);
        p += get16(p, &pl);
        p += get16(p, &maxpl);
        p += get32(p, &namelen);
        name = p;       p += namelen;
        p += get32(p, &modelen);
        mode = p;       p += modelen;
        p += get32(p, &maplen);
        map = p;        p += maplen;    
        printf(
            "\n"
            "  name:      %.*s\n"
            "  mode:      %.*s\n"
            "  map:       %.*s\n"
            "  chat:   %hu/%hu\n"
            "  password:  %s\n"
            "\n",
            namelen, name,
            modelen, mode,
            maplen,  map,
            pl,      maxpl,
            pwd ? "on" : "off");
    }
    
    
    
    int put08(u_char *data, int num) {
        data[0] = num;
        return(1);
    }
    
    
    
    int get08(u_char *data, int *num) {
        if(num) {
            *num = data[0];
        }
        return(1);
    }
    
    
    
    int put16(u_char *data, int num) {
        data[0] = num;
        data[1] = num >> 8;
        return(2);
    }
    
    
    
    int get16(u_char *data, int *num) {
        if(num) {
            *num = data[0] | (data[1] << 8);
        }
        return(2);
    }
    
    
    
    int put32(u_char *data, int num) {
        data[0] = num;
        data[1] = num >> 8;
        data[2] = num >> 16;
        data[3] = num >> 24;
        return(4);
    }
    
    
    
    int get32(u_char *data, int *num) {
        if(num) {
            *num = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
        }
        return(4);
    }
    
    
    
    int putsc(u_char *data, u_char *src) {
        return(sprintf(data, "%s", src) + 1);
    }
    
    
    
    int getsc(u_char *data, u_char **dst) {
        *dst = data;
        return(strlen(data) + 1);
    }
    
    
    
    int putmm(u_char *data, u_char *src, int len) {
        memcpy(data, src, len);
        return(len);
    }
    
    
    
    int getmm(u_char *data, u_char *dst, int len) {
        memcpy(dst, data, len);
        return(len);
    }
    
    
    
    void delimit(u_char *data) {
        while(*data && (*data != '\n') && (*data != '\r')) data++;
        *data = 0;
    }
    
    
    
    int send_recv(int sd, u_char *in, int insz, u_char *out, int outsz, int err) {
        int     retry,
                len;
    
        if(in && !out) {
            if(sendto(sd, in, insz, 0, (struct sockaddr *)&peer, sizeof(peer))
              < 0) std_err();
            return(0);
    
        } else if(in) {
            for(retry = 3; retry; retry--) {
                if(sendto(sd, in, insz, 0, (struct sockaddr *)&peer, sizeof(peer))
                  < 0) std_err();
                if(!timeout(sd, 1)) break;
            }
    
            if(!retry) {
                goto timeout_received;
            }
    
        } else {
            if(timeout(sd, 3) < 0) {
                goto timeout_received;
            }
        }
    
        len = recvfrom(sd, out, outsz, 0, NULL, NULL);
        if(len < 0) std_err();
        return(len);
    
    timeout_received:
        if(err) {
            printf("\nError: socket timeout, no reply received\n\n");
            exit(1);
        }
        return(-1);
    }
    
    
    
    int timeout(int sock, int sec) {
        struct  timeval tout;
        fd_set  fd_read;
        int     err;
    
        tout.tv_sec  = sec;
        tout.tv_usec = 0;
        FD_ZERO(&fd_read);
        FD_SET(sock, &fd_read);
        err = select(sock + 1, &fd_read, NULL, NULL, &tout);
        if(err < 0) std_err();
        if(!err) return(-1);
        return(0);
    }
    
    
    
    u_int resolv(char *host) {
        struct  hostent *hp;
        u_int   host_ip;
    
        host_ip = inet_addr(host);
        if(host_ip == INADDR_NONE) {
            hp = gethostbyname(host);
            if(!hp) {
                printf("\nError: Unable to resolv hostname (%s)\n", host);
                exit(1);
            } else host_ip = *(u_int *)hp->h_addr;
        }
        return(host_ip);
    }
    
    
    
    #ifndef WIN32
        void std_err(void) {
            perror("\nError");
            exit(1);
        }
    #endif

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    your code is too long for me to read it through in 30 sec...
    Could you post smalles possible compileable example still representing your problem?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 06:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21