2012-09-06 53 views
0

我有一臺在Windows下用C編寫的服務器,但是我似乎只能連接到連接到我的計算機連接到的路由器的計算機上。任何想法爲什麼?這裏是我的代碼:爲什麼不能連接到我的服務器應用程序?

#ifndef WIN32_LEAN_AND_MEAN 
#define WIN32_LEAN_AND_MEAN 
#endif 

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

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

#define PORT "1234" 
#define BUFLEN 512 

DWORD ErrorMessage(char* msg, int error) 
{ 
    printf("ErrorMessage: %s\nCodErr: %d\n", msg, error); 
    return -1; 
} 

void PrintArray(char* v, unsigned int len) 
{ 
     for(unsigned int i = 0 ; i < len ; i++) 
    printf("%c",v[i]); 
    printf("\n"); 
} 

DWORD WINAPI ClientHandler(LPVOID lpParam) 
{ 
SOCKET ClientSocket = *((SOCKET*)lpParam); 

char recvbuf[BUFLEN]; 

int iResult,iSendResult; 

do 
{ 
    iResult = recv(ClientSocket, recvbuf, BUFLEN, 0); 

    if(iResult > 0) 
    { 
     PrintArray(recvbuf,iResult); 

     iSendResult = send(ClientSocket, recvbuf, iResult, 0); 

     if(iSendResult == SOCKET_ERROR) 
     { 
      closesocket(ClientSocket); 
      WSACleanup(); 
      return ErrorMessage("send",WSAGetLastError()); 
     } 
    } 

    else if(iResult == 0) 
     printf("Connection closing...\n"); 
    else 
    { 
     closesocket(ClientSocket); 
     WSACleanup(); 
     return ErrorMessage("recv",WSAGetLastError()); 
    } 
} 
while(iResult > 0); 

return 0; 
} 

int main() 
{ 
WSADATA wsaData; 

int iResult; 

iResult = WSAStartup(MAKEWORD(2,2),&wsaData); 

if(iResult != 0) 
{ 
    printf("WSAStartup failed and with error code: %d\n",iResult); 
    return ErrorMessage("WSAStartup",GetLastError()); 
} 

struct addrinfo *result = NULL, *ptr = NULL, hints; 

memset(&hints,0,sizeof(hints)); 

hints.ai_family = AF_INET; 
hints.ai_socktype = SOCK_STREAM; 
hints.ai_protocol = IPPROTO_TCP; 
hints.ai_flags = AI_PASSIVE; 

iResult = getaddrinfo(NULL,PORT,&hints,&result); 

if(iResult != 0) 
{ 
    printf("getaddrinfo failed %d\n",iResult); 
    WSACleanup(); 
    return -1; 
} 

SOCKET ListenSocket = INVALID_SOCKET; 

ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); 

if(ListenSocket == INVALID_SOCKET) 
{ 
    freeaddrinfo(result); 
    WSACleanup(); 
    return ErrorMessage("socket",WSAGetLastError()); 
} 

iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); 

if(iResult == SOCKET_ERROR) 
{ 
    freeaddrinfo(result); 
    closesocket(ListenSocket); 
    WSACleanup(); 
    return ErrorMessage("bind", WSAGetLastError()); 
} 

freeaddrinfo(result); 

if(listen(ListenSocket,5) == SOCKET_ERROR) 
{ 
    closesocket(ListenSocket); 
    WSACleanup(); 
    return ErrorMessage("listen",WSAGetLastError()); 
} 

SOCKET ClientSocket = INVALID_SOCKET; 

sockaddr sa; 

while(true) 
{ 
    ClientSocket = accept(ListenSocket, &sa /*NULL*/, NULL); 

    if(ClientSocket == INVALID_SOCKET) 
    { 
     closesocket(ListenSocket); 
     WSACleanup(); 
     return ErrorMessage("accept",WSAGetLastError()); 
    } 

    printf("New client arrived...\n"); 

    if(CreateThread(NULL, 0, ClientHandler, &ClientSocket, 0, NULL) == NULL) 
     ErrorMessage("CreateThread",GetLastError()); 
} 

iResult = shutdown(ClientSocket,SD_BOTH); 

if(iResult == SOCKET_ERROR) 
{ 
    closesocket(ClientSocket); 
    closesocket(ListenSocket); 
    WSACleanup(); 
    return ErrorMessage("shutsown",WSAGetLastError()); 
} 

closesocket(ListenSocket); 
WSACleanup(); 

return 0; 
} 
+0

您可能需要將端口從路由器轉發到運行此應用程序的服務器。 – Kevin

回答

0

問題不在你的程序中。問題在於你的路由器的配置。

嘗試從外部連接到計算機上的其他TCP/IP服務。

+0

非常感謝。 – conectionist

1

我無法關注您的代碼,但通常我會使用bindhtons。在您的代碼中,如何將字符串「1234」轉換爲數字值並不明顯。我在端口1234上寫了一個非常粗糙的簡單Web服務器,它只是提供一個虛擬的「Hello World」頁面。所有驗證已刪除,以保持示例簡短:

#include "stdafx.h" 
#include <tchar.h> 
#include <windows.h> 
#include <winsock.h> 

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

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    WSADATA wsaData = {0}; 
    WSAStartup(0x202, &wsaData); 
    SOCKET serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); 
    SOCKADDR_IN sin = {0}; 
    sin.sin_family = PF_INET; 
    sin.sin_port = htons(1234); 
    sin.sin_addr.s_addr = INADDR_ANY; 
    bind(serverSocket, (LPSOCKADDR) &sin, sizeof(sin)); 
    listen(serverSocket, 1); 
    SOCKET clientSocket = accept(serverSocket, NULL, NULL); 
    while (clientSocket != INVALID_SOCKET) 
    { 
     char request[1024] = {0}; 
     recv(clientSocket, request, 1024, 0); 
     static char html[] = 
      "HTTP/1.1 200 OK\n" 
      "Content-Type: text/html\n" 
      "\n" 
      "<HTML><HEAD><TITLE>Hello World!</TITLE></HEAD><BODY>Hello World!</BODY></HTML>\n\n"; 
     send(clientSocket, html, sizeof(html), 0); 
     closesocket(clientSocket); 
     clientSocket = accept(serverSocket, NULL, NULL); 
    } 
    closesocket(serverSocket); 
    WSACleanup(); 
    return 0; 
} 
+0

我的代碼是來自MSDN的winsocks教程的修改版本。顯然這是因爲我的路由器。但是當我遇到其他麻煩時,你的代碼可能會派上用場。謝謝。 – conectionist

相關問題