Thread: need help with this prog

  1. #1
    Unregistered
    Guest

    need help with this prog

    ok i get 2 errors when i try to compile this code. the 2 errors are:

    Error: (44,73):Cannot convert 'int *' to 'const char *'
    Error: (44,73):Type mismatch in parameter 'optval' in call to '__stdcall setsockopt(unsigned int,int,int,const char *,int)'

    here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <winsock.h>
    #define PORT 9034
    
        int main(void)
    
        {
            WSADATA wsaData;   // if this doesn't work
            //WSAData wsaData; // then try this instead
    
            if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
                fprintf(stderr, "WSAStartup failed.\n");
    
    
            }
    
        {
            fd_set master;   // master file descriptor list
            fd_set read_fds; // temp file descriptor list for select()
            struct sockaddr_in myaddr;     // server address
            struct sockaddr_in remoteaddr; // client address
            int fdmax;        // maximum file descriptor number
            int listener;     // listening socket descriptor
            int newfd;        // newly accept()ed socket descriptor
            char buf[256];    // buffer for client data
            int nbytes;
            int yes=1;        // for setsockopt() SO_REUSEADDR, below
            int addrlen;
            int i, j;
    
            FD_ZERO(&master);    // clear the master and temp sets
            FD_ZERO(&read_fds);
    
            // get the listener
            if ((listener = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                perror("socket");
                exit(1);
            }
    
            // lose the pesky "address already in use" error message
           if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes,
                                                                sizeof(int)) == -1) {
                perror("setsockopt");
                exit(1);
            }
    
            // bind
            myaddr.sin_family = AF_INET;
            myaddr.sin_addr.s_addr = INADDR_ANY;
            myaddr.sin_port = htons(PORT);
            memset(&(myaddr.sin_zero), '\0', 8);
            if (bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1) {
                perror("bind");
                exit(1);
            }
    
            // listen
            if (listen(listener, 10) == -1) {
                perror("listen");
                exit(1);
            }
    
            // add the listener to the master set
            FD_SET(listener, &master);
    
            // keep track of the biggest file descriptor
            fdmax = listener; // so far, it's this one
    
            // main loop
            for(; {
                read_fds = master; // copy it
                if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
                    perror("select");
                    exit(1);
                }
    
                // run through the existing connections looking for data to read
                for(i = 0; i <= fdmax; i++) {
                    if (FD_ISSET(i, &read_fds)) { // we got one!!
                        if (i == listener) {
                            // handle new connections
                            addrlen = sizeof(remoteaddr);
                            if ((newfd = accept(listener, (struct sockaddr *)&remoteaddr,
                                                                     &addrlen)) == -1) { 
                                perror("accept");
                            } else {
                                FD_SET(newfd, &master); // add to master set
                                if (newfd > fdmax) {    // keep track of the maximum
                                    fdmax = newfd;
                                }
                                printf("selectserver: new connection from %s on "
                                    "socket %d\n", inet_ntoa(remoteaddr.sin_addr), newfd);
                            }
                        } else {
                            // handle data from a client
                            if ((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0) {
                                // got error or connection closed by client
                                if (nbytes == 0) {
                                    // connection closed
                                    printf("selectserver: socket %d hung up\n", i);
                                } else {
                                    perror("recv");
                                }
                                closesocket(i); // bye!
                                FD_CLR(i, &master); // remove from master set
                            } else {
                                // we got some data from a client
                                for(j = 0; j <= fdmax; j++) {
                                    // send to everyone!
                                    if (FD_ISSET(j, &master)) {
                                        // except the listener and ourselves
                                        if (j != listener && j != i) {
                                            if (send(j, buf, nbytes, 0) == -1) {
                                                perror("send");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
            return 0;
        }
         }
    so whats wrong?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Error: (44,73):Cannot convert 'int *' to 'const char *'
    That parameter should be defined as a const void *, but on my system and yours too apparently it is defined as char *. You might be able to get away with a char * cast, give it a try and see how it works out.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Your variable yes is of type int. The function expects a char *.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly amature question: prog errors
    By geek@02 in forum Tech Board
    Replies: 1
    Last Post: 10-03-2008, 01:35 PM
  2. Getting input from another prog?
    By Badman3k in forum C Programming
    Replies: 4
    Last Post: 11-11-2004, 02:58 AM
  3. an option at the end of this prog to ask if I want to run again
    By bandito9111 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2003, 02:30 PM
  4. Try my prog...
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-09-2002, 07:43 AM
  5. password prog
    By ihsir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-06-2002, 06:39 AM