2016-02-19 67 views
0

我使用從MSDN的源代碼,C++的Winsock服務器&客戶端,在服務器端,我將大部分函數的代碼時遇到一個訪問衝突錯誤。我的完整源代碼如下。C++的Winsock應用程序訪問衝突

任何幫助將是偉大的,在此先感謝! 這是我j420s,CPP文件。

#include "j420s.h" 
//Source From : MSDN Winsock Server Code. 
//Original Source : https ://msdn.microsoft.com/en- us/library/windows/desktop/ms737593(v=vs.85).aspx 
int __cdecl main(void) { 

WSADATA wsaData; 
int iResult = NULL; 

SOCKET ListenSocket = INVALID_SOCKET; 
SOCKET ClientSocket = INVALID_SOCKET; 

struct addrinfo *MySocketResult = NULL; 
struct addrinfo MySocket; 

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

// Initialize Winsock 
iResult = SocketInit(iResult, &wsaData); 
if (iResult == 1){ 

    return 1; 

} 

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

// Resolve the server address and port 
iResult = SocketAddrInfo(iResult, &MySocket, MySocketResult); 


// Create a SOCKET for connecting to server 
ListenSocket = SocketCreate(ListenSocket, MySocketResult); 
if (ListenSocket == 1){ 

    return 1; 

} 

// Setup the TCP listening socket 
iResult = SocketBind(iResult, ListenSocket, MySocketResult); 
if (iResult == 1) { 

    return 1; 

} 

iResult = SocketListen(iResult, ListenSocket, MySocketResult); 
if (iResult == 1) { 

    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 FOREVER! 
while (1 == 1){ 
    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); 

    } 

    } 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(); 
system("pause"); 
return 0; 

} 

int SocketAddrInfo(int iResult, addrinfo* MySocket, addrinfo* MySocketResult){ 

iResult = getaddrinfo(NULL, DEFAULT_PORT, MySocket, &MySocketResult); 

if (iResult != 0) { 

    printf("getaddrinfo failed with error: %d\n", iResult); 

    WSACleanup(); 

    return 1; 

} 

return iResult; 

} 
// Socket initialization 
int SocketInit(int iResult, WSADATA *wsaData){ 

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

if (iResult != 0) { 

    printf("WSAStartup failed with error: %d\n", iResult); 

    std::cout << "Server closing in 5 "; 

    for (int i = 4; i > 0; i--){ 

     Sleep(1 * 1000); 

     cout << i << " "; 

    } 

    cout << "Server closing now!" << endl; 

    return 1; 

} 

return iResult; 

} 
// Socket create function to create a socket for connecting to our server. 
SOCKET SocketCreate(SOCKET ListenSocket, addrinfo* MySocketResult){ 

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

if (ListenSocket == INVALID_SOCKET) { 

    printf("Socket failed with error: %ld\n", WSAGetLastError()); 

    freeaddrinfo(MySocketResult); 
    WSACleanup(); 

    std::cout << "Server closing in 5 "; 

    for (int i = 4; i > 0; i--){ 

     Sleep(1 * 1000); 

     cout << i << " "; 

    } 

    cout << "Server closing now!" << endl; 

    return 1; 

} 

return ListenSocket; 

} 

// Socket bind function for binding our socket to an address for incoming   connections. 
int SocketBind(int iResult, SOCKET ListenSocket, addrinfo* MySocketResult) { 

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

if (iResult == SOCKET_ERROR) { 

    printf("Bind failed with error: %d\n", WSAGetLastError()); 

    freeaddrinfo(MySocketResult); 
    closesocket(ListenSocket); 
    WSACleanup(); 

    std::cout << "Server closing in 5 "; 

    for (int i = 4; i > 0; i--){ 

     Sleep(1 * 1000); 

     cout << i << " "; 

    } 

    cout << "Server closing now!" << endl; 

    return 1; 

} 

return iResult; 

} 

// Socket listen function to listen for incoming connections. 
int SocketListen(int iResult, SOCKET ListenSocket, addrinfo* MySocketResult) { 

freeaddrinfo(MySocketResult); 

iResult = listen(ListenSocket, SOMAXCONN); 

if (iResult == SOCKET_ERROR) { 

    printf("Listen failed with error: %d\n", WSAGetLastError()); 

    closesocket(ListenSocket); 
    WSACleanup(); 

    std::cout << "Server closing in 5 "; 

    for (int i = 4; i > 0; i--){ 

     Sleep(1 * 1000); 

     cout << i << " "; 

    } 

    cout << "Server closing now!" << endl; 

    return 1; 

} 

return iResult; 

} 

// Socket accept connection function. 

SOCKET SocketAcceptConnection(SOCKET ClientSocket, SOCKET ListenSocket) { 

ClientSocket = accept(ListenSocket, NULL, NULL); 

if (ClientSocket == INVALID_SOCKET) { 

    printf("accept failed with error: %d\n", WSAGetLastError()); 

    closesocket(ListenSocket); 
    WSACleanup(); 

    std::cout << "Server closing in 5 "; 

    for (int i = 4; i > 0; i--){ 

     Sleep(1 * 1000); 

     cout << i << " "; 

    } 

    cout << "Server closing now!" << endl; 

    return 1; 

} 

closesocket(ListenSocket); 

return 0; 

} 

Here is my j420s.h file. 
#undef UNICODE 

#define WIN32_LEAN_AND_MEAN 

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

using std::cout; 
using std::endl; 

// 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 "10187" 
int SocketInit(int, WSADATA*); 
int SocketAddrInfo(int, addrinfo*, addrinfo*); 
SOCKET SocketCreate(SOCKET, addrinfo*); 
int SocketBind(int, SOCKET, addrinfo*); 
int SocketListen(int, SOCKET, addrinfo*); 
SOCKET SocketAcceptConnection(SOCKET, SOCKET); 

訪問衝突是介於此功能:

int SocketAddrInfo(int iResult, addrinfo* MySocket, addrinfo* MySocketResult){ 

iResult = getaddrinfo(NULL, DEFAULT_PORT, MySocket, &MySocketResult); 

if (iResult != 0) { 

    printf("getaddrinfo failed with error: %d\n", iResult); 

    WSACleanup(); 

    return 1; 

} 

return iResult; 

} 

我調用該函數像這樣:

iResult = SocketAddrInfo(iResult, &MySocket, MySocketResult); 

我相信這件事情與我的指針...

同樣,任何幫助將是巨大的!再次感謝!

+0

「訪問衝突則介於此功能」 這是確定嗎?這是用調試器還是其他方法證實的? – MikeCAT

回答

0

從函數SocketAddrInfo()返回getaddrinfo()的結果將被丟棄,並且函數MySocketResult中的MySocketResult仍然是NULL

之後,此NULL傳遞給SocketCreate(),它通過MySocketResult解除引用。它應該導致粉碎。

你應該通過一個指針MySocketResultSocketAddrInfo()並且有getaddrinfo()修改它。

int SocketAddrInfo(int iResult, addrinfo* MySocket, addrinfo** MySocketResult){ 
    iResult = getaddrinfo(NULL, DEFAULT_PORT, MySocket, MySocketResult); 
    if (iResult != 0) { 
     printf("getaddrinfo failed with error: %d\n", iResult); 
     WSACleanup(); 
     return 1; 
    } 
    return iResult; 
} 

如何撥打:

iResult = SocketAddrInfo(iResult, &MySocket, &MySocketResult); 
+0

驚人!太感謝了!完美的作品! –