简体   繁体   中英

Socket Programming in C, server code with an error

#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. ps Client operate as normal.

enter image description here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM