Home > other >  How to resolve "SHUT_RDWR undeclared error" in shutdown() function to close the listening
How to resolve "SHUT_RDWR undeclared error" in shutdown() function to close the listening

Time:10-11

I am doing socket programming. I have created two .c files. One is called server.c and the other one is client.c. I want to test how socket programming works.

My server.c code is given below:

/*
 * server.c
 *
 *  Created on: 10 Oct 2022
 *      
 */
#include<Winsock2.h>
#include<ws2tcpip.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h> // Provides access to POSIX APIs
#define PORT 8080

int main()
{

    // Creating stuctures for port address
    struct sockaddr_in address; // sockaddr_in is the name of structure

    // declaring server and client socket and some variables
    int server_socket;
    int valread;
    int new_socket;
    int addrlen = sizeof(address);
    char buffer[1024] = {0}; // Creating an empty buffer to read data
    char* hello ="Hello from Raza..";

    // Creating a server socket
    server_socket = socket(AF_INET, SOCK_STREAM, 0);

    // If creating of socket is not successful
    if(server_socket < 0){
        perror("socket failed!");
        exit(EXIT_FAILURE);
    }

    // Populating the components of sockaddr_in structure
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(PORT);

    // Binding the socket to the IP Address
    if (bind(server_socket, (struct sockaddr*)&address,sizeof(address))<0){
        perror("Bind Failed!");
        exit(EXIT_FAILURE);
    }

    // Putting the socket in LISTENING state
    if (listen(server_socket, 3) < 0){
        perror("Listen");
        exit(EXIT_FAILURE);
    }

    if ((new_socket = accept(server_socket, (struct sockaddr*)&address, (socklen_t*)&addrlen)) < 0){
        perror("accept");
        exit(EXIT_FAILURE);
    }

    // Reading data from the socket into the buffer
    valread = read(new_socket, buffer, 1024);
    printf("%s\n", buffer);

    // Sending data to socket
    send(new_socket, hello, strlen(hello), 0);
    printf("Hello message sent\n");

    // Closing the socket
    close(new_socket);
    // closing the listening socket
    shutdown(server_socket, SHUT_RDWR);
    return 0;

    }




But the last function where I am shutting down the socket, I am getting the error that the SHUT_RDWR is undeclared. Error is shown below:

server.c: In function 'main':
server.c:71:26: error: 'SHUT_RDWR' undeclared (first use in this function)
  shutdown(server_socket, SHUT_RDWR);
                          ^~~~~~~~~
server.c:71:26: note: each undeclared identifier is reported only once for each function it appears in

I read that the shudown function is that part of sys/socket.h library. but when I include that also in my program using

#include<sys/socket.h>

Then I get the following error:

server.c:13:23: fatal error: sys/socket.h: No such file or directory
 #include<sys/socket.h>
                       ^
compilation terminated.

And if I remove the shutdown() line of code, then I get the following error:

C:\Users\RAZAJA~1\AppData\Local\Temp\ccGyb5QN.o:server.c:(.text 0x54): undefined reference to `socket@12'
C:\Users\RAZAJA~1\AppData\Local\Temp\ccGyb5QN.o:server.c:(.text 0x91): undefined reference to `htons@4'
C:\Users\RAZAJA~1\AppData\Local\Temp\ccGyb5QN.o:server.c:(.text 0xb2): undefined reference to `bind@12'
C:\Users\RAZAJA~1\AppData\Local\Temp\ccGyb5QN.o:server.c:(.text 0xe4): undefined reference to `listen@8'
C:\Users\RAZAJA~1\AppData\Local\Temp\ccGyb5QN.o:server.c:(.text 0x11c): undefined reference to `accept@12'
C:\Users\RAZAJA~1\AppData\Local\Temp\ccGyb5QN.o:server.c:(.text 0x199): undefined reference to `send@16'
collect2.exe: error: ld returned 1 exit status

I have mentioned the solutions I tried and their responses. What I read in some questions, is that if someone uses MinGW on windows then sys/socket.h doesn.t work. so then ws2tcpip must be used. So I included that (can be seen in the code).

Can someone please tell me what can be the solution here problem here? What mistake I am doing?

Additional Information: Editor Used: Visual Studio Code, Compilor Used: gcc using MinGW

CodePudding user response:

Like others have said in the comments, you are reading the documentation for Linux rather than for Windows OS.

In the WinAPI documentation for sockets, the shutdown() function is as follows:

int WSAAPI shutdown(
  [in] SOCKET s,
  [in] int    how
);

while the int how paramater can be:

SD_RECEIVE 0 - Shutdown receive operations.

SD_SEND 1 - Shutdown send operations.

SD_BOTH 2 - Shutdown both send and receive operations.

So in your case, use:

// closing the socket
shutdown(server_socket, SD_BOTH);
  • Related