2016-02-11 167 views
-1

我正在測試我的代碼以便在連接斷開的情況下重新連接。但是,在套接字重新打開並重新連接後,我正面臨一些分段錯誤。C套接字編程:重新打開並重新連接套接字

#include <pthread.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <sys/socket.h>    //socket 
#include <bluetooth/bluetooth.h> //bluetooth 
#include <bluetooth/rfcomm.h>  //bluetooth 
#include <string.h>     //strlen 
#include <arpa/inet.h>    //inet_addr 

struct BTConnection { 
    int client; 
    int s; 
    int retry; 
}; 

struct BTConnection bt_1; 

void *bt_connect(void *arg); 
void *bt_send(void *arg); 
void *bt_receive(void *arg); 

void *bt_connect(void *arg) 
{ 
    struct BTConnection *connect = (struct BTConnection*)arg; 

    struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 }; 
    char buf[1024] = { 0 }; 
    socklen_t opt = sizeof(rem_addr); 

    // allocate socket 
    connect->s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); 

    // bind socket to port 1 of the first available 
    // local bluetooth adapter 
    loc_addr.rc_family = AF_BLUETOOTH; 
    loc_addr.rc_bdaddr = *BDADDR_ANY; 
    loc_addr.rc_channel = (uint8_t) 1; 
    bind(connect->s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); 

    // put socket into listening mode 
    listen(connect->s, 1); 

    // accept one connection 
    connect->client = accept(connect->s, (struct sockaddr *)&rem_addr, &opt); 
    ba2str(&rem_addr.rc_bdaddr, buf); 

    return 0; 
} 

void *bt_send(void *arg) 
{ 
    struct BTConnection *connect = (struct BTConnection*)arg; 
    char buf[1024] = { 0 }; 
    int bytes_read, retry; 

    puts("[Bluetooth] Sending data to device..."); 
    while(1) { 
     ..... 

     pthread_mutex_lock(&mDataMutex); 
     puts("send mutex in"); 
     while(connect->retry != 0); 
     puts("send mutex out"); 
     pthread_mutex_unlock(&mDataMutex); 

     if(write(connect->client, return_msg, strlen(return_msg)) < 0) { 
      .... 
      puts("Error reading from socket"); 
      pthread_mutex_lock(&mDataMutex); 
      puts("send mutex"); 
      if(connect->retry == 0) 
       connect->retry = 2; 
      pthread_mutex_unlock(&mDataMutex); 
      puts("send mutex end"); 

      pthread_mutex_lock(&mDataMutex); 
      if(connect->retry == 2) { 
       close(connect->client); 
       close(connect->s); 
       bt_connect(&connect); 
       connect->retry = 0; 
       puts("send retry success"); 
      } else 
       retry = 1; 
      pthread_mutex_unlock(&mDataMutex); 

      if(retry == 1) 
      { 
       puts("bluetooth send retrying in 8 secs"); 
       sleep(8); 
      } 
     } else 
      retry = 0; 

     // clear buffer 
     memset(return_msg,0,sizeof return_msg); 
     memset(temp,0,sizeof temp); 
     // set to send data every second 
     sleep(1); 
    } 

    return 0; 
} 

void *bt_receive(void *arg) 
{ 
    // similar as above 
} 

int main(void) 
{ 
    bt_1.retry = 0; 
    int rc; 
    void *status; 
    pthread_t bt_connect_thread, bt_send_thread, bt_receive_thread; 
    pthread_attr_t attr; 

    /* Initialize and set thread detached attribute */ 
    pthread_attr_init(&attr); 
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 

    // Create BT connect thread and start it 
    rc = pthread_create(&bt_connect_thread, &attr, bt_connect, (void *)&bt_1); 
    if (rc) 
    { 
     printf("ERROR; return code from pthread_create() is %d\n", rc); 
     return (-1); 
    } 

    rc = pthread_join(bt_connect_thread, &status); 
    rc = pthread_create(&bt_send_thread, &attr, bt_send, (void *)&bt_1); 
    rc = pthread_create(&bt_receive_thread, &attr, bt_receive, (void *)&bt_1); 

    rc = pthread_join(bt_send_thread, &status); 
    rc = pthread_join(bt_receive_thread, &status); 
    close(bt_1.client); 
    close(bt_1.s); 
    pthread_attr_destroy(&attr); 

    return 0; 
} 

代碼可能看起來有點混亂在發送和作爲我用互斥來檢查和調試輸出接收部。

這是我得到的輸出:

[Bluetooth] Allocating socket... Done! 
[Bluetooth] Bind socket... Done! 
[Bluetooth] Waiting for incoming connections... 
[Bluetooth] Accepted connection from 44:6D:6C:6D:1B:BC 
[Bluetooth] Bluetooth connection thread completed 
[Bluetooth] Receiving data from device... 
rcv mutex in 
rcv mutex out 
[Bluetooth] Sending data to device... 
send mutex in 
send mutex out 
send mutex in 
send mutex out 
[Bluetooth-Receive] Error reading from socket 
receive mutex 
receive mutex end 
[Bluetooth-Receive] Connections closed. Reconnecting... 
[Bluetooth] Allocating socket... Done! 
[Bluetooth] Bind socket... Done! 
[Bluetooth] Waiting for incoming connections... 
[Bluetooth] Accepted connection from 44:6D:6C:6D:1B:BC 
[Bluetooth] Bluetooth connection thread completed 
Segmentation fault 

我不知道什麼是錯的,因爲我沒有保證,我重新打開它之前關閉套接字。任何幫助/指導?謝謝!

編輯:如果bt_send被幹擾它,因爲它時,我自動發送測試數據每一秒......(並同時發送和接收同時運行)

PS沒有打印出[Bluetooth-Send] Error reading from socket不知道。新C和socket編程

+3

如果你運行在這個'gdb'(或其他調試器)中,它會告訴你到底是哪一行代碼導致了段錯誤。 – e0k

+0

我會懷疑'關閉'部分...... –

+0

您應該檢查bind()的返回值是否失敗。 – EOF

回答

1
void *bt_connect(void *arg) 
{ 
    struct BTConnection *connect = (struct BTConnection*)arg; 

好了,所以我們需要調用bt_connect指針傳遞到BTConnection

struct BTConnection *connect = (struct BTConnection*)arg; 
... 
       bt_connect(&connect); 

由於connect是一個指向BTConnection,​​是一個指針的指針。那爲什麼我們把它傳遞給bt_connect

+0

Hi @ david-schwartz感謝您指出了這一點。在這種情況下,我應該傳遞全局變量bt_1嗎?對不起,如果這聽起來像是要求你寫出代碼,但如果你可以添加更正後的代碼,它肯定會幫助我更好,所以我可以從那裏學習並測試它。 – python1010101

+0

只需刪除'&'。 –

+0

非常感謝,作品像魅力! – python1010101

相關問題