本文介绍了bind()失败,出现Windows套接字错误10038的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个简单的程序,将接收一个最多20个字符的字符串,并打印该字符串到屏幕。

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

代码编译,但我得到一个bind()失败:10038.查找msdn上的错误号(nonsocket上的套接字操作),我更改了一些代码

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;

SOCKET sock

这不应该有什么区别,

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

以下是代码:

#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\n", errorMessage, WSAGetLastError());
  exit(1);
}


推荐答案

p>

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

这篇关于bind()失败,出现Windows套接字错误10038的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:46