All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mateus Interciso <mateus@ouvi.com.br>
To: linux-bluetooth@vger.kernel.org
Subject: Question on simple RFCOMM server
Date: Tue, 25 Aug 2009 09:26:47 -0300	[thread overview]
Message-ID: <4A93D887.8060007@ouvi.com.br> (raw)

Hello, I have a very simple RFCOMM server that I adapted from an 
extremely simple TCP echo server I've made for learning TCP, it's a 
simple fork() server, and while the TCP server works perfeclty, the 
RFCOMM works only with 2 connected clients at the same time, and I'm 
failling to see the problem, can someone help me?

Here's the code:

(The commented part if the TCP part)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

#define MYPORT 10
#define BACKLOG 31
#define MAX_SIZE 80

int main(int argc, char *argv[])
{
   int listenfd, connfd;
   pid_t childpid;
   socklen_t clilen;
   struct sockaddr_rc cliaddr, servaddr;
   char buf[MAX_SIZE];
   int i =0;

   //listenfd = socket(AF_INET, SOCK_STREAM, 0);
   listenfd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
   if(listenfd < 0)
   {
     perror("socket");
     exit(errno);
   }

   bzero(&servaddr, sizeof(servaddr));
   servaddr.rc_family = AF_BLUETOOTH;
   servaddr.rc_bdaddr = *BDADDR_ANY;
   servaddr.rc_channel = (uint8_t)MYPORT;
   /*
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
   servaddr.sin_port = htons(MYPORT);
   */
   if(bind(listenfd,(struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
   {
     perror("bind");
     exit(errno);
   }

   if(listen(listenfd,BACKLOG) < 0)
   {
     perror("listen");
     exit(errno);
   }

   while(1)
   {
     clilen = sizeof(cliaddr);
     if((connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen)) 
< 0)
     {
       perror("accept");
       exit(errno);
     }
     if((childpid = fork()) == 0)
     {
       i = 0;
       close(listenfd);
       fprintf(stderr,"[DEBUG] Got connection\n");
       memset(buf,'\0',sizeof(buf));
       //echo server
       if(read(connfd, buf, MAX_SIZE) < 0) perror("read");
       else
         if(send(connfd, buf, strlen(buf), 0) < 0) perror("send");
       exit(0);
     }
     close(connfd);
   }
   return 0;
}


Thanks.

             reply	other threads:[~2009-08-25 12:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-25 12:26 Mateus Interciso [this message]
2009-08-25 12:31 ` Question on simple RFCOMM server Peter Wippich
2009-08-25 13:41 ` Iain Hibbert
2009-08-25 14:18   ` Mateus Interciso
2009-08-25 17:15     ` Iain Hibbert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4A93D887.8060007@ouvi.com.br \
    --to=mateus@ouvi.com.br \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.