Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 12345
#define BUFFER_SIZE 1024
void go_back_n_protocol(int sf, int sn, int ack, int num_frames, int window_size, int *updated_sf, int *updated_sn, int *updated_ack, int *updated_window_range) {
*updated_sn = (sn + num_frames) % (1 << window_size);
*updated_ack = (ack + num_frames) % (1 << window_size);
*updated_sf = (sf + num_frames) % (1 << window_size);
for (int i = 0; i < window_size; i++) {
updated_window_range[i] = (*updated_sf + i) % (1 << window_size);
}
}
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Attaching socket to the port
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Binding the socket to the port
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d...n", PORT);
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// Read data from the client
read(new_socket, buffer, BUFFER_SIZE);
int sf, sn, ack, num_frames, window_size;
sscanf(buffer, "%d,%d,%d,%d,%d", &sf, &sn, &ack, &num_frames, &window_size);
int updated_sf, updated_sn, updated_ack;
int updated_window_range[window_size];
// Process the data
go_back_n_protocol(sf, sn, ack, num_frames, window_size, &updated_sf, &updated_sn, &updated_ack, updated_window_range);
// Prepare the response
char response[BUFFER_SIZE];
snprintf(response, sizeof(response), "Window Size: %d, Updated SF: %d, Updated SN: %d, Updated Window Range: [", window_size, updated_sf, updated_sn);
for (int i = 0; i < window_size; i++) {
char temp[10];
snprintf(temp, sizeof(temp), "%d", updated_window_range[i]);
strcat(response, temp);
if (i < window_size - 1) {
strcat(response, ", ");
}
}
strcat(response, "]");
// Send response to the client
send(new_socket, response, strlen(response), 0);
// Close sockets
close(new_socket);
close(server_fd);
return 0;
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team