why my TCP server code send a SYN/ACK on only first packet or only on the first connection?

user3189297
SOCKET sock;
SOCKET fd;
uint16 port = 18001;

void CreateSocket()
{
   struct sockaddr_in server, client;  // creating a socket address structure: structure contains ip address and port number
  WORD wVersionRequested;
WSADATA wsaData;
int len;

    printf("Initializing Winsock\n");


    wVersionRequested = MAKEWORD (2, 2);
    iResult =  WSAStartup (wVersionRequested, &wsaData);      
if (iResult != NO_ERROR)
  printf("Error at WSAStartup()\n"); 


    // create socket
    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0)    {
        printf("Could not Create Socket\n");
        //return 0;
    }

    printf("Socket Created\n");




    // create socket address of the server
    memset( &server, 0, sizeof(server));
    // IPv4 - connection
    server.sin_family = AF_INET;
    // accept connections from any ip adress
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    // set port
    server.sin_port = htons(18001);


    //Binding between the socket and ip address
    if(bind (sock, (struct sockaddr *) &server, sizeof(server)) < 0)
    {
        printf("Bind failed with error code: %d", WSAGetLastError());
    }

    //Listen to incoming connections
    if(listen(sock,3) == -1){
        printf("Listen failed with error code: %d", WSAGetLastError());
    }

    printf("Server has been successfully set up - Waiting for incoming connections");

    for(;;){
        len = sizeof(client);
        fd = accept(sock, (struct sockaddr*) &client, &len);

        if (fd < 0){
            printf("Accept failed");
            close(sock);
        }
        //echo(fd);
        printf("\n Process incoming connection from (%s , %d)", inet_ntoa(client.sin_addr),ntohs(client.sin_port));
//closesocket(fd);
    }

}

The server code is accepting a connection from the client via the ip address and the port number. It is sending SYN/ACK to the client only during the first connection and It is sending like below for the second time: RST / ACK (it is resetting during the second time).

Could anyone tell me what is the error in the above code ??

Andrey

Look at Accept multiple subsequent connections to socket

Here is a quote: "To service multiple clients, you need to avoid blocking I/O -- i.e., you can't just read from the socket and block until data comes in."

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

why setTimeout() only run my code once,at first time?

From Dev

Why is the compiler only compiling the first two sections of my code?

From Dev

only the first line of my javascript code works

From Dev

Why my listview only detects the first checkbox?

From Dev

Why is only my first HTTP request running?

From Dev

Why is my for only working on the first variable?

From Dev

Why is my for loop only grabbing first element?

From Dev

Why is my script is only getting the first row of my php table? below is my code

From Dev

Why Is My C# Code Only Displaying The First Row from my Database?

From Dev

NodeJS: TCP socket server only returns data the first time

From Dev

NodeJS: TCP socket server only returns data the first time

From Dev

Why is my list function only outputting first value of my array?

From Dev

PHP Ajax - Only send the first input value in database....Why?

From Dev

PHP/AJAX Why is my getAttribute only targeting the first line in the foreach code

From Dev

Why XMLHttpRequest send only "server connection established" in readyState attribut?

From Dev

How to send stdout in a tcp packet ? (only using bash commands)

From Dev

why my upvote button only works for the first post?

From Dev

Why is my recyclerview only displaying the content of the first item?

From Dev

Why is my script only accessing the first element in array?

From Dev

Why does my converted html string only show the first line?

From Dev

Why does my if/else condition with hasClass only fire first condition?

From Dev

Why is my script only accessing the first element in array?

From Dev

Why is my padding not being removed only from the first child

From Dev

Why does my loop only work correctly through the first iteration?

From Dev

Why is my regex function only running the first time around?

From Dev

Why does my function only returns the first object from the array

From Dev

Run code on first OnCreate only

From Dev

Why is the first cudaMalloc the only bottleneck?

From Dev

Why this only works for the first div?

Related Related

  1. 1

    why setTimeout() only run my code once,at first time?

  2. 2

    Why is the compiler only compiling the first two sections of my code?

  3. 3

    only the first line of my javascript code works

  4. 4

    Why my listview only detects the first checkbox?

  5. 5

    Why is only my first HTTP request running?

  6. 6

    Why is my for only working on the first variable?

  7. 7

    Why is my for loop only grabbing first element?

  8. 8

    Why is my script is only getting the first row of my php table? below is my code

  9. 9

    Why Is My C# Code Only Displaying The First Row from my Database?

  10. 10

    NodeJS: TCP socket server only returns data the first time

  11. 11

    NodeJS: TCP socket server only returns data the first time

  12. 12

    Why is my list function only outputting first value of my array?

  13. 13

    PHP Ajax - Only send the first input value in database....Why?

  14. 14

    PHP/AJAX Why is my getAttribute only targeting the first line in the foreach code

  15. 15

    Why XMLHttpRequest send only "server connection established" in readyState attribut?

  16. 16

    How to send stdout in a tcp packet ? (only using bash commands)

  17. 17

    why my upvote button only works for the first post?

  18. 18

    Why is my recyclerview only displaying the content of the first item?

  19. 19

    Why is my script only accessing the first element in array?

  20. 20

    Why does my converted html string only show the first line?

  21. 21

    Why does my if/else condition with hasClass only fire first condition?

  22. 22

    Why is my script only accessing the first element in array?

  23. 23

    Why is my padding not being removed only from the first child

  24. 24

    Why does my loop only work correctly through the first iteration?

  25. 25

    Why is my regex function only running the first time around?

  26. 26

    Why does my function only returns the first object from the array

  27. 27

    Run code on first OnCreate only

  28. 28

    Why is the first cudaMalloc the only bottleneck?

  29. 29

    Why this only works for the first div?

HotTag

Archive