public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [Bluez-users] [ECONNRESET]Problem with last buffer readed with a read or recv
@ 2004-10-20 14:01 Paolino paperino
  2004-10-20 16:13 ` Marcel Holtmann
  0 siblings, 1 reply; 6+ messages in thread
From: Paolino paperino @ 2004-10-20 14:01 UTC (permalink / raw)
  To: bluez users

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
Bluez-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-users

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Bluez-users] [ECONNRESET]Problem with last buffer readed with a read or recv
  2004-10-20 14:01 [Bluez-users] [ECONNRESET]Problem with last buffer readed with a read or recv Paolino paperino
@ 2004-10-20 16:13 ` Marcel Holtmann
  2004-10-20 19:15   ` Collin R. Mulliner
  2004-10-22  8:03   ` Paolino paperino
  0 siblings, 2 replies; 6+ messages in thread
From: Marcel Holtmann @ 2004-10-20 16:13 UTC (permalink / raw)
  To: Paolino paperino; +Cc: BlueZ Mailing List

Hi Andrea,

> 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?

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
Bluez-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-users

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Bluez-users] [ECONNRESET]Problem with last buffer readed with a read or recv
  2004-10-20 16:13 ` Marcel Holtmann
@ 2004-10-20 19:15   ` Collin R. Mulliner
  2004-10-22  8:03   ` Paolino paperino
  1 sibling, 0 replies; 6+ messages in thread
From: Collin R. Mulliner @ 2004-10-20 19:15 UTC (permalink / raw)
  To: Paolino paperino; +Cc: BlueZ Mailing List

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 <marcel@holtmann.org>:

> Hi Andrea,
>
> > 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.
> >
> > 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
> sides.
>
> Regards
>
> Marcel
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: IT Product Guide on ITManagersJourna=
l
> 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
> Bluez-users@lists.sourceforge.net
> 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
Bluez-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-users

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Bluez-users] [ECONNRESET]Problem with last buffer readed with a read or recv
  2004-10-20 16:13 ` Marcel Holtmann
  2004-10-20 19:15   ` Collin R. Mulliner
@ 2004-10-22  8:03   ` Paolino paperino
  2004-10-22 11:07     ` Marcel Holtmann
  1 sibling, 1 reply; 6+ messages in thread
From: Paolino paperino @ 2004-10-22  8:03 UTC (permalink / raw)
  To: Marcel Holtmann; +Cc: BlueZ Mailing List

[-- Attachment #1: Type: text/plain, Size: 1259 bytes --]

> 
> 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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: clientTestRC.c --]
[-- Type: text/x-csrc; name="clientTestRC.c", Size: 2529 bytes --]

#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);	
}

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: serverTestRC.c --]
[-- Type: text/x-csrc; name="serverTestRC.c", Size: 2843 bytes --]

#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);
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Bluez-users] [ECONNRESET]Problem with last buffer readed with a read or recv
  2004-10-22  8:03   ` Paolino paperino
@ 2004-10-22 11:07     ` Marcel Holtmann
  2004-10-22 15:13       ` Paolino paperino
  0 siblings, 1 reply; 6+ messages in thread
From: Marcel Holtmann @ 2004-10-22 11:07 UTC (permalink / raw)
  To: Paolino paperino; +Cc: BlueZ Mailing List

Hi Andrea,

> 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.

> 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]$

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
Bluez-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-users

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Bluez-users] [ECONNRESET]Problem with last buffer readed with a read or recv
  2004-10-22 11:07     ` Marcel Holtmann
@ 2004-10-22 15:13       ` Paolino paperino
  0 siblings, 0 replies; 6+ messages in thread
From: Paolino paperino @ 2004-10-22 15:13 UTC (permalink / raw)
  To: Marcel Holtmann; +Cc: BlueZ Mailing List

 --- Marcel Holtmann <marcel@holtmann.org> ha scritto:

> Hi Andrea,
...
> 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. 

> 
> 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

> 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 :-( 

> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-10-22 15:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-10-20 14:01 [Bluez-users] [ECONNRESET]Problem with last buffer readed with a read or recv Paolino paperino
2004-10-20 16:13 ` Marcel Holtmann
2004-10-20 19:15   ` Collin R. Mulliner
2004-10-22  8:03   ` Paolino paperino
2004-10-22 11:07     ` Marcel Holtmann
2004-10-22 15:13       ` Paolino paperino

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox