SOCKETS IN C - TCP Sequence Number

0

I need a help with sockets in C. My teacher spent a job in which I must execute the TCP communication between client and server where I should execute some rules ..

  • 1 - The Client sends a message to the server. The message must contain the sequence number and the number of bytes loaded by the message.
  • 2 - The server should receive the message and send an acknowledgment to the client. The acknowledgment message must contain the number of sequence of the acknowledgment message, the sequence number of the next segment to be sent by the client.

Can anyone explain me better how the sequence number works and if possible help me codify this? The part of TCP communication between client / server is attached and ready.

Client code snippet:

 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] != '
//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");
}
';i++) { if(server_reply[i] != ' ') vl += (int)server_reply[i]; } printf("%d\n",vl); } close(sock);

Server code snippet:

 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] != '
//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");
}
';i++) { if(server_reply[i] != ' ') vl += (int)server_reply[i]; } printf("%d\n",vl); } close(sock);
    
asked by anonymous 20.06.2017 / 00:38

1 answer

0

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

For example:

message 1:

0000 0010 0123456890

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

message 2:

0001 0015 012345689012345

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

Etc.

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

Message Flow:

Client 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.

    
20.06.2017 / 13:11