简体   繁体   中英

Winsock not sending in a while loop

I am very new to networking and have an issue with sending messages during a while loop.

To my knowledge I should do something along the lines of this:

Create Socket()
Connect()

While
     Do logic
     Send()
End while

Close Socket()

However it sends once and returns -1 there after.

The code will only work when I create the socket in the loop.

While
     Create Socket()
     Connect()
     Do logic
     Send()
     Close Socket()
End while

Here is a section of the code I am using but doesn't work:

//init winsock
WSAStartup(MAKEWORD(2, 0), &wsaData);

//open socket
    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    //connect
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family      = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr(ipaddress);
    serveraddr.sin_port        = htons((unsigned short) port);
    connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr));

while(true) {

    if (send(sock, request.c_str(), request.length(), 0)< 0 /*!= request.length()*/) {
        OutputDebugString(TEXT("Failed to send."));
    } else {
        OutputDebugString(TEXT("Activity sent."));
    }
    Sleep(30000);
}
//disconnect
closesocket(sock);

//cleanup
WSACleanup();

The function CheckForLastError() returns:10053

WSAECONNABORTED Software caused connection abort. An established connection was aborted by the software in your host computer, possibly due to a data transmission time-out or protocol error

Thanks

To understand how and why your program fails,you have to understand the functions you use. Some of them are blocking functions and some are them not. Some of them need previous calles of other functions and some of them don't. Now from what i understand we are talking about a client here,not a server. The client has only non blocking functions in this case. That means that whenever you call a function,it will be executed without waiting. So send() will send data the second it is called and the stream will go on to the next line of code. If the information to be sent was not yet ready...you will have a problem,since nothing will be sent. To solve it you could use some sort of a delay. The problem with delays is that they are Blocking functions meaning your stream will stop once it hits the delay. To solve it you can create a thread and lock it untill the information is ready to be sent. But that would do the job for one send(). You will send the info and thats that. If you want to hold the communication and send repeatedly info,you will need to create a while loop. once you have a while loop you dont have to worry about anything. That is because you can verify that the information is ready with a stream control and you can use send over and over again before terminating the connection. Now the question is what is happening on the server side of things? "ipaddress" should hold the ip of the server. The server might reject your request to connect.Or worst he might accept your request but he is listening with diffrent settings in relation to your client.Meaning that maybe the server is not reciving (does not have recv() function)information and you are trying to send info... that might resault in errors/crashes and what not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM