2

I am getting error in socket programming. It will hang after bind() function,when executing. Here is the code:

int socket_rcv,new_socket;
struct sockaddr_in add1, add2;
int test[100];
    buffer= test;

printf("\n Initializing Socket....");

socket_rcv = socket(AF_INET,SOCK_STREAM,0);
if(socket_rcv == -1)
{
    perror("Socket not created.Error:");
    return 1;
}

printf("\n Socket created");

add1.sin_family = AF_INET;
add1.sin_addr.s_addr = htonl(INADDR_ANY);
add1.sin_port = htons(port_num);


if((socket_rcv = bind(socket_rcv,(struct sockaddr*)&add1,sizeof(add1))) == -1)
{
    perror("binding failed. Error:");
    return 1;
}
printf("\n Bind completed");

if(listen(socket_rcv,10) == -1)
{
    perror("listen failed.Error:");
    return 1;
}

socklen_t sizes = sizeof(add2);

printf("\n Waiting for connection.....");

new_socket = accept(socket_rcv, (struct sockaddr*) &add2, &sizes);

if(new_socket != -1)
{
    printf("\n %d, Accepted",new_socket);

    if(recv(new_socket,(char*)buffer,100,1)<0)
    {
        printf("\n No data received from %d socket",new_socket);
        return 1;
        }
    printf("\n Data Received\n");
}
else
{
    perror("Accept failed. Error:");
    return 1;
}

close(new_socket);
close(socket_rcv);
return 0;

My problem is while execute this code, it will hang after bind() function. The line which displayed at last is "Bind Completed", after that it will hang. SO is there problem in bind() function?

5
  • 4
    How do you create socket_rcv? Commented Sep 19, 2013 at 5:02
  • @dxr I think ur socket creation was not successful..Try to post required code !! Commented Sep 19, 2013 at 5:08
  • I defined socket_rcv as integer and same I used.like:int socket_rcv; if((socket_rcv=socket(AF_INET,SOCK_STREAM,0))==-1) { printf("\n Socket not created"); return 1; } printf("\n Socket created"); Commented Sep 19, 2013 at 5:21
  • Why are you masking your "client" struct ? Commented Sep 19, 2013 at 5:27
  • Abhineet: As per syntax int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); Commented Sep 19, 2013 at 5:35

3 Answers 3

2

Before you can accept a connection from a socket you have to do three things:

  1. Create the socket, using the socket function. From the comments you seem to be doing this correctly.
  2. Bind the socket to an address with the bind function. This tells the system on which address you are accepting connections. From the comments it looks like you are doing this correctly, too.
  3. Mark the socket as "passive" using the listen function. This tells the system that the socket will be used to accept incoming connections.

Here's a complete program that shows all the steps there are to accepting a connection.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdio.h>

int main(void) {
    int server_fd;
    struct sockaddr_in server_addr;

    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    server_addr.sin_port = htons(8080);

    if ((server_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        return 1;
    }

    if (bind(server_fd, (struct sockaddr*) &server_addr, sizeof(server_addr)) == -1) {
        perror("bind");
        return 1;
    }

    if (listen(server_fd, 10) == -1) {
        perror("listen");
        return 1;
    }

    struct sockaddr_in client_addr;
    socklen_t client_addrlen = sizeof(client_addr);

    int client_fd = accept(server_fd, (struct sockaddr*) &client_addr, &client_addrlen);
    if (client_fd == -1) {
        perror("accept");
        return 1;
    }

    puts("Connection accepted");

    close(client_fd);
    close(server_fd);
}
Sign up to request clarification or add additional context in comments.

17 Comments

yes...I wrote code in these format only....but I used bind function this way: if((socket_rcv=bind(socket_rcv(structsockaddr*)&server,sizeof(server)))==-1) { printf("\n binding failed"); return 1; } printf("\n Bind completed"); can i use socket_rcv like this?
bind returns either 0 or -1. Don't set socket_rcv to the return value of bind.
but when I removed socket_rcv, it will not to be executed. Actually the problem is in removing socket_rcv,after executing socket() function,will not execute bind function. the compile process will stop there only.
Are you saying that now the code will not compile? Maybe you should update the question with the complete code from the socket creation to the accept call.
no...I am not saying like that,but if I will remove socket_rcv then I am not able to compile because compiler will stop after creating socket. and sorry about the code, but check it with above comments you can find bind() and from my question you can find accept() and just below that comments,can find socket() functions.
|
0

all I upload the working code, what all errors I was getting that solved.

if((bind(socket_rcv,(struct sockaddr*)&add1,sizeof(add1))) == -1)
    {
        perror("binding failed. Error:");
        return 1;
    }
    printf("\n Bind completed");

Thank you all of you

Comments

0

Just an FYI, Another way to solve this problem may be to run your C program under tcpclient - i.e. tcpclient will spawn your program and open an tcp connection to the server, and pipe your program's stdout to the server, and pipe output from the server to your program's stdin. The nice thing about doing it this way is that you can let tcpclient to all the heavy lifting as far as setting up the socket, etc., and you can focus on the core function of your program. See http://cr.yp.to/ucspi-tcp/tcpclient.html for more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.