socket programming c++

Hi all, I am new here.

I am try to make an easy c++ code to manage socket communication in OSX.

My trouble is about accept() syscall!
It seem that it work well with .c code, but with .cpp don't!

Here there are the librarys that I am using.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>     
#include <string>       
#include <netdb.h>      
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <stdlib.h>     
#include <unistd.h>     
#include <sys/types.h>  
#include <sys/ipc.h>    
#include <sys/sem.h>    
#include <sys/shm.h>    
#include <fcntl.h>      
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <cstring>
#include <stdio.h>

#define C_MAX 10
#define BUFF_SIZE 8192  
#define F_NAME_L 1024


Here there is the function that I am using to initialize the socket:

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
int INIT_SOCKET(int port_number) {
	struct sockaddr_in server;
	int my_socket, bind_if;
	
	cout << "Socket buildind.......................\n";
	my_socket = socket(AF_INET, SOCK_STREAM, 0);      
	if (my_socket == -1)  {                           
        perror("SOCKET ERROR BUILDING OCCURRED!!! Probably another socket is using this port.\n");
  		exit(1);
    }
	cout << "Socket built!!!\n";
    cout << "I am initializing the socket...\n";
	server.sin_family = AF_INET;                      
	server.sin_port = port_number;                    
	server.sin_addr.s_addr = INADDR_ANY;                

    bind_if = bind(my_socket, (struct sockaddr *)&server, sizeof(server));
	if (bind_if== -1) {
        perror("ERROR OCCURRED IN BIND CALL!!!\n");
  		exit(1);
    }
	
	listen(my_socket, C_MAX);   
	cout << "Socket initializzation COMPLETED...!\n";
	
	return(my_socket);          
}


Here the part of main() where I have problems:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
MAIN(){

   int gate_n = 6666;
   int main_socket, ds_sock, ds_sock_bis;
   struct sockaddr client;
   int main_socket_bis, dim;
   char buff[BUFF_SIZE];
   char buffer_nome_file[F_NAME_L];
   int ret, status;
   dim = sizeof(client);  
   main_socket = INIT_SOCKET(gate_n); 
   dim = sizeof(client);
   cout << "Waiting for connections..........\n";   
   while(1) {
      main_socket_bis = accept(main_socket, &client, &dim);
      while(accept(main_socket, &client, &dim)) == -1);
      cout << "Conenction established!\n";
      ...
      ...


The problem is that the compiler (Xcode) can't compile accept() sys call with file .cpp.
If I use .c it works well!
Some suggestion?


Thank,
Fabio.
You really should declare your variables where they're used. As simple as that code is, it's not easy to read.

You shouldn't have a problem calling accept() from C++. I'm not clear how wrapping up socket initialisation in INIT_SOCKET helps. There are different kinds of sockets, each has its own method of initialisation.

Why MAIN()? Why are you using uppercase function names? How would you seperate from from macros?
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <string>  
#include <netdb.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <stdlib.h>     
#include <unistd.h>     
#include <sys/types.h>  
#include <sys/ipc.h>    
#include <sys/sem.h>    
#include <sys/shm.h>    
#include <fcntl.h>      
#include <vector>
#include <cstring>
#include <stdio.h>
#include <dirent.h>
#include <errno.h>

using namespace std;
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------

#define C_MAX 10        
#define BUFF_SIZE 8192  
#define F_NAME_L 1024   
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------

int init_socket(int port_number);
int init_socket(int port_number) {
	struct sockaddr_in server;
	int my_socket, bind_if;
	// Socket Building
	my_socket = socket(AF_INET, SOCK_STREAM, 0);    
	if (my_socket == -1)  {                         
  		exit(1);
    }

	server.sin_family = AF_INET;                     
	server.sin_port = port_number;                   
	server.sin_addr.s_addr = INADDR_ANY;             
    
    bind_if = bind(my_socket, (struct sockaddr *)&server, sizeof(server));
	if (bind_if== -1) {
        exit(1);
    }
    listen(my_socket, C_MAX);  
    return(my_socket);       
}
               
//-----------------------------------------------------------------------------------------------
               
int getdir(string, vector<string> &);
int getdir(string dir, vector<string> &files){
            DIR *dp;
            struct dirent *dirp;           
            while ((dirp = readdir(dp)) != NULL) {
                files.push_back(string(dirp->d_name));
            }
            closedir(dp);
            return 0;          
}
               
//-----------------------------------------------------------------------------------------------

void send (char*, int);
void send (char* file_name, int socket) {                   
     int data, file_descriptor;
     char buffer[BUFF_SIZE];                   
     file_descriptor = open (file_name, O_RDONLY);
     if (file_descriptor == -1)  {       
         exit(1);
     }
                   
    while ((data = read (file_descriptor, buffer, sizeof(buffer))) > 0) {           
          if (write (socket, buffer, data) == -1){
              exit(1);
          }
    }
                   
    close (file_descriptor);
}
               
//-----------------------------------------------------------------------------------------------

void send_list (int socket);
void send_list (int socket) {
                   
     int data;
     int i=0;
     string dir = string("//Users//fabiocremona//Desktop//Files test");
     vector<string> files = vector<string>();
     getdir(dir,files);  
     i = sizeof(files);  
     while (i > 0) {           
        char *buffer = (char*)files[i].c_str();
        data = sizeof(*buffer);
        if (write (socket, buffer, data) == -1){
            exit(1);
            }
        i--;
     }
}
//----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------
// MAIN PROGRAM:
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------

int main() {
    int num_porta = 6666;                                
    int main_socket, ds_sock, ds_sock_bis, data;         
    struct sockaddr client;                              
    int main_socket_bis, dim, ds_file, byte_readed, pid; 
    char buff[BUFF_SIZE];                   
    char buffer_nome_file[F_NAME_L];     
    int ret;            
    long sem_ID;        
    long sem_KEY = 50;  
    struct sembuf oper;
    dim = sizeof(client);
    main_socket = init_socket(num_porta);
    dim = sizeof(client);
    while(1) {
        while(main_socket_bis = accept(main_socket, &client, &dim) == -1);                      
        pid = fork();
        if (pid == -1) {
            exit(1);
        }
    if (pid == 0) {	
        send_list(main_socket_bis);
        while ((data = read(main_socket_bis, buff, sizeof(buff))) > 0);
        send (buff, main_socket_bis);
        // Close the socket
        close(main_socket_bis);
        cout << "Connection closed\n\n";
        cout << "Waiting for new connections...\n\n";
        exit(0);
        } // if(PID==0)
    else    {
            close(main_socket_bis);
            }
    } // while(1)
                   
    return 0;
                   
} //main()    
               



This is the entire code .cpp.
Xcode give me error on accept ().
If I rename the file in .c, I have c++ structures that don't work anymore, but no more error on accept().
Someone can help me?
1
2
3
4
5
g++     x.cpp   -o x
x.cpp: In function ‘int main()’:
x.cpp:125: error: invalid conversion from ‘int*’ to ‘socklen_t*’
x.cpp:125: error:   initializing argument 3 of ‘int accept(int, sockaddr*, socklen_t*)’
make: *** [x] Error 1


dim should be a socklen_t, not an int.
thank you very much :)
Topic archived. No new replies allowed.