2016-04-05 1 views
0

シンプルなサーバーとクライアントでC++でネットワークを試しています。私はWinsockの例を使用しています。私はコード::ブロックを使用してサーバを構築しようとするたびに、私は次のエラーを取得:私はこの質問から解決しようとしているWinsockサーバーとクライアントC++コード - getaddrinfoがこのスコープで宣言されていません

||=== Build: Debug in Server Test (compiler: GNU GCC Compiler) ===| 
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|12|warning: ignoring #pragma comment [-Wunknown-pragmas]| 
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp||In function 'int main()':| 
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|47|error: 'getaddrinfo' was not declared in this scope| 
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|57|warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'int' [-Wformat]| 
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|58|error: 'freeaddrinfo' was not declared in this scope| 
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|67|error: 'freeaddrinfo' was not declared in this scope| 
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|73|error: 'freeaddrinfo' was not declared in this scope| 
||=== Build failed: 4 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===| 

winsock compiling error, it cant find the addrinfo structures and some relating functions

をしかし、それは4から上がりましたエラーから36エラー。

クライアントコードで同じ問題が発生しています。

サーバコード:

#undef UNICODE 

#define WIN32_LEAN_AND_MEAN 

#include <windows.h> 
#include <winsock2.h> 
#include <ws2tcpip.h> 
#include <stdlib.h> 
#include <stdio.h> 

// Need to link with Ws2_32.lib 
#pragma comment (lib, "Ws2_32.lib") 
// #pragma comment (lib, "Mswsock.lib") 

#define DEFAULT_BUFLEN 512 
#define DEFAULT_PORT "27015" 

int __cdecl main(void) 
{ 
    WSADATA wsaData; 
    int iResult; 

    SOCKET ListenSocket = INVALID_SOCKET; 
    SOCKET ClientSocket = INVALID_SOCKET; 

    struct addrinfo *result = NULL; 
    struct addrinfo hints; 

    int iSendResult; 
    char recvbuf[DEFAULT_BUFLEN]; 
    int recvbuflen = DEFAULT_BUFLEN; 

    // Initialize Winsock 
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData); 
    if (iResult != 0) { 
     printf("WSAStartup failed with error: %d\n", iResult); 
     return 1; 
    } 

    ZeroMemory(&hints, sizeof(hints)); 
    hints.ai_family = AF_INET; 
    hints.ai_socktype = SOCK_STREAM; 
    hints.ai_protocol = IPPROTO_TCP; 
    hints.ai_flags = AI_PASSIVE; 

    // Resolve the server address and port 
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); 
    if (iResult != 0) { 
     printf("getaddrinfo failed with error: %d\n", iResult); 
     WSACleanup(); 
     return 1; 
    } 

    // Create a SOCKET for connecting to server 
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); 
    if (ListenSocket == INVALID_SOCKET) { 
     printf("socket failed with error: %ld\n", WSAGetLastError()); 
     freeaddrinfo(result); 
     WSACleanup(); 
     return 1; 
    } 

    // Setup the TCP listening socket 
    iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); 
    if (iResult == SOCKET_ERROR) { 
     printf("bind failed with error: %d\n", WSAGetLastError()); 
     freeaddrinfo(result); 
     closesocket(ListenSocket); 
     WSACleanup(); 
     return 1; 
    } 

    freeaddrinfo(result); 

    iResult = listen(ListenSocket, SOMAXCONN); 
    if (iResult == SOCKET_ERROR) { 
     printf("listen failed with error: %d\n", WSAGetLastError()); 
     closesocket(ListenSocket); 
     WSACleanup(); 
     return 1; 
    } 

    // Accept a client socket 
    ClientSocket = accept(ListenSocket, NULL, NULL); 
    if (ClientSocket == INVALID_SOCKET) { 
     printf("accept failed with error: %d\n", WSAGetLastError()); 
     closesocket(ListenSocket); 
     WSACleanup(); 
     return 1; 
    } 

    // No longer need server socket 
    closesocket(ListenSocket); 

    // Receive until the peer shuts down the connection 
    do { 

     iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); 
     if (iResult > 0) { 
      printf("Bytes received: %d\n", iResult); 

     // Echo the buffer back to the sender 
      iSendResult = send(ClientSocket, recvbuf, iResult, 0); 
      if (iSendResult == SOCKET_ERROR) { 
       printf("send failed with error: %d\n", WSAGetLastError()); 
       closesocket(ClientSocket); 
       WSACleanup(); 
       return 1; 
      } 
      printf("Bytes sent: %d\n", iSendResult); 
     } 
     else if (iResult == 0) 
      printf("Connection closing...\n"); 
     else { 
      printf("recv failed with error: %d\n", WSAGetLastError()); 
      closesocket(ClientSocket); 
      WSACleanup(); 
      return 1; 
     } 

    } while (iResult > 0); 

    // shutdown the connection since we're done 
    iResult = shutdown(ClientSocket, SD_SEND); 
    if (iResult == SOCKET_ERROR) { 
     printf("shutdown failed with error: %d\n", WSAGetLastError()); 
     closesocket(ClientSocket); 
     WSACleanup(); 
     return 1; 
    } 

    // cleanup 
    closesocket(ClientSocket); 
    WSACleanup(); 

    return 0; 
} 

答えて

0

私は、このマシン上でVSを持っていますが、WINDOWS.H前winsock2.hインポートしようとしないでください。

私はgccでCygwinを持っている、とコマンドラインでソースをコンパイル:

gccのserver.cpp -lws2_32 -oserver.exe

が動作しているようです:

のtelnet 127.0.0.1 27015

こんにちは
こんにちは

+0

あなたの答えは、残念なことに助けには見えませんでした。私はCygwinコンパイラを使ってみました。私は '#include 'を '#include 'の前に置き、それを削除しました。私は同じエラーがあります。私はgcc.exeを次のようにコマンドラインから直接実行しようとしました: 'C:¥Program Files(x86)¥CodeBlocks¥MinGW¥bin> gcc" C:¥Users¥****¥Desktop¥Programming¥CodeBlocks Projects¥Server Test \ main.cpp "-lws2_32 -oserver.exe'を実行し、' gcc:error:CreateProcess:No such file or directory'を取得しました。私はWindows 7 x64とCode :: Blocksを使用しています。 – FluorescentGreen5

+0

パスにもgccが存在するbinディレクトリはありますか?例えばあなたは、私はWindowsを使用していていても、Linuxの話をしているように見える –

+0

は/ usr/binに... $これはgcc は/ usr/binに/ gccを: $は は/ usr/local/binに$ PATHをエコー私が指定した通りです。 "\t あなたのパスにgccがある/ binディレクトリはありますか?" .cppファイルのパスについて話していますか? – FluorescentGreen5

0

誰かが、まだこの問題に苦労している場合。あなたがしなければならないのは、以下の通りです:

  • このように、あなたのコードの先頭に_WIN32_WINNTを定義します(ws2tcpip.hによって使用される)#define _WIN32_WINNT 0x501
  • はこのようにコンパイルします。g++ prog.c -o prog -lws2_32

これらの手順は、サーバーとクライアントの両方のコードで作成されます。

これは役に立ちます。

関連する問題