无法使用winsock2.lib将数据发送到客户端

时间:2013-06-18 12:50:17

标签: c++ sockets

我们有tcp / ip应用程序项目,在源代码中使用winsock2.lib,服务器和客户端函数如下:

服务器:

#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include<string>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment (lib, "Ws2_32.lib")
using namespace::std;



int server()
{
    string SERVER_ADDRESS= "172.17.11.138";
    int PORT_NUMBER = 27015;

    sockaddr_in server;
    server.sin_family=AF_INET;
    server.sin_port=htons(PORT_NUMBER);
    server.sin_addr.S_un.S_addr=inet_addr(&SERVER_ADDRESS[0]);


    WSADATA wsaData;
        int iResult;
    SOCKET ListenSocket = INVALID_SOCKET;
        SOCKET ClientSocket = INVALID_SOCKET;

   // Initialize Winsock
       iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
       if (iResult != 0) {
       printf("WSAStartup failed with error: %d\n", iResult);
       return 1;
    }
    int iSendResult;
char recvbuf[256];
int recvbuflen = 256;

// Create a SOCKET for connecting to server
ListenSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
    printf("socket failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
    return 1;
}

// Setup the TCP listening socket
iResult = bind( ListenSocket, (struct sockaddr*)&server,sizeof(server));
if (iResult == SOCKET_ERROR) {
    printf("bind failed with error: %d\n", WSAGetLastError());
    closesocket(ListenSocket);
    WSACleanup();
    return 1;
}

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);

// 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;
}
}

客户端:

#define WIN32_LEAN_AND_MEAN

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


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


int client()
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
string SERVER_ADDRESS= "172.17.11.138";

char *sendbuf = "this is a test";
char recvbuf[256];
int PORT_NUMBER = 27015;
int iResult;

sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(PORT_NUMBER);
server.sin_addr.S_un.S_addr=inet_addr(&SERVER_ADDRESS[0]);
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
    printf("WSAStartup failed with error: %d\n", iResult);
    return 1;
}

// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
    printf("socket failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
    return 1;
}

iResult = connect( ConnectSocket,(struct sockaddr*)&server,sizeof(server));
if (iResult == SOCKET_ERROR) {
    closesocket(ConnectSocket);
    printf("socket failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
    return 1;
}
if (ConnectSocket == INVALID_SOCKET) {
    printf("Unable to connect to server!\n");
    WSACleanup();
    return 1;
    }

 iResult = recv(ConnectSocket, recvbuf, 256, 0);
    if ( iResult > 0 )
        printf("Bytes received: %d\n", iResult);
    else if ( iResult == 0 )
        printf("Connection closed\n");
    else
        printf("recv failed with error: %d\n", WSAGetLastError());
}

我们编译了上面的代码,服务器和客户端可以相互连接(用tcpview应用程序检查)但是,我们想从服务器向客户端发送数据,winsock2返回错误10057(如果我们关闭监听套接字错误也将是10038),发送函数返回-1。

1 个答案:

答案 0 :(得分:0)

嗯,有一个问题是,你从服务器发送了什么?这段代码:

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

似乎正在发送未初始化缓冲区iResult的{​​{1}}字节(从最后listen调用开始为0)。客户端似乎有一个recvbuf,在任何地方都没有使用。看起来像是一些要清理的错误,你将会运作......