C Socket Programming: reopen and reconnect socket
<p>I am testing out my code for reconnection in case the connection drop. However I'm facing some Segmentation Error after the socket is re-opened and re-connected.</p>

<pre><code>#include &lt;pthread.h&gt;
#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/socket.h&gt;             //socket
#include &lt;bluetooth/bluetooth.h&gt;    //bluetooth
#include &lt;bluetooth/rfcomm.h&gt;       //bluetooth
#include &lt;string.h&gt;                 //strlen
#include &lt;arpa/inet.h&gt;              //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-&gt;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-&gt;s, (struct sockaddr *)&amp;loc_addr, sizeof(loc_addr));

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

    // accept one connection
    connect-&gt;client = accept(connect-&gt;s, (struct sockaddr *)&amp;rem_addr, &amp;opt);
    ba2str( &amp;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(&amp;mDataMutex);
        puts("send mutex in");
        while(connect-&gt;retry != 0);
        puts("send mutex out");
        pthread_mutex_unlock(&amp;mDataMutex);

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

            pthread_mutex_lock(&amp;mDataMutex);
            if(connect-&gt;retry == 2) {
                close(connect-&gt;client);
                close(connect-&gt;s);
                bt_connect(&amp;connect);
                connect-&gt;retry = 0;
                puts("send retry success");
            } else
                retry = 1;
            pthread_mutex_unlock(&amp;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(&amp;attr);
    pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE);

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

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

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

    return 0;
}
</code></pre>

<p>The code may look a little messy in sending and receiving part as I was using mutex to check and debug the output.</p>

<p>This is the output I am getting:</p>

<pre><code>[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
</code></pre>

<p>I am not sure what is wrong since I did ensure that i close the socket before re-opening it. Any help/guidance? Thanks!</p>

<p>Edit: Not sure if bt_send is jamming it since it didn't print out <code>[Bluetooth-Send] Error reading from socket</code> when I'm sending test data automatically every second... (and both send and receive are running simultaneously)</p>

<p>PS. New to C and Socket Programming</p>