LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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
 
LinkBack Search this Thread
Old 02-13-2012, 08:49 AM   #1
DoctorZeus
LQ Newbie
 
Registered: Sep 2011
Location: Earth
Distribution: Arch Linux
Posts: 26

Rep: Reputation: Disabled
Where am I going wrong with this socket code?


Hello, I am trying to create a very basic socket program in C, (first time programming linux sockets) that the data is recieved and then displayed to the user.. However it doesn't seem to wait for a connection when I call "accept()" and simply sets conn_desc to -1 without pausing at all so of course itthen prints "connection attempt failed!". Can anyone see where I am going wrong?

Here is my code:

Code:
//Socket Header File for connections

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>

#define Buffer_Size 50

//Integers to hold all the socket data for both server and client
int sock_descriptor, conn_desc;

// Two socket address structures - One for the server itself and the other for client
struct sockaddr_in serv_addr, client_addr;

char buff[Buffer_Size];

void InitaliseSockets()
{

    sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);

    // Check that the socket descriptor has been created correctly
    if(sock_descriptor < 0)
    {
        printf("Failed creating socket\n");
        exit(0);
    }

    // Initialize the server address struct to zero
    bzero((char *)&serv_addr, sizeof(serv_addr));

    // Fill server's address family (j.e. get local ips)
    serv_addr.sin_family = AF_INET;

    // Allow connections from any IP
    serv_addr.sin_addr.s_addr = INADDR_ANY;

    //Listen on this port
    serv_addr.sin_port = htons(28886);

    if (bind(sock_descriptor, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("Failed to bind socket!\n");
        exit(0);
    }
    else
        printf("Waiting for commands...\n");
}

void WaitForConnect()
{
    printf("Waiting for connection...\n");
    unsigned int size = sizeof(client_addr);

    // Server blocks on this call until a client tries to establish connection.
    conn_desc = accept(sock_descriptor, (struct sockaddr *)&client_addr, &size); IT DOESNT SEEM TO WANT TO WAIT HERE, IT SIMPLY CONTINUES AND conn_desc is then == -1...
    if (conn_desc == -1)
    {
        printf("Conection Attempt failed!\n");
        exit(0);
    }
    else
        printf("Connected!\n");

    if ( read(conn_desc, buff, sizeof(buff)-1) > 0)
        printf("Received %s", buff);
    else
        printf("Failed receiving\n");

    close(conn_desc);
    close(sock_descriptor);
}
InitialiseSockets() is called at the start of the program, after that WaitForConnection() is called from main. Here is my main function:

Code:
int main()
{
    std::string InputCommand;

    InitaliseSockets();

    START:
    cout << "Type 'start' to start the server..\n";
    cin >> InputCommand;

    //Check for commands
    if (InputCommand == "start")
        WaitForConnect();
    else if (InputCommand == "exit")
        exit(0);
    else
        goto START;
    return 0;
}
Can anyone see why this might be happening?

Thanks

DoctorZeus

Last edited by DoctorZeus; 02-13-2012 at 08:55 AM.
 
Old 02-13-2012, 09:08 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 332

Rep: Reputation: 118Reputation: 118
I don't remember the sockets api very well, but shouldn't you listen() before you accept()?
 
1 members found this post helpful.
Old 02-13-2012, 10:27 AM   #3
DoctorZeus
LQ Newbie
 
Registered: Sep 2011
Location: Earth
Distribution: Arch Linux
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
I don't remember the sockets api very well, but shouldn't you listen() before you accept()?
You make a very good point ..

DUH! How did I miss that! :P

Thanks for the help! I knew I was missing something!

DoctorZeus
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem: Receiving wrong Data on Winsock from Unix Socket DeBlob Programming 2 01-28-2011 09:39 AM
Using Linux Socket Code in Windows(? Possible ?) ibaydan Linux - Networking 3 05-14-2008 03:19 AM
optimize socket networking code powah Linux - Networking 0 09-26-2006 04:47 PM
Problems in compiling socket code in c Prakhardeep Programming 2 12-22-2004 09:45 AM
Why is my socket unable to bind to the port in the C Code. mcp_achindra Programming 10 03-24-2004 08:34 AM


All times are GMT -5. The time now is 06:09 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration