我正在尝试编译回显服务器代码。我正在Windows 10上使用CodeBlocks IDE。
#include<stdio.h>
#include<string.h>
#include<winsock2.h>
#pragma comment(lib, “ws2_32.lib”) //Winsock Library
//Vulnerable Function using strcpy
int vulnerable_function(char *input)
{
char buffer[128];
strcpy(buffer,input);
return 1;
}
//Main Function
int main()
{
WSADATA wsa;
SOCKET master , new_socket;
struct sockaddr_in server, address;
int addrlen, valread;
//Size of the receive buffer
char *buffer;
buffer = (char*) malloc((1024 + 1) * sizeof(char));
WSAStartup(MAKEWORD(2,2),&wsa);
//Create a socket
master = socket(AF_INET , SOCK_STREAM , 0 );
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 10000 );
//Bind
bind(master ,(struct sockaddr *)&server , sizeof(server));
//Listen for incoming connections
listen(master , 1);
//Accept the incoming connection
puts("Waiting for incoming connections…");
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(master , (struct sockaddr *)&address, (int *)&addrlen);
printf("New connection , socket fd is %d , ip is : %s , port : %d \n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
valread = 1;
//Receiving Loop
while(valread != 0)
{
valread = recv(new_socket, buffer, 1024, 0);
if ( valread == 2) //Server close the connection when a return is send from the client
{
closesocket( new_socket );
exit(0);
}
buffer[valread]=' ';
vulnerable_function(buffer);
printf("%s:%d – %s \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port), buffer);
send( new_socket , buffer , valread , 0 );
}
closesocket(new_socket);
WSACleanup();
return 0;
}
我遇到几个错误
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x60): undefined reference to `WSAStartup@8'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x7f): undefined reference to `socket@12'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0xb0): undefined reference to `htons@4'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0xd7): undefined reference to `bind@12'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0xed): undefined reference to `listen@8'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x125): undefined reference to `accept@12'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x13d): undefined reference to `ntohs@4'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x151): undefined reference to `inet_ntoa@4'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x19d): undefined reference to `recv@16'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x1b4): undefined reference to `closesocket@4'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x1eb): undefined reference to `ntohs@4'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x1ff): undefined reference to `inet_ntoa@4'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x240): undefined reference to `send@16'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x258): undefined reference to `closesocket@4'
C:\Users\manu\Documents\manu\hack_learning\EchoServer.o:EchoServer.c:(.text+0x260): undefined reference to `WSACleanup@0'
collect2.exe: error: ld returned 1 exit status
正在编译代码的命令是
mingw32-gcc.exe -c C:\Users\manu\Documents\manu\hack_learning\EchoServer.c -o C:\Users\manu\Documents\manu\hack_learning\EchoServer.o
mingw32-g++.exe -o C:\Users\manu\Documents\manu\hack_learning\EchoServer.exe C:\Users\manu\Documents\manu\hack_learning\EchoServer.o
我认为问题在于IDE并未链接具有socket
代码实现的库。
我应该链接的库的名称是什么以及如何获取它。我的系统上安装了Win 10 SDK。