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

int main(int argc, char *argv[]){
    // established the socket
    char inputBuffer[256] = {};
    char message[] = {"Hi this is the server.\n"};
    int sockfd = 0;
    int forClientSocketfd = 0;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd == -1) printf("Fail to create the socket.");

    // socket connection
    struct sockaddr_in serverInfo, clientInfo;
    int addrlen = sizeof(clientInfo);
    bzero(&serverInfo, sizeof(serverInfo));

    serverInfo.sin_family = PF_INET;
    serverInfo.sin_addr.s_addr = INADDR_ANY;
    serverInfo.sin_port = htron(10024);
    bind(sockfd, (struct sockaddr *) &serverInfo, sizeof(serverInfo));
    listen(sockfd, 5);

    while(1){
        forClientSocketfd = accept(sockfd, (struct sockaddr*) &clientInfo, &addrlen);
        send(forClientSocketfd, message, sizeof(message), 0);
        recv(forClientSocketfd, inputBuffer, sizeof(inputBuffer), 0);
        printf("Received from client: %s\n", inputBuffer);
    }

    return 0;
}

This is the code for socket programming that I seen through from the net. when I compiled it, it throw the error message as below. Having no idea what's going on, even though searching through the internet. p.s. Client operate as normal.

enter image description here

3
  • I did a search for htron and couldn't find it. Did you mean htons? Commented Sep 29, 2019 at 13:08
  • Please show textual information as text, instead of picure of text. meta.stackoverflow.com/a/285557/7733418 Commented Sep 29, 2019 at 13:12
  • YuCheng Liao. The htons / htron typo is exteremely plausible (@JohnnyMopp), see linux.die.net/man/7/ip and search that page for the "sin_port " identifier you are assigning the result to. The question seems therefor typo-caused. Please give feedback on whether in your opinion there must be a different explanation and why. Commented Sep 29, 2019 at 13:22

1 Answer 1

3

you have a typo on line number 24 it should be htons and not htron

htons()

The htons function takes a 16-bit number in host byte order and returns a 16-bit number in network byte order used in TCP/IP networks(the AF_INET or AF_INET6 address family). The htons function can be used to convert an IP port number in host byte order to the IP port number in network byte order

also add the stdio header file to your code to remove the other warnings heres the final corrected code with no warnings or errors.

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

int main(int argc, char *argv[])
{
    // established the socket
    char inputBuffer[256] = {};
    char message[] = {"Hi this is the server.\n"};
    int sockfd = 0;
    int forClientSocketfd = 0;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
        printf("Fail to create the socket.");

    // socket connection
    struct sockaddr_in serverInfo, clientInfo;
    int addrlen = sizeof(clientInfo);
    bzero(&serverInfo, sizeof(serverInfo));

    serverInfo.sin_family = PF_INET;
    serverInfo.sin_addr.s_addr = INADDR_ANY;
    serverInfo.sin_port = htons(10024);
    bind(sockfd, (struct sockaddr *)&serverInfo, sizeof(serverInfo));
    listen(sockfd, 5);

    while (1)
    {
        forClientSocketfd = accept(sockfd, (struct sockaddr *)&clientInfo, &addrlen);
        send(forClientSocketfd, message, sizeof(message), 0);
        recv(forClientSocketfd, inputBuffer, sizeof(inputBuffer), 0);
        printf("Received from client: %s\n", inputBuffer);
    }

    return 0;
}

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

7 Comments

For one, the answer seems to pick up the idea from another users comment (not mine). Also, there is still some assumption, second guessing OPs intentions (though very plausibly), it would therefor be better to first clarify with OP, you do have the commenting privilege. Finally if you are correct, then the whole question is a typoe and hence off-topic.
glad it helped! please use a nice editor/ide to get a detailed info of warnings and errors I recommend vs code
@Yunnosch first thing commenting is for those if you are not sure about the answer/unclear po need some clarification about the topic,And I have provided the answer after trying it out myslef to make sure I add something new and it works as I claim
I am aware of everything in your comment and stand with everything I wrote in mine.
Linking to and quoting from windows documentation for a question not related to windows at all is not the best idea.
|

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.