c++ sockets

This is a discussion on c++ sockets within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, I am trying to write a simple echo server as I have just started using sockets in c++. I ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72

    c++ sockets

    Hi, I am trying to write a simple echo server as I have just started using sockets in c++. I am using beej's guide, which was written for c but I was told that the functions are the same in c++. However I get the following error when I try to compile the server program.

    Code:
    server.cpp: In function ‘int main()’:
    server.cpp:34: error: expected initializer before ‘struct’
    server.cpp:43: error: ‘server_addr’ was not declared in this scope
    server.cpp:53: error: ‘list_check’ was not declared in this scope
    server.cpp:59: error: ‘sin_size’ was not declared in this scope
    server.cpp:63: error: comparison between distinct pointer types ‘std::string*’ and ‘const char*’ lacks a cast
    I understand what I am doing wrong for the last error and I will fix that, wanted to try and see if I could just use strings as I find them easier to work with.

    Code:
    #include <iostream>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {
            struct sockaddr_in
            {
                    short int          sin_family;  // Address family
                    unsigned short int sin_port;    // Port number
                    struct in_addr     sin_addr;    // Internet address
                    unsigned char      sin_zero[8]; // Same size as struct sockaddr
            };
    
            int sockfd, clientsock;
            //int len1, len2, len3;
    
            string *buf;
    
            int bind_check, list_check //variables used in error checking
    
            //char *msg1 = "Connected to server...";
            //char *msg2 = "Initializing data transfer...";
            //char *msg3 = "Ready. Commence data transfer.";
            //len1 = strlen(msg1);
            //len2 = strlen(msg2);
            //len3 = strlen(msg3);
    
            struct sockaddr_in server_addr;
            struct sockaddr_in client_addr;
            sockfd=socket(PF_INET, SOCK_STREAM, 0 );
            if ( sockfd == -1 )
            {
                    cout<<"Could not bind socket.\n";
                    return 1;
            }
    
            server_addr.sin_family = AF_INET;
            server_addr.sin_port = htons(31337);
            server_addr.sin_addr.s_addr = INADDR_ANY;
            memset(server_addr.sin_zero, '\0', sizeof server_addr.sin_zero);
    
            bind_check=bind( sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr) );
            if ( bind_check == -1 )
            {
                    cout<<"Could not bind socket.\n";
            }
            list_check=listen( sockfd, 5 );
            if( list_check == -1 )
            {
                    cout<<"Could not listen() on the socket"<<sockfd<<"\n";
            }
    
            sin_size = sizeof( struct sockaddr_in );
    
            clientsock=accept( sockfd, (struct sockaddr *)&client_addr, &sin_size );
    
            while ( buf != "exit" )
            {
                    if ( recv( clientsock, buf, 500, 0 ) == 0 )
                    {
                            cout<<"Remote host closed connection. Firewall?";
                    }
            }
            close(sockfd);
            close(clientsock);
    }
    I have no idea what I am doing wrong, I'm still pretty new to c++, so any advice will be appreciated.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Why are you declaring struct sockaddr_in yourself? Don't do that.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    I decided to put it in to try and fix the 'expected initializer before 'struct'' error, but it didn't work. I took it out and I'm still getting these compile errors:

    Code:
    server.cpp: In function ‘int main()’:
    server.cpp:26: error: expected initializer before ‘struct’
    server.cpp:35: error: ‘server_addr’ was not declared in this scope
    server.cpp:45: error: ‘list_check’ was not declared in this scope
    server.cpp:51: error: ‘sin_size’ was not declared in this scope
    server.cpp:55: error: comparison between distinct pointer types ‘std::string*’ and ‘const char*’ lacks a cast
    Calef13
    Last edited by Calef13; 06-26-2007 at 10:52 AM. Reason: clearer answer

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Code:
    int bind_check, list_check //variables used in error checking
    Missing terminating semi-colon, methinks.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    ok, those were pretty stupid mistakes, should have seen them :P

    I changed the source and there are only a few errors left:

    Code:
    server.cpp: In function ‘int main()’:
    server.cpp:53: error: invalid conversion from ‘int*’ to ‘socklen_t*’
    server.cpp:53: error:   initializing argument 3 of ‘int accept(int, sockaddr*, socklen_t*)’
    Code:
    #include <iostream>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {
            int sockfd, clientsock, sin_size;
            //int len1, len2, len3;
    
            char *buf;
    
            int bind_check, list_check; //variables used in error checking
    
            //char *msg1 = "Connected to server...";
            //char *msg2 = "Initializing data transfer...";
            //char *msg3 = "Ready. Commence data transfer.";
            //len1 = strlen(msg1);
            //len2 = strlen(msg2);
            //len3 = strlen(msg3);
    
            struct sockaddr_in server_addr;
            struct sockaddr_in client_addr;
            sockfd=socket(PF_INET, SOCK_STREAM, 0 );
            if ( sockfd == -1 )
            {
                    cout<<"Could not bind socket.\n";
                    return 1;
            }
    
            server_addr.sin_family = AF_INET;
            server_addr.sin_port = htons(31337);
            server_addr.sin_addr.s_addr = INADDR_ANY;
            memset(server_addr.sin_zero, '\0', sizeof server_addr.sin_zero);
    
            bind_check=bind( sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr) );
            if ( bind_check == -1 )
            {
                    cout<<"Could not bind socket.\n";
            }
            list_check=listen( sockfd, 5 );
            if( list_check == -1 )
            {
                    cout<<"Could not listen() on the socket"<<sockfd<<"\n";
            }
    
            sin_size = sizeof( struct sockaddr_in );
    
            clientsock=accept( sockfd, (struct sockaddr *)&client_addr, &sin_size );
    
            while ( buf != "exit" )
            {
                    if ( recv( clientsock, buf, 500, 0 ) == 0 )
                    {
                            cout<<"Remote host closed connection. Firewall?";
                    }
            }
            close(sockfd);
            close(clientsock);
    }

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by Calef13 View Post
    Code:
    server.cpp: In function ‘int main()’:
    server.cpp:53: error: invalid conversion from ‘int*’ to ‘socklen_t*’
    This error wouldn't have happened in C. C++ is pickier about types. Define sin_size as a socklen_t instead of a plain old int. The second error is probably due to the same problem.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    That fixed it. Thanks a lot for your help everyone. It compiled, now all I need to do is write a client :P

    Calef13

  8. #8
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Hi,

    I got the server working, but my client fails at the connect() function. I'm stumped again, I'm afraid. My firewall is not the problem, and I'm running it on localhost anyway.

    ok, the server works perfectly, I just tested it with telnet, so the client is definetly the problem. Posting tht much code was a mistake, sorry.

    update: perror() claims "connect: Socket operation on non-socket", so I'll check again to make sure I'm not messing up the socket call.

    Client:

    Code:
    #include<iostream>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    
    #define DEST_IP "127.0.0.1"
    #define MAX 1024
    
    using namespace std;
    
    int main()
    {
            char buf[MAX];
    
            struct sockaddr_in dest_addr;
    
            int sock, len, check_recv, check_send;
    
            if ( sock=socket(PF_INET, SOCK_STREAM, 0 ) == -1 )
            {
                    cout<<"Error, could not open socket\n";
            }
    
            dest_addr.sin_family = AF_INET;
            dest_addr.sin_port = htons(31337);
            dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
            memset( dest_addr.sin_zero, '\0', sizeof( dest_addr.sin_zero ) );
    
            if ( connect( sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == -1 )
            {
                    cout<<"Could not connect to remote host.\n";
            }
    
            cin.getline( buf, MAX, '\n' );
    
            len = sizeof(buf);
    
            while(1)
            {
    
                    check_send=send( sock, buf, len, 0 );
                    switch ( check_send )
                    {
                            case -1:
                                    cout<<"Error sending, bailing out...\n";
                                    break;
                            default:
                                    cout<<check_send<<"Bytes sent\n";
                                    break;
                    }
                    check_recv=recv( sock, buf, MAX, 0 );
                    switch ( check_recv )
                    {
                            case 0:
                                    cout<<"Remote host closed connection.\n";
                                    break;
                            case -1:
                                    cout<<"Error receiving data.\n";
                                    break;
                            default:
                                    cout<<check_recv<<"Bytes received.\n";
                                    break;
                    }
            }
            close(sock);
    }

    The client fails after running the connect() function as I get the 'Could not connect to remote host' error message.

    Any help would be greatly appreciated,

    Calef13
    Last edited by Calef13; 06-26-2007 at 06:57 PM. Reason: post made more relevant and less annoying.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    1

    Thumbs up problem I faced with this code

    why this message comes to me?
    server.cpp: In function ‘int main()’:

    and was not declared in this scope as well

    please help me by any hints .


    Code:
    #include <time.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sys/types.h>      
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <iostream>
    #include <arpa/inet.h>
    
    using namespace std;
    
    extern void *serve(void *);
    
    int main(int argc, char *argv[])
    {
      int port = htons(atoi(argv[1]));
      int ssock, connsock, s;
      
      struct sockaddr_in myaddr;
      ssock = socket(AF_INET, SOCK_STREAM, 0);
      myaddr.sin_family = AF_INET;
      myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
      myaddr.sin_port = port;
      s=bind(ssock,(struct sockaddr *)&myaddr,sizeof(myaddr));
      if(s < 0)
        
        {
          cerr << "cant bind\n"; exit(1);
        }
      
      listen(ssock,5); 
      
      pthread_t th;
      int r;
      while(true)
        
        {
          connsock=accept(ssock,0,0);
          r=pthread_create(&th,0,serve,(void *)connsock);
        }
      
    }
    
    void *serve(void *s)
    {
      int fin, fout, n;
      int sock = (int)s, rc;
      char buffer[2048],req[2048],resource[2048],protoc[2048],contentLengths[2048],dates[2048];  
      char fname[2048] ="pages/", logfile[2048], *exts, *hostip; 
      
      unsigned short port;
      unsigned int cliaddrlen;
      struct sockaddr_in myaddr, cliaddr;
      
      pthread_detach(pthread_self());  
      rc = read(sock,buffer,2048);
      
      n=sscanf(buffer,"%s %s %s", req, resource, protoc);
      
      const char *reply1="HTTP/1.1 501 Method Not Implemented\r\nConnection: Close\r\n\r\n<h1>sorry</h1>\r\n";
      const char *reply2="HTTP/1.1 200 OK\r\nConnection: Close\r\n";
      const char *reply3="HTTP/1.1 404 File Not Found\r\nConnection: Close\r\n\r\n<h1>sorry</h1>\r\n";
      
      const char *reply4="Content-Type: text/html\r\n\r\n";
      const char *reply5="Content-type: image/jpeg\r\n\r\n";
      const char *reply6="Content-type: text/plain\r\n\r\n";
      
      const char *reply8="New connection\r\n\r\n";
      const char *length="Content-Length: %d\r\n";  
      
      struct stat statbuf;
      struct tm tmbuf;
      time_t mtime;
      
      fout=open ("logfile",O_APPEND|O_WRONLY|O_CREAT,0644); 
      time(&mtime);
      
      hostip = inet_ntoa(cliaddr.sin_addr); 
      port = ntohs(cliaddr.sin_port);
      
      
      gmtime_r(&mtime, &tmbuf);//returns the time the connection was made and the type of request.
      sprintf(logfile,req,protoc ,2048 ,"---Connection time: %A, %d-%b-%y %T GMT\r\n",&tmbuf);
      
      write(fout, logfile, strlen(logfile));;
      close (fout);
      
      if(strcmp(req,"GET")==0) // depending on the request it prints out the correct protocol response
        {
          strcat(fname,resource+1);
          if(access(fname,F_OK) <0)
    	{
    	  write(sock,reply3,strlen(reply3));
    	}
          else
    	{ 
    	  write(sock,reply2,strlen(reply2));
    	  exts = strrchr(fname, '.');
    	  
    	  if(strcmp(exts,".cgi")==0)          
    	    { 
    	      int pid;
    	      pid = fork();         
    	      
    	      if (fork == 0)
    		{
    		  dup2(sock,0);
    		  dup2(sock,1);
    		  
    		  execlp(fname, fname,0);
    		  exit(2); 
                   	}
    	    }
    	  else 
    	    {
    	      
    	      stat(fname,&statbuf);// shows the content-length of the file that been sent by the response.
    	      sprintf(contentLengths,length,statbuf.st_size); 
    	      write(sock,contentLengths,strlen(contentLengths));
    	      
    	      mtime = statbuf.st_mtime;//Shows the last modified time if the file exists.
    	      gmtime_r(&mtime, &tmbuf);
    	      strftime(dates,2048 ,"Last-Modified: %A, %d-%b-%y %T GMT\r\n",&tmbuf); 
                  write(sock,dates,strlen(dates));
    	      
    	      // checks the extension of the name of the requested file eg. html.
    	      if(strcmp(exts,"jpg")==0 || strcmp(exts,"jpeg")==0)
                    {
    		  write(sock,reply5,strlen(reply5));
    		}
    	      if (strcmp(exts, "txt")==0)
                    {
                      write(sock,reply6,strlen(reply6));
                    } 
    	      else 
    		{
    		  write(sock,reply4,strlen(reply4));
    		}
    	      fin=open(fname,O_RDONLY);
    	      
    	      rc = read(fin, buffer, 2048);
    	      while(rc > 0) 
    		{
    		  write(sock, buffer, rc);
    		  rc = read(fin, buffer, 2048);
    		}
    	      close(fin);
    	    }
            }
        }
      else 
        
        {
          write(sock,reply1,strlen(reply1));
        }
      close(sock);
      pthread_exit(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  2. Cross platform sockets
    By zacs7 in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-27-2007, 05:16 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 06:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 10:49 AM

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