程序代写代做代考 c/c++ UDP Example

UDP Example

TCP Example
Computer Networking – Spring 2019

TCP Intro
TCP (transport control protocol)
Most complex transport protocol
Reliable delivery
In-order delivery
Flow control
Congestion control
TCP is the default for most applications
Web
Email
File transfer
Remote login
Etc.

Programming TCP
We will make two programs
Server – will await connections from clients
Client – will make connection to server
Command line options
Server will take in IP address and port to listen on (await connections)
Client will take in IP address and port to connect to, as well as data to send
Operation
Client will attempt to send data to server on IP/port
Server will receive data from client
Server will send this data back to the client
A so-called “echo server”

Programming TCP
Server C/C++ networking function calls
socket – create a new network socket on the server to receive data from clients (a socket is just a file descriptor, like an open file, or stdout)
bind – associates the socket file descriptor with a network address (IP address and port)
listen – tell OS to start allowing clients to connect on bound address (includes ”backlog”, number of outstanding connections to allow)
accept – causes server to accept a new incoming connection; this completes the three-way handshake (also returns a NEW socket to be used for this new connection)
recv – receives data (if present) on the socket (used by client and server)
send – sends data to a connected socket (used by client and server)

Programming TCP
Client C/C++ networking function calls
socket – create a new network socket on the server to send data to server
connect – connect to the server
recv – receives data (if present) on the socket (used by client and server)
send – sends data to a connected socket (used by client and server)

NOTE: client does not need to bind to a specific address, why is that?

Programming TCP
Client/server network utility library functions
inet_pton – convert a network address string (e.g., 192.168.0.100) into binary format suitable for using for network operations
htons – convert a 16-bit (short) number from host byte order into network byte order
ntohs – convert a 16-bit (short) number from NBO to HBO
What do ntohl/htonl do?
gethostbyname – performs host to IP address lookup so we can use hostnames instead of IP addresses
What network protocol already discussed might gethostbyname leverage?
fcntl – set socket options such as non-blocking

Programming TCP
Network structures
sockaddr_in – IPv4 network address (IP and port)
in_addr – IPv4 IP address (part of sockaddr_in)
in_port_t – port in NBO (16-bit, part of sockaddr_in)
sa_family_t – type of network address (part of sockaddr_in, AF_INET for IPv4 address)
sockaddr – generic socket address type; often you will cast a sockaddr_in to sockaddr to be compatible with network function calls
These are the same as we used for UDP (IPv6 is coming though!)

Programming TCP
Network Types
AF_INET – type used for sockaddr_in family, indicates IPv4, also used for the domain argument to the socket function call
SOCK_STREAM – socket type argument, indicates the TCP protocol
What would we use for UDP?
Error handling
Most network related functions return either 0 or a number greater than 1 to indicate success, and -1 to indicate error.
You should always check the return value!
If -1 is returned, you should handle the error somehow!!!

Programming TCP – Server
Create a socket, indicating an IPv4 address and UDP
tcp_socket = socket(AF_INET, SOCK_STREAM, 0);

Declare and initialize server address
struct sockaddr_in recv_addr;
uint16_t port;
ip_string = “192.168.0.100”;
port = 9000;
ret = inet_pton(AF_INET, ip_string, (void *)&recv_addr.sin_addr);
recv_addr.sin_family = AF_INET;
recv_addr.sin_port = htons(port);

Programming TCP – Server
Declare and initialize server address (2)
host_string = “localhost”;
port_string = “9000”;
struct addrinfo hints;
struct addrinfo *results;
struct addrinfo *results_it;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_protocol = 0;
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
ret = getaddrinfo(host_string, port_string, &hints, &results);
results_it = results;
ret = -1;
while (results_it != NULL) {
std::cout << "Trying to bind to something?" << std::endl; ret = bind(tcp_socket, results_it->ai_addr, results_it->ai_addrlen);
if (ret == 0) // Success {
break;
}
perror(“bind”);
results_it = results_it->ai_next;
}

// Whatever happened, we need to free the address list.
freeaddrinfo(results);

Programming TCP – Simple Server
Declare buffer, receive data from client (blocking call)

int incoming_socket;
char recv_buf[2048];
struct sockaddr_in client_address
socklen_t client_address_length = sizeof(struct sockaddr_in);
sizeof(struct sockaddr_in);
ret = listen(tcp_socket, 10);
incoming_socket = accept(tcp_socket, (struct sockaddr *)&client_address, &client_address_length);
ret = recv(incoming_socket, recv_buf, 2047, 0);

Programming TCP – Server
Echo data back to client

ret = send(incoming_socket, recv_buf, received_data_length, 0);

Let’s look at the full code, these are just the basics…

Programming TCP – Client
Create a socket, indicating an IPv4 address and UDP
server_socket = socket(AF_INET, SOCK_STREAM, 0);

Declare and initialize server address
struct sockaddr_in server_addr;
uint16_t port;
ip_string = “192.168.0.100”;
port = 9000;
ret = inet_pton(AF_INET, ip_string, (void *)&server_addr.sin_addr);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
Note: better to use getaddrinfo (as in server example)

Programming TCP – Client
Connect to the server
server_addr_length = sizeof(struct sockaddr_in);
ret = connect(server_socket, &server_addr, server_addr_length);
Send some data to the server
char *msg = “test_message”;
ret = send(server_socket, msg_buf, strlen(msg), 0);

Receive reply from server
char recv_buf[2048];
ret = recv(server_socket, recv_buf, 2047, 0);

/docProps/thumbnail.jpeg