LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-03-2005, 04:32 AM   #1
hk_michael
LQ Newbie
 
Registered: Oct 2004
Posts: 22

Rep: Reputation: 15
pthread socket programming problem


Hi all!
My project is now coming into a situation that the server program can detect the disconnection from phone to PC, but then after reconnection, the program has some errors. Actually, I'm not quite familiar with thread programming, especially multithread. So, I wish to know that which book or website can I refer for the pthread programming? By the way, I put the codes here to all of you to see what's wrong with my program. Thank you very much.
Michael

/*start of code*/

int main (int argc, char **argv)
{
int ctl, dev, fd;
struct sockaddr_rc laddr, raddr;
struct rfcomm_dev_req req;
struct termios ti;
char dst[18], devname[MAXPATHLEN];
struct sigaction sa;
struct pollfd p;
int sk, nsk, alen, try = 3;
ctl=5;
dev=0;
rfcomm_raw_tty = 1;
profile T610profile;
svc_info_t T610svc;
bdaddr_t bdaddr;
sdp_session_t *sess;
target=user=password=NULL;
printf("\E[H\E[J");
printf ("Bluetooth infrared converter\n\n");
printf ("Doing initializations !\n");
printf("T610 profile is added\n");
T610svc.class=0x1108;
printf("Service name is added\n");
T610svc.profile=0x1108;
T610svc.channel=3;
T610svc.name="HS";
add_service(T610profile.bdaddr, &T610svc);
sess = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, 0);
mysql_init(&mysql);
if (!mysql_connect(&mysql, target, user, password))
{
printf("Database connection failed\n");
return 0;
}
else
printf("Database connection ok\n");
mysql_select_db(&mysql,"AVDevice");
system("hciconfig hci0 class 0x200404");
while (1)
{
laddr.rc_family = AF_BLUETOOTH;
bacpy(&laddr.rc_bdaddr, &bdaddr);
laddr.rc_channel = 3;
if ((sk = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
perror("Can't create RFCOMM socket");
return;
}
if (bind(sk, (struct sockaddr *)&laddr, sizeof(laddr)) < 0) {
perror("Can't bind RFCOMM socket");
close(sk);
return;
}
printf("Waiting for connection on channel %d\n", laddr.rc_channel);
listen(sk, 10);
alen = sizeof(raddr);
nsk = accept(sk, (struct sockaddr *) &raddr, &alen);
alen = sizeof(laddr);
if (getsockname(nsk, (struct sockaddr *)&laddr, &alen) < 0) {
perror("Can't get RFCOMM socket name");
close(nsk);
return;
}
memset(&req, 0, sizeof(req));
req.dev_id = dev;
req.flags = (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP);
bacpy(&req.src, &laddr.rc_bdaddr);
bacpy(&req.dst, &raddr.rc_bdaddr);
req.channel = raddr.rc_channel;
if ((dev = ioctl(nsk, RFCOMMCREATEDEV, &req)) < 0) {
perror("Can't create RFCOMM TTY");
close(sk);
return;
}
snprintf(devname, MAXPATHLEN - 1, "/dev/rfcomm%d", dev);
while ((fd = open(devname, O_RDONLY | O_NOCTTY)) < 0)
{
snprintf(devname, MAXPATHLEN - 1, "/dev/bluetooth/rfcomm/%d", dev);
if ((fd = open(devname, O_RDONLY | O_NOCTTY)) < 0)
{
if (try--)
{
snprintf(devname, MAXPATHLEN - 1, "/dev/rfcomm%d", dev);
sleep(1);
continue;
}
perror("Can't open RFCOMM device");
memset(&req, 0, sizeof(req));
req.dev_id = dev;
req.flags = (1 << RFCOMM_HANGUP_NOW);
ioctl(ctl, RFCOMMRELEASEDEV, &req);
close(sk);
return;
}
}
if (rfcomm_raw_tty) {
tcflush(fd, TCIOFLUSH);
cfmakeraw(&ti);
tcsetattr(fd, TCSANOW, &ti);
}
close(sk);
close(nsk);
ba2str(&req.dst, dst);
printf("Connection from %s to %s\n", dst, devname);
pthread_create(&tid, NULL, mainprog, nsk);
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_NOCLDSTOP;
sa.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &sa, NULL);
sigaction(SIGPIPE, &sa, NULL);
sa.sa_handler = sig_term;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sa.sa_handler = sig_hup;
sigaction(SIGHUP, &sa, NULL);
p.fd = fd;
p.events = POLLERR | POLLHUP;
while (!__io_canceled)
{
p.revents = 0;
if (poll(&p, 1, 100))
break;
}
printf ("Mobile user has quitted!\n");
close(fd);
}
mysql_close(&mysql);
}

void *mainprog(void *arg)
{
int submenu_selected;
int ans_selected[7];
int a,b;
int choice;
int count;
int which_submenu;
int type_selected;
printf("Now T610 is connected to the PC and thread is created\n");
parsearguments ();
snprintf (tmp, sizeof (tmp), "t68_remote_v2 (%s)", modemname);
tmp[sizeof (tmp) - 1] = 0;
openlogfile (tmp, logfile, LOG_DAEMON, loglevel);
set_alarmhandler (alarmhandler, alarmlevel, modemname);
openmodem ();
setmodemparams ();
sprintf (command, "at+cscs=i\"8859-1\"\r");
ret_code = put_command (command, answer, sizeof (answer), 5, 0);
sprintf (command, "AT*EAM=\"Bt-IrConverter\"\r");
ret_code = put_command (command, answer, sizeof (answer), 5, 0);
submenu_selected = 0;
while (!submenu_selected)
{
ret_code = put_command ("", answer, sizeof (answer), 5, 0);
submenu_selected = (NULL != strstr (answer, "*EAAI"));
}
while (1)
{
sprintf (command, "AT*EASM=\"Bt-IrConverter\",1,1,7,\"TV\",\"CD\",\"VCD\",\"DVD\",\"VCR\",\"Projector\",\"quit\",\r");
ret_code = put_command (command, answer, sizeof (answer), 7, 0);
for (a=0; a<7; a++)
ans_selected[a]=0;
count=1;
while (count)
{
ret_code = put_command ("", answer, sizeof (answer), 5, 0);
for (b=0; b<7; b++)
{
sprintf (buffer, "*EAMI: %d", b+1);
ans_selected[b] = (NULL != strstr (answer, buffer));
if (ans_selected[b]==1)
{
count=0;
type_selected=b+1;
break;
}
}
}
if (ans_selected[6])
return;
else if (ans_selected[0])
printf("Mobile user selected TV\n");
else if (ans_selected[1])
printf("Mobile user selected CD\n");
else if (ans_selected[2])
printf("Mobile user selected VCD\n");
else if (ans_selected[3])
printf("Mobile user selected DVD\n");
else if (ans_selected[4])
printf("Mobile user selected VCR\n");
else if (ans_selected[5])
printf("Mobile user selected Projector\n");
switch (type_selected)
{
case 1:
devicecontrol(&mysql, "TV", remote);
break;
case 2:
devicecontrol(&mysql, "CD", remote);
break;
case 3:
devicecontrol(&mysql, "VCD", remote);
break;
case 4:
devicecontrol(&mysql, "DVD", remote);
break;
case 5:
devicecontrol(&mysql, "VCR", remote);
break;
case 6:
devicecontrol(&mysql, "Projector", remote);
break;
default:
break;
}
}
pthread_exit(NULL);
}
 
Old 04-03-2005, 08:54 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
but then after reconnection, the program has some errors.
Can you share what these errors are with us ?
Quote:
By the way, I put the codes here to all of you to see what's wrong with my program.
Please edit your first post and put your code between the code tags, like this
Code:
 Your code here
And indent it, if it is not yet, it's unreadable as now.
Add comments too, and show where the errors happen.

Last edited by jlliagre; 04-03-2005 at 08:55 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
socket programming problem...??? arunka Programming 4 10-11-2005 08:08 AM
Pthread Programming. bruse Mandriva 2 08-09-2005 08:35 PM
Socket Programming jawadhashmi Programming 4 05-24-2005 03:04 AM
socket programming problem bgraur Programming 2 03-09-2003 09:05 AM
Socket programming problem krivi Programming 3 01-14-2002 02:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration