Junior — Middle
Have you had experience working with the TCP protocol in your projects?
sobes.tech AI
Answer from AI
Yes, experience with the TCP protocol is usually associated with creating network applications where establishing a reliable connection between the client and server is important.
TCP (Transmission Control Protocol) provides guaranteed data delivery in the correct order. In C/C++ projects, sockets are often used to work with TCP:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
int main() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
connect(sock, (sockaddr*)&addr, sizeof(addr));
const char* msg = "Hello TCP Server";
send(sock, msg, strlen(msg), 0);
close(sock);
return 0;
}
This code establishes a TCP connection with a server and sends data.