1

good evening, I am a newbie in using linux sockets under the c programming language.

for now I'm trying to make a client communicate with a server. basically the thing i'm stuck at is making the server accept a connection from the client and print the numbers after it receives them. here is the source code for the server so far:

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

int main(int argc, char *argv[])
{
    int listenfd = 0, connfd = 0, n = 0;
    struct sockaddr_in serv_addr;
    char sendBuff[1024], recvBuff[1024];

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(sendBuff, '0', sizeof(sendBuff));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(5000);

    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

    listen(listenfd, 10);

    while(1)
    {
        printf("in first while loop\n");
        connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
        while( (n = read(listenfd, recvBuff, sizeof(recvBuff)-1)) > 0)
        {
                printf("in second while loop\n");
                recvBuff[n] = 0;

                fputs(recvBuff, stdout);
        }
    }
}

I didn't post the client code since I don't think it's relevant. I have made a server which sends something to a client and it worked just fine. but I'm seriously confused by how I'm supposed to make the server accept input from the client. thank you in advance.

2
  • 2
    while( (n = read(connfd, recvBuff, sizeof(recvBuff)-1)) > 0) Commented Mar 20, 2013 at 23:56
  • 2
    Minor point, but they're not really "linux sockets". The socket API has been around since the 80s, known originally as Berkeley sockets, because they showed up in BSD UNIX first. It was the de facto standard for a long time before it became a POSIX standard with only minor changes. If you do use it correctly (and handling byte order issues is part of that) it should compile and run on a long list of platforms. Commented Mar 21, 2013 at 0:03

2 Answers 2

1

The other answers given (to read from connfd and not listenfd) will solve the problem in your code; this answer is presented to possibly help with the "serious confusion" surrounding server sockets.

A "socket" is most commonly thought of as a pipe between two applications, and this is how an accepted and connected socket behaves in our code (connfd in your example above).

However, in general, a "socket" is simply a "handle", and can be used in other ways as well, such as by a server who wishes to accept new connections by binding to a specific port. In your example above, listenfd is a socket handle that is being used to bind to a specific port, and can therefore accept connections. Each successful return from accept actually returns a new socket handle to a connected socket (e.g. connfd), one in which data can actually be read from and written to.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to change

connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
while( (n = read(listenfd, recvBuff, sizeof(recvBuff)-1)) > 0)

To this

connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
while( (n = read(connfd, recvBuff, sizeof(recvBuff)-1)) > 0)

As listening socket could only accept connections

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.