Sockets C
Sockets C
transport transport
network network controlled
link by OS
link Internet
physical ©Kurose & Ross 2013
physical
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
• 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 TCP
socket port 80
port nnn accept socket
bytes
new
bytes 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.
write reply to
connection_socket read reply from
client_socket
Close
connection_socket Close
client_socket
C TCP Client
#include <sys/types.h> // socket, recv, send, close
#include <sys/socket.h>
#include <netdb.h> // getaddrinfo
Libraries we #include <unistd.h> // close
will use #include <string.h> // memset
#include <stdio.h> // fgets, fputs, puts
#include <stdbool.h> // true false “localhost” works if the
server is running on the
int main(int argc, char *argv[]) { same computer as the
client. Effectively “this
Set these to computer”
char * SERVER_NAME = "localhost";
the
char * SERVER_PORT = "6789"; The port must be the
hostname
and port of port the server is
the SERVER listening on.
/* 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: ");
Note we can use perror( ) to
print out the reason for
char character; failure. This should be done
for all function calls that
while(true) {
might fail.
puts("Input lowercase letter: ");
character = getchar(); Loop forever getting a
send(client_socket,&character,sizeof(char),0); character from the user,
sending it to the server
on the connected
socket…
Computer Networking and Applications
C TCP Client
}
We won’t ever reach this code since we
close(client_socket);
have an infinite loop above. But if we
were to finish we should close the
freeaddrinfo(server_addr); 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
memset(&hints, 0, sizeof(hints));
memset(&server_addr,0,sizeof(server_addr)); /* clear sockaddr structure */ But notice this
difference. This hint
hints.ai_family = AF_INET; tells getaddrinfo to fill in
hints.ai_socktype = SOCK_STREAM; my (the servers)
address for me.
hints.ai_flags = AI_PASSIVE; // fill in my IP address
/* Create a socket */
listen_socket = socket(server_addr->ai_family, server_addr->ai_socktype, 0);
Application 2-16