我正在做套接字編程。我創建了兩個 .c 檔案。一個被稱為server.c,另一個是client.c。我想測驗套接字編程是如何作業的。
我的server.c代碼如下:
/*
* 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;
}
但是我關閉套接字的最后一個函式,我得到了 SHUT_RDWR 未宣告的錯誤。錯誤如下圖所示:
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
我讀到 shudown 函式是sys/socket.h庫的一部分。但是當我將它也包含在我的程式中時
#include<sys/socket.h>
然后我收到以下錯誤:
server.c:13:23: fatal error: sys/socket.h: No such file or directory
#include<sys/socket.h>
^
compilation terminated.
如果我洗掉這shutdown()行代碼,則會收到以下錯誤:
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
我已經提到了我嘗試過的解決方案及其回應。我在一些問題中讀到的是,如果有人在 Windows 上使用 MinGW,那么sys/socket.h就不起作用。所以ws2tcpip必須使用then。所以我把它包括在內(可以在代碼中看到)。
有人可以告訴我這里的解決方案是什么嗎?我在做什么錯誤?
附加資訊: 使用的編輯器:Visual Studio 代碼,使用的編譯器:使用 MinGW 的 gcc
uj5u.com熱心網友回復:
就像其他人在評論中所說的那樣,您正在閱讀 Linux 而不是 Windows 作業系統的檔案。
在sockets 的 WinAPI 檔案中,shutdown()函式如下:
int WSAAPI shutdown(
[in] SOCKET s,
[in] int how
);
而int how引數可以是:
SD_RECEIVE 0 - Shutdown receive operations.
SD_SEND 1 - Shutdown send operations.
SD_BOTH 2 - Shutdown both send and receive operations.
所以在你的情況下,使用:
// closing the socket
shutdown(server_socket, SD_BOTH);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515797.html
標籤:C插座
