SOCKETS IN C - TCP Sequence Number

Asked

Viewed 181 times

0

I could use a little help with sockets in C. My teacher passed a job in which I must perform communication TCP between client and server where I must execute some rules..

  • 1 - The Client sends a message to the server. The message must contain the sequence number and amount of bytes loaded by message.
  • 2 - The server must receive the message and send an acknowledgement to the client. The recognition message shall contain the number of sequence of the recognition message means the sequence number of the next segment to be sent by the customer.

Could someone please explain to me how the sequence number works and if possible help me encode it? The client/server TCP communication part is attached and ready.

Snippet of client code:

 sock = socket(AF_INET , SOCK_STREAM , 0);
    if (sock == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons( 8888 );

    //Connect to remote server
    if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        perror("connect failed. Error");
        return 1;
    }

    puts("Connected\n");

    //keep communicating with server
    while(1)
    {
        printf("Enter message : ");
    gets(message);
    fflush(stdin);
        //scanf("%s" , message);

        //Send some data
        if( send(sock , message, strlen(message) , 0) < 0)
        {
            puts("Send failed");
            return 1;
        }

        //Receive a reply from the server
        if( recv(sock , server_reply , 2000 , 0) < 0)
        {
            puts("recv failed");
            break;
        }

        puts("Server reply :");
        //puts(server_reply);
    int vl = 0;
    for(int i =0; server_reply[i] != '\0';i++)
    {
        if(server_reply[i] != ' ')
            vl += (int)server_reply[i];
    }
        printf("%d\n",vl);
}
    close(sock);

Chunk of server code:

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
    printf("Could not create socket");
}
puts("Socket created");

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

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
    //print the error message
    perror("bind failed. Error");
    return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);

//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
    perror("accept failed");
    return 1;
}
puts("Connection accepted");

//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
    //Send the message back to client
    write(client_sock , client_message , strlen(client_message));
}

if(read_size == 0)
{
    puts("Client disconnected");
    fflush(stdout);
}
else if(read_size == -1)
{
    perror("recv failed");
}

1 answer

0

Assemble the messages to be sent to the server following the requested layout, containing the sequence number and the size of the message data area.

For example:

message 1:

0000 0010 0123456890

Message 1 has sequence number 0 (0000), size 10 (0010), and 10 bytes of data: 01...90

message 2:

0001 0015 012345689012345

Message 2 has sequence number 1 (0001), size 15 (0015), and 15 bytes of data: 01...9012345

Etc..

The server response will contain only the last sequence number received and the next sequence number received.

Message stream:

Customer sends message 0000:

0000 0010 0123456890

Server responds to response 0000:

0000 0001

in this message the server informs the client that it has received the message with sequence number 0000, and expects to receive the next message with sequence number 0001.

Etc..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.