Skip to contentSkip to conversation

Problem with last buffer readed with a read or recv

Bluez-users Paolino paperino 3 participants 6 messages Oct 20, 2004 – Oct 22, 2004 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

Hi, i need some hints.
I writed a RFCOMM cleint side socket and its servere side. The server sends an arbitrary sequence of 2048 bytes throught the write function. The client side read as far as it receive a 0 or -1 frem the read. The code is like this:
char *buffer[2048];
do {
tmpPtr += nread;
nread = recv(fd, buffer, length, 0);
} while (nread > 0);

The problem arise when the server correctly send its last bucket, the client must read it (read 2048 bytes), at the next read the client must get an EOF
(0) value from the recv...but this not happen it receive -1 and errno=ECONNRESET. The same code works as expected using TCP streams, but when it is used over RFCOMM SOCK_STREAM it causes this anomaly.

Somebody could explain what happens, why the recv call doesn't return a 0 when last data are reached on the stream, but give an error message?

Thank's a lot
Best regards
Andrea S.

___________________________________
Nuovo Yahoo! Messenger: E' molto più divertente: Audibles, Avatar, Webcam, Giochi, Rubrica… Scaricalo ora!
http://it.messenger.yahoo.it

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

maintainer
#1 lore raw

Hi Andrea,

Quoted 19 lines

I writed a RFCOMM cleint side socket and its servere side. The server sends an arbitrary sequence of 2048 bytes throught the write function. The client side read as far as it receive a 0 or -1 frem the read. The code is like this:
char *buffer[2048];

do {
   tmpPtr += nread;		
   nread = recv(fd, buffer, length, 0);
} while (nread > 0);

The problem arise when the server correctly send its last bucket, the client must read it (read 2048 bytes), at the next read the client must get an EOF
(0) value from the recv...but this not happen it receive -1 and errno=ECONNRESET. The same code works as expected using TCP streams, but when it is used over RFCOMM SOCK_STREAM it causes this anomaly.

Quoted 3 lines

Somebody could explain what happens, why the recv call doesn't return a 0 when last data are reached on the stream, but give an error message?

are you sure that your client/server code is correct, because until you don't close the socket on the server side, the client can't receive any additional information. It simply blocks. Show us the full code for both sides.

Regards

Marcel

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#2 lore raw

Hi,

I have a pretty good documented/commented application that does RFCOMM client-server communication (client and server implemented in the applica= tion).
Maybe you should just take a look at my code (it is really not just hacke= d
stuff!). There is one file with all the RFCOMM stuff in.
http://mulliner.org/bluetooth/btchat/ (you want: btchatd)

... Collin

Quoting Marcel Holtmann <[email protected]>:

Quoted 21 lines

Hi Andrea,

Quoted 19 lines

I writed a RFCOMM cleint side socket and its servere side. The server sends an arbitrary sequence of 2048 bytes throught the write function. The client side read as far as it receive a 0 or -1 frem the read. The code is like this:
char *buffer[2048];

do {
   tmpPtr +=3D nread;
   nread =3D recv(fd, buffer, length, 0);
} while (nread > 0);

The problem arise when the server correctly send its last bucket, the client must read it (read 2048 bytes), at the next read the client must get an EOF
(0) value from the recv...but this not happen it receive -1 and errno=3DECONNRESET. The same code works as expected using TCP streams, but when it is used over RFCOMM SOCK_STREAM it causes this anomaly.

Quoted 7 lines
Quoted 3 lines

Somebody could explain what happens, why the recv call doesn't return a 0 when last data are reached on the stream, but give an error message?

are you sure that your client/server code is correct, because until you don't close the socket on the server side, the client can't receive any additional information. It simply blocks. Show us the full code for bot=

h

Quoted 11 lines

sides.

Regards

Marcel

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJourna=

l

Quoted 1 line

Use IT products in your business? Tell us what you think of them. Give =

us

Quoted 1 line

Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out =

more

Quoted 6 lines

http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#3 lore raw
In reply to Marcel Holtmann
Quoted 10 lines

are you sure that your client/server code is correct, because until you
don't close the socket on the server side, the client can't receive any
additional information. It simply blocks. Show us the full code for both
sides.

Regards

Thanks Marcel i attach the server and client source code.
brief man:
clientTestRC <btadddr>
serverTestRC

The code is structured to use also TCP socket, it is only necessary to decomment an undef and recompile. In tcp all work correctly...last read retrn a 0.

Out snapshot server:
[andrea@ZANDEGIA andrea]$ ./serverTestRC
Binding...
Bounded on channel 10
Listenning...
accept ok
loop 0
loop 1
loop 2
loop 3
loop 4
[andrea@ZANDEGIA andrea]$

Out snapshot client:
[andrea@tsunami Bluetoooth]$ ./clientTestRC
00:10:C6:4D:38:AC
bluetooth_initclient_socket called
connecting...
Connection opened
Receiving ...
nread value 1019
nread value 1029
nread value 1019
nread value 1029
nread value 1019
nread value 1029
nread value 2038
nread value 10
nread value 2048
nread value -1
[andrea@tsunami Bluetoooth]$

Best regards
Andrea S.

___________________________________
Nuovo Yahoo! Messenger: E' molto più divertente: Audibles, Avatar, Webcam, Giochi, Rubrica… Scaricalo ora!
http://it.messenger.yahoo.it

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/select.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <netdb.h>
#include <sys/socket.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
//#include <bluetooth/l2cap.h>

#define MAXDIM 2048
#define BLUE
//#undef BLUE

int bluetooth_initclient_socket(char *str_addr)

{
	struct sockaddr_rc addr;
	//bdaddr_t bdaddr;
	int s;
	
	printf("bluetooth_initclient_socket called\n");
	/* Default options */
	if ((s = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
		perror("Can't create socket.");
		exit(1);
	}

	memset(&addr, 0, sizeof(addr));
	addr.rc_family = AF_BLUETOOTH;
	//addr.rc_bdaddr = bdaddr;
	str2ba(str_addr, &addr.rc_bdaddr);
	//str2ba("00:10:C6:4D:38:AC", &addr.rc_bdaddr);
 
	addr.rc_channel = htobs(10); //channel
	printf("connecting...\n");
	if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		perror("Can't connect.");
		exit(1);
	}
	printf("Connection opened\n");
	return s;	
}

int tcp_initclient_socket(char *str_addr, int port)

{
	struct sockaddr_in 	addr;
	struct in_addr      inet_address;
	struct hostent      *host_info;
	int s;

	printf("tcp_initclient_socket called\n");
	if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("Can't create socket.");
		exit(1);
	}
	printf("Socket created, resolving address...\n");	
	if ((host_info = gethostbyname(str_addr)) == NULL )
		printf ("Error in resolving");

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = host_info->h_addrtype;
	addr.sin_port = htons(port);
	memcpy(&addr.sin_addr, host_info->h_addr, host_info->h_length);	

	printf("Connecting...\n");
	if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		perror("Can't connect.");
		exit(1);
	}
	printf("Connection opened\n");
	return s;		
}

int main(int argc, char *argv[])

{
	
	int s;
	char *buffer, *tmpPtr;
	int nread;
#ifdef BLUE
	s = bluetooth_initclient_socket(argv[1]);
#else
	s = tcp_initclient_socket(argv[1], 2004);

#endif

	if (!(buffer = malloc(MAXDIM))) {
		printf("Can't allocate data buffer\n");
		exit(1);
	}

	printf("Receiving ...\n");
	//Read buffers until EOF is reached
	int length = MAXDIM;
	tmpPtr = buffer;
    do {
    	//tmpPtr += nread;		
		nread = recv(s, tmpPtr, length, 0);
		//length -= nread;
		printf("nread value %d\n", nread);
    } while (nread > 0);
	close(s);	
}

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/select.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <netdb.h>
#include <sys/socket.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
//#include <bluetooth/l2cap.h>

#include <sys/types.h>
#include <sys/ioctl.h>

#define MAXDIM 2048
#define BLUE
//#undef BLUE

int bluetooth_initserver_socket(int channel)

{
	struct sockaddr_rc server;
	bdaddr_t bdaddr;
	int s;

	/* Default options */	
	if ((s = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
		printf("Can't create socket. %s(%d)\n", strerror(errno), errno);
		exit(1);
	}

	memset(&server, 0, sizeof(server));
	server.rc_family = AF_BLUETOOTH;
	bacpy(&bdaddr, BDADDR_ANY);
	server.rc_bdaddr = bdaddr;
	server.rc_channel = htobs(channel);//channel;
	printf("Binding...\n");
	if (bind(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
		printf("Can't bind socket. %s(%d)\n", strerror(errno), errno);
		exit(1);
	}
	printf("Bounded on channel %d\n", server.rc_channel);
	return s;
}

int tcp_initserver_socket(int port)

{
	struct sockaddr_in server;
	int s;

	if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		printf("Can't create socket. %s(%d)\n", strerror (errno), errno);
		exit(1);
	}
	
	memset(&server, 0, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = INADDR_ANY;
	server.sin_port = htons(port);
	
	printf("Binding...\n");
	if (bind(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
		printf("Can't bind socket.%s(%d)\n", strerror (errno), errno);
		exit(1);
	}
	printf("Bounded  on port %d\n", port);
	return s;
}

int main(int argc, char *argv[])

{	
	//struct sockaddr_in clientAddr;
    int clientDescr, s, i;
	socklen_t clientAddrLen;
#ifdef BLUE
	struct sockaddr_rc clientAddr;
#else
    struct sockaddr_in clientAddr;

#endif

#ifdef BLUE	
	s = bluetooth_initserver_socket(10);
#else		
	s = tcp_initserver_socket(2004);
#endif	
	if (listen(s, 5) < 0)
  	{
    	//Error
		printf("Error in listen (%d)\n", strerror(errno), errno);
    	exit(1);
  	}
	printf("Listenning...\n");

//Accept a connection memset(&clientAddr, 0, sizeof(clientAddr));

  	if ((clientDescr = accept(s, (struct sockaddr *) &clientAddr, &clientAddrLen)) < 0){
	  	printf("accept failed %s (%d)\n", strerror(errno), errno);
	  	exit(1);
	}

printf("accept ok\n");

  	char *buffer;
  	buffer = malloc(MAXDIM);
	for (i = 0; i < MAXDIM; i++)
		buffer[i] = MAXDIM - i;
	//Sends 5 packets of MAXDIM bytes
	for (i = 0; i < 5; i++){
		printf("loop %d\n", i);
		if (write(clientDescr, buffer, MAXDIM) < 0){
	 		printf("Error during write\n");
	 		return;
	 	}	
	}

close (clientDescr);
}

maintainer
#4 lore raw

Hi Andrea,

Quoted 9 lines

Thanks Marcel i attach the server and client source code.
brief man:
clientTestRC <btadddr>
serverTestRC

The code is structured to use also TCP socket, it is only necessary to decomment an undef and recompile. In tcp all work correctly...last read retrn a 0.

the line "addr.rc_channel = htobs(10);" is wrong, because rc_channel is only a uint8_t and so you don't need any byte swapping magic. Remove the call to htobs() and simply set the channel number.

On the server side you forgot to init clientAddrLen before you called accept(). Do a "clientAddrLen = sizeof(clientAddr);" first.

Quoted 23 lines

Out snapshot server:
[andrea@ZANDEGIA andrea]$ ./serverTestRC
Binding...
Bounded on channel 10
Listenning...
accept ok
loop 0
loop 1
loop 2
loop 3
loop 4
[andrea@ZANDEGIA andrea]$

Out snapshot client:
[andrea@tsunami Bluetoooth]$ ./clientTestRC
00:10:C6:4D:38:AC
bluetooth_initclient_socket called
connecting...
Connection opened
Receiving ...
nread value 1019
nread value 1029
nread value 1019

Quoted 8 lines

nread value 1029
nread value 1019
nread value 1029
nread value 2038
nread value 10
nread value 2048
nread value -1
[andrea@tsunami Bluetoooth]$

for me this looks ok, because you close the connection from the server side and recv returns with an error. Does it makes any difference when you use read/write instead of recv/send?

I seems that you really wanna the behaviour described in the recv manual page:

RETURN VALUE

These calls return the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.

For this behaviour you must send me a kernel patch that implements this, but I will only accept it if you change the L2CAP in the same way.

Regards

Marcel

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#5 lore raw

--- Marcel Holtmann <[email protected]> ha scritto:

Quoted 1 line

Hi Andrea,

...

Quoted 10 lines

the line "addr.rc_channel = htobs(10);" is wrong, because rc_channel is
only a uint8_t and so you don't need any byte swapping magic. Remove the
call to htobs() and simply set the channel number.

On the server side you forgot to init clientAddrLen before you called
accept(). Do a "clientAddrLen = sizeof(clientAddr);" first.

Thanks for the hint i haven't looked at this
particular..i didn't know.

Quoted 6 lines

for me this looks ok, because you close the connection from the server
side and recv returns with an error. Does it makes any difference when
you use read/write instead of recv/send?

Yes also using read/write i get the same behaviour

Quoted 4 lines

For this behaviour you must send me a kernel patch that implements this,
but I will only accept it if you change the L2CAP in the same way.

For the moment i think i'm not a kernel hacker :-(

Quoted 3 lines

Regards

Marcel

Thanks Marcel for the time spent
Best regards
Andrea S.

___________________________________
Nuovo Yahoo! Messenger: E' molto più divertente: Audibles, Avatar, Webcam, Giochi, Rubrica… Scaricalo ora!
http://it.messenger.yahoo.it