程序代写 Computer Networking and Applications – cscodehelp代写

Computer Networking and Applications
How processes communicate
• Socketsprovidetheapplica.onprogrammers’interface(API) between a process and the transport layer (sys/socket.h, java.net).
• User applica.on code runs on end-systems – not network core

Copyright By cscodehelp代写 加微信 cscodehelp

• The applica.on programmer needs to specify
– which transport protocol to use
– what host to send messages to (e.g. IP address or hostname)
– what process on the des.na.on host to send messages to (port number)
application
controlled by app developer
controlled by OS
©Kurose & Ross 2013
application

Computer Networking and Applications
Internet Transport Services • What services do applica.ons need?
– Reliable data transfer, Minimum throughput guarantees, Bounded delays, Security
• What do the Internet protocols provide?
– Reliable data transfer with transmission control protocol TCP
– Minimal overhead, available bandwidth/delays, no delivery guarantee with user datagram protocol UDP
– emerging protocols for providing .ming and bandwidth guarantees
• Current choices in Internet are TCP or UDP. How does a network applica.on designer decide?

Computer Networking and Applications
Transport service requirements: common apps
application
file transfer e-mail Web documents real-time audio/video
stored audio/video interactive games text messaging
no loss no loss no loss loss-tolerant
loss-tolerant loss-tolerant no loss
throughput
audio: 5kbps-1Mbps video:10kbps-5Mbps
same as above few kbps up
time sensitive
yes, 100’s msec
yes, few secs yes, 100’s msec yes and no

Computer Networking and Applications
Socket Programming with TCP
• Recall that TCP provides a reliable byte stream. All of our data will
be going to the same host and port (ie to the same process).
• Assume we want to get a web page. We want to talk to www.foo.com on port 80. If we stay connected to the socket on port 80, how will www.foo.com service other requests?
• port 80 is used to establish a connec.on on a second server socket. connect
client socket port nnn
accept bytes
TCP port 80 socket
new socket port 80

Computer Networking and Applications
Socket programming with TCP
Application Example:
1. Client reads a character (data) from its keyboard and sends the data to the server.
2. The server receives the data and converts character to uppercase.
3. The server sends the modified data to the client.
4. The client receives the modified data and displays the character on its screen.
©Kurose & Ross 2001

Computer Networking and Applications
server (running on hostid)
Client/server socket interaction: TCP
create socket, port=x, for incoming
listen_socket = socket() bind( )
wait for incoming connection request
listen() connection_socket = accept()
read request from
connection_socket write reply to
connection_socket
connection_socket
TCP connection setup
create socket, connect to hostid, port=x
client_socket = socket() connect( )
send request using
client_socket
read reply from
client_socket
client_socket
Application Layer 2-6

Computer Networking and Applications
Libraries we will use
C TCP Client
#include // socket, recv, send, close #include
#include // getaddrinfo
#include // close
#include // memset #include // fgets, fputs, puts #include // true false
int main(int argc, char *argv[]) {
char * SERVER_NAME = “localhost”; char * SERVER_PORT = “6789”;
Set these to the hostname and port of
the SERVER
“localhost” works if the server is running on the same computer as the client. Effectively “this computer”
The port must be the port the server is listening on.
You need to set these for *your* application.
Application Layer 2-7

Computer Networking and Applications
C TCP Client
/* Set socket address structures */
struct addrinfo hints; // hints for the type of socket wanted
struct addrinfo * server_addr; // holder for the address information
/* clear memory of the structures */ memset(&hints,0,sizeof(hints)); memset(&server_addr,0,sizeof(server_addr));
/* set hints on type of connec.on */
hints.ai_family = AF_INET; /* set address family to IPv4 */ hints.ai_socktype = SOCK_STREAM; /* use TCP stream */
The address information is now stored in server_addr. We will use this information to create and connect the socket
getaddrinfo(SERVER_NAME, SERVER_PORT, &hints, &server_addr);

Computer Networking and Applications
C TCP Client
/* Create socket */
int client_socket = socket(server_addr->ai_family, server_addr->ai_socktype, 0);
/* connect socket */
if (connect(client_socket, server_addr->ai_addr, server_addr->ai_addrlen))
perror(“connect failed: “);
char character; while(true) {
puts(“Input lowercase letter: “);
character = getchar(); send(client_socket,&character,sizeof(char),0);
Note we can use perror( ) to print out the reason for failure. This should be done for all function calls that might fail.
Loop forever getting a character from the user, sending it to the server on the connected socket…

Computer Networking and Applications
C TCP Client
puts(“Server returned: “);
recv(client_socket, &character, sizeof(char),0); putchar(character);
getchar(); // read the newline character putchar(‘
’);
… still in the loop. Read what character the server sends back on the connected socket and print it.
} close(client_socket);
freeaddrinfo(server_addr); }
We won’t ever reach this code since we have an infinite loop above. But if we were to finish we should close the connected socket so the server knows we are finished and free dynamic memory.
Both will happen automatically (by the operating system) if we kill the client process.

Computer Networking and Applications
C TCP Server
#include // socket, recv, send, close bind #include
#include // getaddrinfo
#include // close
#include // memset #include // toupper #include // NULL
int main(int argc, char *argv[]) {
struct addrinfo hints; // fill this in with the type of address wanted struct addrinfo * server_addr; // structure to hold server’s address int listen_socket, connection_socket;
char * DEFAULT_PORT = “6789”;
int QLEN = 1;
Libraries we will use.
Note similarities and differences from client
This is the port the server will listen on and the client will connect to. If it doesn’t match, the client will not reach the server application.
QLEN is the number of simultaneous requests. We will only connect to one client at a time.
Application Layer 2-11

Computer Networking and Applications
C TCP Server
/* Set up server address structure: serv_addr */
/* This sets the port and and IP address that we will bind to */ /* the socket we just created */
Address set up is similar to the client address set up
memset(&hints, 0, sizeof(hints)); memset(&server_addr,0,sizeof(server_addr)); /* clear sockaddr structure */
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // fill in my IP address
getaddrinfo(NULL, DEFAULT_PORT, &hints, &server_addr);
But notice this difference. This hint tells getaddrinfo to fill in my (the servers) address for me.
I don’t have to give the server name like I did in the client.
Application Layer 2-12

Computer Networking and Applications
C TCP Server
/* Create a socket */
listen_socket = socket(server_addr->ai_family, server_addr->ai_socktype, 0);
/* Bind the socket to the address information set in serv_addr */ bind(listen_socket, server_addr->ai_addr, server_addr->ai_addrlen);
/* Start listening for connections */ listen(listen_socket, QLEN);
/* Accept and handle requests */
connection_socket = accept(listen_socket, NULL, NULL);
Different here! The client just connected. The server must bind, listen and accept.
We have two sockets now. connection_socket and listen_socket. Which will we talk to the client on?
Application Layer 2-13

Computer Networking and Applications
while(recv(connection_socket, &character, sizeof(char),0)) { /* send capitalised character back */
capital_character = (char) toupper((int)character); send(connection_socket, &capital_character, sizeof(char),0);
/* finished, close the socket */ close(connection_socket); close (listen_socket);
C TCP Server
/* Receive the characters */ char character;
char capital_character;
Receive returns the number of characters read and will receive 0 when the client closes the socket.
Send it back to the client
How would this differ if we wanted to keep running the server and accept another client connection?
Application Layer 2-14

Computer Networking and Applications
UDP: no “connec.on” between client and server
• no handshaking
• sender explicitly a_aches IP address
and port of des.na.on
• server must extract IP address, port of sender from received datagram
UDP: transmi_ed data may be received out of order, or lost
application viewpoint
UDP provides unreliable transfer of groups of bytes (“datagrams”)
between client and server
Socket programming with UDP
©Kurose & Ross 2001

Computer Networking and Applications
server (running on serverIP) create socket, port= x:
Client/server socket interaction: UDP
create socket:
clientSocket = socket(AF_INET,SOCK_DGRAM)
Create datagram with server IP and
serverSocket = socket(AF_INET,SOCK_DGRAM)
port=x; send datagram via read datagram recvfrom() clientSocket
serverSocket
write reply sendto()
serverSocket
specifying client address, port number
read datagram from
clientSocket
clientSocket
Application 2-16

程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com

Leave a Reply

Your email address will not be published. Required fields are marked *