Phydor
Phydor

Reputation: 21

Simple server in c does not work

I coded this little server and it doesn't work. The message directly at the beginning isn't printed out as well and I don't know how to analyse the problem by using gdb. Could you help me? Is there any library missing or whats wrong?

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

#define PORT 7890

int main(void) {
    printf("HelloWorld");
    int sockfd, sock_client;

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        printf("Could no open socket\n");
    }
    int yes = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == -1) {
        printf("Coud not reuse\n");
    }

    printf("socket was created");


    struct sockaddr_in sockaddr_host, sockaddr_client;
    sockaddr_host.sin_family = AF_INET;
    sockaddr_host.sin_port = htons(PORT);
    sockaddr_host.sin_addr.s_addr = 0;
    memset(&(sockaddr_host.sin_zero), '\0', 8);



    if (bind(sockfd, (struct sockaddr *) &sockaddr_host, sizeof (sockaddr_host)) == -1) {
        printf("Could not bind socket");
    }
    if (listen(sockfd, 1) == -1) {
        printf("Could not start listening");

    } else {
        printf("Server is listening on %s: %d", inet_ntoa(sockaddr_host.sin_addr), ntohs(sockaddr_host.sin_port));
    }

    while (1) {
        socklen_t client_length = sizeof (sockaddr_client);
        if ((sock_client = accept(sockfd, (struct sockaddr *) &sockaddr_client, &client_length)) == -1) {
            printf("Could not accept connection");
        }
        printf("sever: got connection from %s on port %d", inet_ntoa(sockaddr_client.sin_addr), ntohs(sockaddr_client.sin_port));
        char message[] = "Hello\n";
        if (send(sockfd, message, sizeof (message), 0) == -1) {
            printf("Could not send message");
        }
        close(sock_client);
        close(sockfd);
    }

    return 0;
}

Upvotes: 0

Views: 129

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

If you were missing a library, the linker would already complain.

Standard output is usually line buffered. Add a newline after HelloWorld and you will see at least the first output.

printf("HelloWorld\n");

Same with the other printf.


After adding \n to each printf, you will see

HelloWorld
socket was created
Server is listening on 0.0.0.0: 7890

When you now connect to your server, e.g. with netcat

nc localhost 7890

your server will output

sever: got connection from 127.0.0.1 on port 36496


Some errors remain though.

if(send(sockfd, message, sizeof(message), 0) == -1) {

should be rather

if(send(sock_client, message, sizeof(message) - 1, 0) == -1) {

Otherwise the server sends the message to itself. Also sizeof(message) includes the final \0.

Finally, you shouldn't close(sockfd);, if you want to continue receiving further connection requests beyond the first one.

Upvotes: 1

Mohan
Mohan

Reputation: 1901

As you said

The message directly at the beginning isn't printed out as well

After the printf add fflush as

printf("HelloWorld");
fflush(stdout);

Is there any library missing

I don't thinks so any library is missing because you have successfully compiled the program and created the executable.

Upvotes: 0

Related Questions