2017-09-23 25 views
0

winsock2를 사용하여 간단한 서버를 프로그래밍하려고합니다. 나는 컴파일을 제대로하지 못한다.C winsock2.h WS2_32.lib 정의되지 않은 연결

#define _WIN32_WINNT 0x0501 
#define WIN32_LEAN_AND_MEAN 
#include <windows.h> 
#include <stdio.h> 
#include <string.h> 
#include <winsock2.h> 
#include <ws2tcpip.h> 
#include "dump.h" 

#pragma comment(lib,"WS2_32.lib") 

#define PORT 7890 

int main(void) 
{ 
    //fd --> file descriptor 
    int sockfd, new_sockfd; //warten an sockfd, neue Verbindung an new_sockfd 
    struct sockaddr_in host_addr, client_addr; //Addressinformationen 
    //sockaddr_in aus winsock.h 

    //Laenge des Inputs --> winsock Alternative suchen 
    socklen_t sin_size; 

    int recv_length = 1, yes = 1; 
    char buffer[1024]; 
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) 
     //PF_INET --> Protocol family 
     //AF_INET --> Addres family 
     printf("%s\n", "in socket"); 
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(int)) == -1) 
     printf("%s\n", "setting socket option SO_REUSEADDR"); 

    //vorbereiten auf den bind-Befehl 
    host_addr.sin_family = AF_INET; 
    host_addr.sin_port = htons(PORT); 
    //htons --> host to network short 
    //Wandelt bei 16 Bit int Hostbytereihenfolge in Netzwerkbytereihenfolge 
    host_addr.sin_addr.s_addr = 0; //Automatisch mit meiner IP fuellen 
    memset(&(host_addr.sin_zero), '\0', 8); // Rest der Struktur mit 0 fuellen 

    if (bind(sockfd, (struct sockaddr *) &host_addr, sizeof(struct sockaddr)) == -1) 
     printf("%s\n", "binding to socket"); 
    if (listen(sockfd, 5) == -1) 
    { 
     printf("%s\n", "listening on socket"); 
    } 

    //Schleife um am PORT zu lauschen und Verbindungen zu akzeptieren 
    while (1) 
    { 
     sin_size = sizeof(struct sockaddr_in); 
     //accept gibt neuen sockfd zurueck ! 
     new_sockfd = 

     accept(sockfd, (struct sockaddr *) &client_addr, &sin_size); 
     if (new_sockfd == -1) 
     { 
      printf("%s\n", "accepting connection"); 
     } 
     printf("server: got connection from %s port %d\n", 
       inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); 
     send(new_sockfd, "Hello, world!\n", 13, 0); 
     recv_length = recv(new_sockfd, buffer, 1024, 0); 
     while (recv_length > 0) 
     { 
      printf("RECV: %d bytes \n", recv_length); 
      dump(buffer, recv_length); 
      recv_length = recv(new_sockfd, buffer, 1024, 0); 
     } 
     closesocket(new_sockfd); 
    } 
    return 0; 
} 

Linux에서는 socket.h가 처음 이었지만 windows7에서는 winsock2로 구현하려고했습니다. 나는 Windows SDK를 수동으로 설치했고 링크해야하는 Ws2_32.lib 라이브러리를 발견했다. CLION에서 gcc (mingw32)로 컴파일 중입니다. 나는 매개 변수 깃발을 설정했다

-lws2_32 -lwsock32 

나는이 질문에 대답하는 많은 게시물이 있지만 나에게는 아무런 효과가없는 것으로 알고있다.

CLION의 출력은 : 처음에

================================================== 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../crt2.o succeeded 
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../crt2.o 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtbegin.o succeeded 
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtbegin.o 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libws2_32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/ws2_32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libws2_32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/ws2_32.lib failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libws2_32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/ws2_32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0\libws2_32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/libws2_32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/ws2_32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/libws2_32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/ws2_32.lib failed 
attempt to open c:/mingw/bin/../lib/gcc/libws2_32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc/ws2_32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc\libws2_32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libws2_32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/ws2_32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libws2_32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/ws2_32.lib failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libws2_32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/ws2_32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib\libws2_32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libws2_32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../ws2_32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libws2_32.a succeeded 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libwsock32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/wsock32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libwsock32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/wsock32.lib failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libwsock32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/wsock32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0\libwsock32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/libwsock32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/wsock32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/libwsock32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/wsock32.lib failed 
attempt to open c:/mingw/bin/../lib/gcc/libwsock32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc/wsock32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc\libwsock32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libwsock32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/wsock32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libwsock32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/wsock32.lib failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/libwsock32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/wsock32.dll failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib\libwsock32.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libwsock32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../wsock32.dll.a failed 
attempt to open c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libwsock32.a succeeded 

이 :

gcc -Wl,-verbose -lws2_32 -lwsock32 -o simple_server main.c > linker.txt 

그것은 출력 다음 제공합니다

"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" --build C:\Users\Marcel\Desktop\Projekte\C-Projekte\Sequenz-Uebungen\Simple_Server\cmake-build-debug --target Simple_Server -- -j 2 
[ 50%] Linking C executable Simple_Server.exe 
CMakeFiles\Simple_Server.dir/objects.a(main.c.obj): In function `main': 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:49: undefined reference to `[email protected]' 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:53: undefined reference to `[email protected]' 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:58: undefined reference to `[email protected]' 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:64: undefined reference to `[email protected]' 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:66: undefined reference to `[email protected]' 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:78: undefined reference to `[email protected]' 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:84: undefined reference to `[email protected]' 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:83: undefined reference to `[email protected]' 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:85: undefined reference to `[email protected]' 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:86: undefined reference to `[email protected]' 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:91: undefined reference to `[email protected]' 
C:/Users/Marcel/Desktop/Projekte/C-Projekte/Sequenz-Uebungen/Simple_Server/main.c:93: undefined reference to `[email protected]' 
collect2.exe: error: ld returned 1 exit status 
CMakeFiles\Simple_Server.dir\build.make:95: recipe for target 'Simple_Server.exe' failed 
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Simple_Server.dir/all' failed 
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Simple_Server.dir/rule' failed 
mingw32-make.exe[3]: *** [Simple_Server.exe] Error 1 
mingw32-make.exe[2]: *** [CMakeFiles/Simple_Server.dir/all] Error 2 
mingw32-make.exe[1]: *** [CMakeFiles/Simple_Server.dir/rule] Error 2 
Makefile:117: recipe for target 'Simple_Server' failed 
mingw32-make.exe: *** [Simple_Server] Error 2 

나는 GCC로 컴파일 이제 다음 명령어를 사용 :

using internal linker script: 
================================================== 
/* Default linker script, for normal executables */ 
/* Copyright (C) 2014-2017 Free Software Foundation, Inc. 
    Copying and distribution of this script, with or without modification, 
    are permitted in any medium without royalty provided the copyright 
    notice and this notice are preserved. */ 
OUTPUT_FORMAT(pei-i386) 
SEARCH_DIR("/mingw/mingw32/lib"); SEARCH_DIR("/mingw/lib"); SEARCH_DIR("/usr/local/lib"); SEARCH_DIR("/lib"); SEARCH_DIR("/usr/lib"); 
+0

우리는 우리가 그것을 볼 수 없기 때문에 당신의 연결 명령을 사용하여 문제가 있는지 알 수 없습니다. clion 출력을 자세한 출력으로 바꿉니다 ('make VERBOSE = 1') –

+0

자세한 모드를 켜려면 어떻게합니까? – MNCODE

답변

1

먼저 #pragma comment(lib, "WS2_32.lib")을 제거하십시오. 이것은 Visual C++ 컴파일러 지시어입니다.

둘째, CMakeLists.txt를 제공하지 않았습니다. CLion은 크로스 플랫폼 솔루션을 목표로하고 있으며 CMake의 지정된 환경 (toolchain 탭)에 맞게 파일을 구성합니다. 즉, gcc를 호출하고 라이브러리를 수동으로 연결할 필요가 없으며 target_link_libraries(myexecutable ws2_32) 이후에 add_executable(myexecutable)이 필요합니다.

셋째, Win95와 역 호환성을 원하지 않는 한, 실제로는 wsock32.lib을 프로젝트에 링크 할 필요가 없습니다. 자세한 내용은 this answer을 참조하십시오.

최상위 CMake 파일 (I 성공적으로 빌드를 가지고)과 같아야합니다

# system 
cmake_minimum_required(VERSION 3.6) 
set(CMAKE_C_STANDARD 99) 

# project 
project(WinsockExample C) 

# sources 
set(source_files 
    main.c 
    ) 

# build 
add_executable(${CMAKE_PROJECT_NAME} ${source_files}) 
target_link_libraries(${CMAKE_PROJECT_NAME} ws2_32) 
+1

정말 많이 도와 줘서 고마워. :) – MNCODE

+0

@MNCODE 당신을 도울 수있어서 기쁩니다. 그런데 실수로'add_executable()'이'source_files' 변수를 사용한다고 가정했습니다. :) – Liastre