dxr
dxr

Reputation: 29

socket programming in linux by c

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?

Upvotes: 2

Views: 3513

Answers (3)

mti2935
mti2935

Reputation: 12027

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.

Upvotes: 0

dxr
dxr

Reputation: 29

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

Upvotes: 0

Joni
Joni

Reputation: 111359

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);
}

Upvotes: 2

Related Questions