Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8563913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:04:49+00:00 2026-06-11T17:04:49+00:00

The following code is my TCP server program: #include #include #pragma comment (lib,ws2_32.lib) #define

  • 0

The following code is my TCP server program:

    #include
    #include
    #pragma comment (lib,"ws2_32.lib")
    #define PORT 8888
    #define ADDR "127.0.0.1"

int main() { WSADATA wsock; SOCKET listensocket,connectsocket; SOCKADDR_IN seraddr,cliaddr; int cliaddrsize=sizeof(cliaddr); int nret=0; char buf[100]; printf("init socket ...\n"); if(WSAStartup(MAKEWORD(2,2),&wsock)!=0) { printf("WSAStartup() failed %d\n",WSAGetLastError()); return 0; } printf("init successfully\n"); printf("create TCP socket...\n"); if((listensocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET) { printf("socket create failed %d\n",WSAGetLastError()); WSACleanup(); return 0; } printf("socket create successfully\n"); seraddr.sin_family=AF_INET; seraddr.sin_addr.s_addr=inet_addr(ADDR); seraddr.sin_port=htons(PORT); if(bind(listensocket,(SOCKADDR *)&seraddr,sizeof(seraddr))==SOCKET_ERROR) { printf("bind failed %d\n",WSAGetLastError()); closesocket(listensocket); WSACleanup(); return 0; } printf("bind successfully\n"); if(listen(listensocket,5)==SOCKET_ERROR) { printf("listen failed %d\n",WSAGetLastError()); closesocket(listensocket); WSACleanup(); return 0; } printf("wait for a connection on port %d\n",PORT); if(connectsocket=accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrsize)==INVALID_SOCKET) //accept { printf("accept failed %d\n",WSAGetLastError()); closesocket(listensocket); WSACleanup(); return 0; } printf("get connection from %s : %d successfully\n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));//NB 啊 closesocket(listensocket); printf("wait to receive data...\n"); memset(buf,0,sizeof(buf)); while(1) { if(nret=recv(connectsocket,buf,sizeof(buf),0)==SOCKET_ERROR) //recv { printf("recv failed %d\n",WSAGetLastError()); closesocket(connectsocket); WSACleanup(); return 0; } printf(buf); printf("\n"); if(strncmp(buf,"exit",sizeof("exit"))==0) { printf("exit the loop\n"); break; } if(nret=send(connectsocket,buf,sizeof(buf),0)==SOCKET_ERROR) { printf("send failed %d\n",WSAGetLastError()); } } closesocket(connectsocket); WSACleanup(); return 0; }

And I use my network debugging assistent software to run as a TCP client. As follows:

I started my TCP server program, it went as follows:

(...cannot upload images...)

But when I click on my TCP client, the TCP server went wrong :
the dos box shows that : recv failed 10038

My question is why it went wrong ? And how to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:04:50+00:00Added an answer on June 11, 2026 at 5:04 pm

    This:

    if(connectsocket=accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrsize)
           ==INVALID_SOCKET)
    

    will result in connectsocket having a value of 0, which does not refer to a valid socket descriptor which the error code 10038 means:

    An operation was attempted on something that is not a socket.

    because of operator precedence:

    // Result of this will be 0 (false) when result of accept()
    // is not `INVALID_SOCKET`
    accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrsize) == INVALID_SOCKET
    
    // Which is then assigned to connectsocket
    connectsocket = 0
    
    // And the failing if branch is not entered
    if (connectsocket)
    

    You need parenthesis around the assignment:

    if((connectsocket=accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrsize))
        ==INVALID_SOCKET)
    

    which you have already done for the earlier call to socket().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to make a php tcp/ip server with the following code: <?php //
In the following code: tcp::socket socket(io_service); tcp::endpoint ep(boost::asio::ip::address::from_string(addr), i); socket.async_connect(ep, &connect_handler); socket.close(); is it
i have the following main code: #include ComHandler.h #include socketMessage.h #define THIS_IP localhost #define
Can anyone help me debug this program? The following is server code : package
The following code using boost::asio will not compile: #ifndef _SERVER_H_ #define _SERVER_H_ #include Connection.h
I am starting our JMX server using the following code: Integer port = 8291;
The following is my current code for connecting to gmail's smtp server on port
I am having difficulties in implementing a simple TCP server. The following code is
I have code that looks like the following: //unrelated code snipped resolver.reset(new tcp::resolver(iosvc)); tcp::resolver::query
I have the following code to start my own Cocoa HTTP Server . In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.