send data socket TCP,ERRoR sendig: Bad file descriptor

Pages: 12
I want to create data and then send to server . data(information on each sensor) should create in Network.cpp after new sensor added( each sensor is an new network) and send with client.cpp ,I use Inheritance to use function add of Network class to client class or inverse ,but I need help to use function correctly for my program work.when run program I get this error(send failed. Error: Bad file descriptor) what am I missing??
1
2
3
4
5
6
7
8
9
Network.h:
class Network :public client{
public:
	Network();
	void add(Sensor new_sensor);
	virtual ~Network();
private:
	vector<Sensor> sensors;
}


Network.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Network::add(const client &a,Sensor new_sensor) {
	sensors.push_back(new_sensor);
	for (i = 0; i < (int) sensors.size(); i++) {
	unsigned int Header[2] = {0xe1,0xe2};
	uint16_t u16;
	u16 = htons(Header[2]);
	memcpy(packet + 0, &u16, 2);
	unsigned int SensorSn[4] = { 0x1e, 0x25, 0x71, 0x80 };
	uint32_t u32;
	u32 = htons(SensorSn[4]);
	memcpy(packet + 2, &u32, 4);
	uint32_t a32;
	unsigned int SensorTemp[4] = { 0x00, 0x00, 0x00, 0x23 };
	a32 = htons(SensorTemp[4]);
	memcpy(packet + 6, &a32, 4);
        a.send_data();
}

client.cpp:
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
void client::conn(string address, int port) {
	//create socket if it is not already created
	if (sock == -1) {
		//Create socket
		sock = socket(AF_INET, SOCK_STREAM, 0);
		if (sock == -1) {
			perror("Could not create socket");
		}

		cout << "Socket created\n" << endl;
	}
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(1234);
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	while(1){
	int m=connect(sock, (struct sockaddr *) &server_addr, sizeof(server_addr));
	if (m==-1){
		perror("connect failed. Error");       
		continue;
	}
	}
	cout << "Connected\n";  //doesn't show this line!!!
	close(sock);


}
void client::send_data()const {
	int n=send(sock, packet, sizeof(packet), 0)  ;
if (n==-1){
	perror("send failed. Error");  ///my program stop in this line
			exit(1);

}
	cout << "Data send"<<endl;

	close(sock);
}
Last edited on
Where is packet defined? Where is sock defined? What will your client do if your send() returns fewer than sizeof( packet )?
@koothkeeper I need help in my send function,I don't know is it true or not.........
client.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class client {
public:
	client();
	void conn(string, int);
	void send_data() const;
	string receive(int);
	virtual ~client();
private:
	int sock;
	struct sockaddr_in server_addr;
protected:
	char packet[10];
};

} 

Last edited on
All sends and receives should be in a loop: Sends and receives can return fewer characters than what your specify.

In the case of your send_data() function, you will have to look at the return from send(). It could be -1, which you have correctly coded, or it could be < sizeof( packet ). If that happens, you will have to specify the next byte in your packet not sent.
@koothkeeper so you say I change this line
1
2
int n=send(sock, packet, sizeof(packet), 0)  ;
if (n==-1){
to if(send(sock, packet, sizeof(packet), 0)) <sizeof(packet){
????
Not exactly - something like:

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

int
    MaxPacketSize = 4096,
    Pos = 0,
    ThisMuchLeft = sizeof( packet ),
    ThisMuch = 0,
    n = 0;

    while( ThisMuchLeft > 0 )
        {
        if( ThisMuchLeft > Max )
            ThisMuch = Max;
        else
            ThisMuch = ThisMuchLeft;

        n = send( sock, &packet[ Pos ], ThisMuch, 0 );

       if( -1 == n )
            {
            // Handle this error

            break;

            }    /*    if( -1 == n )    */

        if( 0 == n )
            {
            //  The other side is finished sending and/or closed its socket:

            break;

            }    /*    if( 0 == n )    */
            
        ThisMuchLeft -= n;

        Pos += n;

        }    /*    while( ThisMuchLeft > 0 )    */


Last edited on
@koothkeeper tank you for your code but if I set perror("Send failed. Error"); in line 20 and 29 ,I have same error ....
what do you think about close(sock) in any function is it right??
What same error? Yes, you should always close the socket.
(send failed. Error: Bad file descriptor)
Then you must have a problem allocating your socket. Are you checking the return from socket()?
sorry I'm new in socket programming ,what are you mean??
following
I change my send function format then my program work
1
2
3
4
5
6
7
8
9
10
11
bool client::send_data() {
	//Send some data
	 if( send(sock , packet , sizeof( packet ) , 0) < 0)
	    {
	        perror("Send failed");
	        return false;
	    }
	    cout<<"Data send\n";

	    return true;
	}

and I called this function as client::send_data(); so data is send....
now I have an other problem ,, in program each new sensor has packet data then send
in my program for one of them send work
1
2
3
4
5
6
7
1
1 12                 //in
Socket created //out
Connected       //out
Data send       //out
2 20                //in
3 30                //in 

what am I do in Network.cpp to program work like:
1
2
3
4
5
6
7
8
9
1
1 12
Socket created
Connected
Data send
2 20
Data send
3 30
Data send
Last edited on
You need to post all of your code because I'm not sure what you now have. The error you are getting suggests that your socket handle is not good. Please post all of your code.
server.cpp:

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
int main() {
	char packet[30];
	char buffer[20] = "I got your message";
	int conn_sock, comm_sock, n, m;
	struct sockaddr_in server_addr, client_addr;
	if ((conn_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		perror("Couldn't create socket");
		exit(1);
	}
	cout << "Already create socket!!!\n" << endl;

	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(0);
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	memset(&server_addr, 0, sizeof(server_addr));
	if (bind(conn_sock, (struct sockaddr *) &server_addr, sizeof(server_addr))
			== -1) {
		perror("couldn't bind");
		exit(1);
	}
	if (listen(conn_sock, 10) == -1) {
		perror("couldn't listen");
		exit(1);
	}

	cout << "Listening For Connection...\r" << endl;
	socklen_t len = sizeof(server_addr);
	if (getsockname(conn_sock, (struct sockaddr *) &server_addr, &len) == -1)

		perror("getsockname");
	else
		printf("port number %d\n", ntohs(server_addr.sin_port));

	while (1) {
		memset(&client_addr, 0, sizeof(client_addr));
		if ((comm_sock = accept(conn_sock, (struct sockaddr *) &client_addr,
				(socklen_t *) &client_addr)) == -1) {
			perror("couldn't accept\n");
			continue;
		}
		cout << "accepted" << endl;
		//flush(cout);
		bzero(packet, 10);
		//m = recv(conn_sock, packet, 10, 0);
		m = recv(comm_sock, packet, 10, 0);
		if (m < 0) {
			perror("recv failed");
			exit(1);
		}
		cout<<"received"<<endl;
//flush(cout);
		/* Write a response to the client */
		n = send(comm_sock, buffer, sizeof(buffer), 0);

		if (n < 0) {
			perror("ERROR send to client");
			exit(1);
		}
		cout<<"sending....."<<endl;



	}
	close(comm_sock);
	close(conn_sock);
	return 0;
}
I thought you are having the problems in your client.
client::conn has an infinite loop in the original post.

In the code above, close(comm_sock); should be in the loop.
Nasi, where is your new client.cpp?
sorry for Laten ,client.cpp:

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
#define MYPORT 53515
namespace personalization {
bool client::conn() {
	//create socket if it is not already created
	if (sock == -1) {
		//Create socket
		sock = socket(AF_INET, SOCK_STREAM, 0);
		if (sock == -1) {
			perror("Could not create socket");
		}

		cout << "Socket created" << endl;
	}


	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(MYPORT);
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	if (connect(sock , (struct sockaddr *)&server_addr , sizeof(server_addr)) < 0)
		{
			perror("connect failed. Error");
			return false;
		}

		cout<<"Connected\n";
		return true;

 	close(sock);

}
bool client::send_data() {
	//Send some data
	 if( send(sock , packet , sizeof( packet ) , 0) < 0)
	    {
	        perror("Send failed");
	        return false;
	    }
	    cout<<"Data send\n";

	    return true;
	    close(sock);
	}

bool client::rec_data() {
	char buffer[20];
	string reply;

	//Receive a echo from the server
	if (recv(sock, buffer, sizeof(buffer), 0) < 0) {
		perror("receive failed");
		return false;
	}
cout<<"receive :"<<buffer;
//flush(cout);
	reply = buffer;
	close(sock);
	return true;

}

client::client() {
	// TODO Auto-generated constructor stub
sock=-1;

}


Last edited on
Pages: 12