socklen_t error

Nov 14, 2010 at 12:27am
Hello,


i am trying to execute the code below :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 /* A simple server using TCP
   **************************/
#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sstream>
#include <fstream>

#define PORTNUM 6741


void error(char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd , clilen, n;
	 struct sockaddr_in serv_addr, cli_addr;
     char buff[256];
	 
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
	 
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
		
     bzero((char *) &serv_addr, sizeof(serv_addr));
	 
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(PORTNUM);
	 
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
			  
     listen(sockfd,5);
	 
     clilen = sizeof(cli_addr);
	 
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr,  &clilen);
				 
     if (newsockfd < 0) 
          error("ERROR on accept");
		  
     bzero(buff,256);
	 
     n = read(newsockfd,buff,255);

     return 0; 
}
  


and i receive the error:

1
2
3
 s.cpp: In function `int main(int, char**)': 
s.cpp:50: error: invalid conversion from `int*' to `socklen_t*'
s.cpp:50: error:   initializing argument 3 of `int accept(int, sockaddr*, socklen_t*)' 


i tried(casting) in line 51:

 
newsockfd = accept(sockfd, struct sockaddr *) &cli_addr,  (socklen_t *)&clilen);


but it is not working .

The stange is that if you execute the code without including <sstream> and <fstream> library then there in no error !

Any help will be appreciated .

Thanks a lot.
Last edited on Nov 14, 2010 at 12:33am
Nov 14, 2010 at 10:06am
well then what's wrong with declaring socklen_t clilen; when the compiler wants it that badly
Nov 14, 2010 at 2:18pm
OK,

first of all, thanks for the reply CODER777 .

finally, the problem was that i used gcc and NOT g++ . Both of the solutions are fine.
Last edited on Nov 14, 2010 at 2:20pm
Topic archived. No new replies allowed.