LinuxQuestions.org
Review your favorite Linux distribution.
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 02-12-2016, 01:21 PM   #1
bomby
LQ Newbie
 
Registered: Feb 2016
Posts: 2

Rep: Reputation: Disabled
C Wifi Socket Re-connection unsuccessful


Language: C
Topic: TCP/IP Socket Programming (Server/Client Architecture)
Source: http://www.binarytides.com/server-cl...sockets-linux/

This program is using a server/client architecture (over TCP/IP).

I am now experiencing problems with the server coding.


Main Methods of the Code:

void *wifi_connect(void *arg); Opens socket and listen, accept connections

void *wifi_send(void *arg); Sends message to client

void *wifi_receive(void *arg); Receives message from client and print out


Current condition of the code:

Connecting to client application, sending and receiving messages to/from client application works fine via WIFI on successful connection.


Problem:

When the client application is closed, the server realizes the connection with the client is dropped/disconnected. However, now I'm trying to make the server able to open the socket again and listen for incoming connections again to reconnect in when the client choose to connect with the server again.

This is my current logic/sequence flow:
  1. Open socket (Done!)
  2. Listen and accept any incoming connections from client (Done!)
  3. Send and Receive msgs upon successful connection with client (Done!)
  4. Now client disconnects
  5. I close the socket on server, and re-run the connect method again
  6. So far, code says Binding done, Accepted Connection, but it just hangs the program without doing anything (suppose to start send/recv)


Server Code:

Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>             //socket
#include <string.h>                 //strlen
#include <arpa/inet.h>              //inet_addr
#include <stdbool.h>                //boolean

struct WifiConnection {
    int socket_desc, c, client_sock;
    bool retry, connected, send, rcv;
    struct sockaddr_in server, client;
};
struct WifiConnection wifi;

void *wifi_connect(void *arg);
void *wifi_send(void *arg);
void *wifi_receive(void *arg);


void *wifi_connect(void *arg)
{
    struct WifiConnection *connect = (struct WifiConnection*)arg;

    do {
        // Create socket
        printf("Allocating Socket... ");
        connect->socket_desc = socket(AF_INET , SOCK_STREAM , 0);
        if (connect->socket_desc == -1) {
            printf("\nERROR: Could not create socket\n");
            sleep(3);
            continue;
        } else
            printf("Done!\n");

        // Prepare the sockaddr_in structure
        connect->server.sin_family = AF_INET;
        connect->server.sin_addr.s_addr = INADDR_ANY;
        connect->server.sin_port = htons( 8888 );

        // Bind
        printf("Bind socket... ");
        if( bind(connect->socket_desc,(struct sockaddr *)&connect->server , sizeof(connect->server)) < 0) {
            printf("\nERROR: Bind failed\n");
            sleep(3);
            continue;
        } else
            printf("Done!\nWaiting for incoming connections...\n");

        // Listen
        listen(connect->socket_desc , 10);

        // Accept and incoming connection
        connect->c = sizeof(struct sockaddr_in);

        // Accept connection from an incoming client
        connect->client_sock = accept(connect->socket_desc, (struct sockaddr *)&connect->client, (socklen_t*)&connect->c);
        if (connect->client_sock < 0) {
            perror("Accept failed");
            sleep(3);
            continue;
        }
        connect->connected = true;
        puts("Connection accepted");
    } while (!connect->connected);

    return 0;
}


/*----------------------------------------------------------------------------*/
void *wifi_send(void *arg)
{
    struct WifiConnection *connect = (struct WifiConnection*)arg;
    char message[2000], temp[20];

    //Send some data to client
    puts("Sending data to client...");
    while(1) {
        strcpy(message,"some things here");

        if(send(connect->client_sock , message , strlen(message)+1 , 0) < 0)
        {
            puts("Error sending from socket (Probably disconnected)");

            close(connect->socket_desc);
            puts("Connections closed. Reconnecting...");
            wifi_connect(connect);
        }
        // Clear buffer
        memset(message, 0, sizeof message);
    }

    return 0;
}


/*----------------------------------------------------------------------------*/
void *wifi_receive(void *arg)
{
    struct WifiConnection *connect = (struct WifiConnection*)arg;
    char message[2000];

    // Receive a message from client
    puts("Receiving data from client...");
    while(1) {
        if(recv(connect->client_sock, message, 2000, 0) > 0 ) {
            printf("Incoming Message: %s\n", message);
        }
        else {
            puts("Error reading from socket (Probably disconnected)");
            close(connect->socket_desc);
            puts("Connections closed. Reconnecting...");
            wifi_connect(connect);
        }
        // Clear buffer
        memset(message,0,sizeof message);
    }

    return 0;
}


/*----------------------------------------------------------------------------*/
int main(void)
{
    int rc;
    bool wifi_init = false;
    void *status;

    wifi_connect((void *)&wifi);

    while(!wifi_init) {
        if(wifi.connected && !wifi_init) {
            wifi_send((void *)&wifi);
            wifi_receive((void *)&wifi);
            wifi_init = true;
        }
    }

/*----------------------------------------------------------------------------*/
   // removed multithreading codes for simplicity.
   // in actual implementation theres pthread_join calls to ensure
   // the codes below will run after the program choose to exit.
/*----------------------------------------------------------------------------*/

    // Close all connections
    close(wifi.socket_desc);

    return 0;
}
Output:
Code:
Allocating Socket... Done!
Bind socket... Done!
Waiting for incoming connections...

Connection accepted
Receiving data from client...
Sending data to client...
Incoming Message: hi

Error reading from socket (Probably disconnected)
Connections closed. Reconnecting...

Allocating Socket... Done!
Bind socket... Done!
Waiting for incoming connections...
Connection accepted
^C ---- i have to manually terminate it since it stucks here forever
Help appreciated! Thank you!

PS. very new to C so not very sure about many things!

EDIT: Removed multithreading codes for simplicity. Please just assume that sending and receiving methods will still keep running when connection with client is lost.

EDIT: Problem seem to be resolved so far. Might be due to other parts of my code.

Last edited by bomby; 02-13-2016 at 08:22 AM.
 
Old 02-12-2016, 01:45 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You could receive a return of zero from the recv() and the errno from that recv() could be EAGAIN which just means you had no data when you called it that one time.
 
Old 02-13-2016, 05:29 AM   #3
bomby
LQ Newbie
 
Registered: Feb 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
Somehow seems to be solved, thanks so much for pointing that out as well

Last edited by bomby; 02-13-2016 at 08:34 AM.
 
  


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
Aodv implementation using socket programming in two laptops using lan connection ElenaVamp Linux - Networking 1 04-22-2015 02:40 PM
Connection of two systems using C socket programming !!! apanimesh061 Programming 3 10-06-2011 05:28 PM
permission denied error in php socket programming jayasekar Linux - Software 1 01-21-2010 06:04 AM
IO:Socket error:Connection refused nsfocus Programming 12 05-27-2008 12:16 AM

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

All times are GMT -5. The time now is 12:45 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