Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

c++ - bind() fails with windows socket error 10038

I'm trying to write a simple program that will receive a string of max 20 characters and print that string to the screen.

The code compiles, but I get a bind() failed: 10038. After looking up the error number on msdn (socket operation on nonsocket), I changed some code from

int sock;

to

SOCKET sock

which shouldn't make a difference, but one never knows.

Here's the code:

#include <iostream>
#include <winsock2.h>
#include <cstdlib>
using namespace std;

const int MAXPENDING = 5;
const int MAX_LENGTH = 20;

void DieWithError(char *errorMessage);

int main(int argc, char **argv)
{
  if(argc!=2){
    cerr << "Usage: " << argv[0] << " <Port>" << endl;
    exit(1);
  }

  // start winsock2 library
  WSAData wsaData;
  if(WSAStartup(MAKEWORD(2,0), &wsaData)!=0){
    cerr << "WSAStartup() failed" << endl;
    exit(1);
  }

  // create socket for incoming connections
  SOCKET servSock;
  if(servSock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)==INVALID_SOCKET)
    DieWithError("socket() failed");

  // construct local address structure
  struct sockaddr_in servAddr;
  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = INADDR_ANY;
  servAddr.sin_port = htons(atoi(argv[1]));

  // bind to the local address
  int servAddrLen = sizeof(servAddr);
  if(bind(servSock, (SOCKADDR*)&servAddr, servAddrLen)==SOCKET_ERROR)
    DieWithError("bind() failed");

  // mark the socket to listen for incoming connections
  if(listen(servSock, MAXPENDING)<0)
    DieWithError("listen() failed");

  // accept incoming connections
  int clientSock;
  struct sockaddr_in clientAddr;
  char buffer[MAX_LENGTH];
  int recvMsgSize;

  int clientAddrLen = sizeof(clientAddr);
  for(;;){
    // wait for a client to connect
    if((clientSock=accept(servSock, (sockaddr*)&clientAddr, &clientAddrLen))<0)
      DieWithError("accept() failed");
    // clientSock is connected to a client


    // BEGIN Handle client
    cout << "Handling client " << inet_ntoa(clientAddr.sin_addr) << endl;
    if((recvMsgSize = recv(clientSock, buffer, MAX_LENGTH, 0)) <0)
      DieWithError("recv() failed");

    cout << "Word in the tubes: " << buffer << endl;
    closesocket(clientSock);

    // END Handle client
  }
}

void DieWithError(char *errorMessage)
{
  fprintf(stderr, "%s: %d
", errorMessage, WSAGetLastError());
  exit(1);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is with

servSock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)==INVALID_SOCKET

which does not associate as you think it does. Why would you even want to write something like that, what's wrong with

SOCKET servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(servSock == INVALID_SOCKET)
    DieWithError("socket() failed");

?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...