MileJet
MileJet

Reputation: 69

TCP Server Crash

I am just wondering why this crashes when a client connects?? Its supposed to be a simple TCP server that when the client connects and sends a string, the server responds with the number of A's in the string. The server crashes as soon as the client types a letter in.

#include<io.h>
#include<stdio.h>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s , new_socket, master;
struct sockaddr_in server , address;
int c, valread;
char *message = "Welcome to Marshall's TCP Server!!";
int MAXRECV = 1024;
char *buffer;
char AmmtA = 'a';
char AmmtB = 'A';
int count = 0, x;
fd_set readfds;
buffer =  (char*) malloc((MAXRECV + 1) * sizeof(char));

printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
    printf("Failed. Error Code : %d",WSAGetLastError());
    return 1;
}

printf("Initialised.\n");

//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
    printf("Could not create socket : %d" , WSAGetLastError());
}

printf("Socket created.\n");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 9000 );

//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
    printf("Bind failed with error code : %d" , WSAGetLastError());
    exit(EXIT_FAILURE);
}

puts("Bind done");

//Listen to incoming connections
listen(s , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");

c = sizeof(struct sockaddr_in);

while( (new_socket = accept(s , (struct sockaddr *)&address, &c)) != INVALID_SOCKET )
{
    puts("Connection accepted");

send(new_socket , message , strlen(message) , 0);   

valread = recv( new_socket , buffer, MAXRECV, 0);

            if( valread == SOCKET_ERROR)
            {
                int error_code = WSAGetLastError();
                if(error_code == WSAECONNRESET)
                {
                    //Somebody disconnected , get his details and print
                    printf("Host disconnected unexpectedly , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));

                    //Close the socket and mark as 0 in list for reuse
                    closesocket( s );
                }
                else
                {
                    printf("recv failed with error code : %d" , error_code);
                }
            }
            if ( valread == 0)
            {
                //Somebody disconnected , get his details and print
                printf("Host disconnected , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));

                //Close the socket and mark as 0 in list for reuse
                closesocket( s );
            }

            else
            {
       for (x = 0; buffer[x] != '\0'; x++) {
                if (buffer[x] == AmmtA || buffer[x] == AmmtB)
                count++;
    }

        char feedback[150];
        sprintf(feedback, "There is %d A's in your string", count);
                feedback[MAXRECV] = '\0';
                printf("%s:%d - %s \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port), buffer);
                send( s , feedback , MAXRECV , 0 );
            }
free(buffer);
}

if (new_socket == INVALID_SOCKET)
{
    printf("accept failed with error code : %d" , WSAGetLastError());
    return 1;
}

closesocket(s);
WSACleanup();

return 0;

}

Upvotes: 0

Views: 584

Answers (2)

David Schwartz
David Schwartz

Reputation: 182753

   for (x = 0; buffer[x] != '\0'; x++) {
            if (buffer[x] == AmmtA || buffer[x] == AmmtB)
            count++;
}

Why are you comparing buffer[x] to 0. There's no special reason any particular entry in the buffer should be zero, and this can easily read off the end of the buffer. Perhaps you think buffer contains a string. But it doesn't. It contains whatever you read from the socket, and it has no special format or terminator.

Fortunately, you do know the number of bytes you read. You stored that in valread, so you want:

   for (x = 0; x < valread; x++) {

Upvotes: 2

Umamahesh P
Umamahesh P

Reputation: 1244

10057 - WSAENOTCONN - Socket is not connected.

The send call is correct and it uses new_socket. But the recv call uses socket/sd "s". The recv call should also use new_socket.

The error 10057 is returned because s is only bound to the local end point and not connected with the remote end whereas the new socket returned by the accept connected to the remote end.

Upvotes: 2

Related Questions