Basics of Networking

Download Report

Transcript Basics of Networking

Networking Tutorial
Special Interest Group for Software Engineering
Luke Rajlich
What will be covered
●
●
●
●
Basics of networking
Simple network client
Simple network server
Transform / Echo server
Basics of Networking
●
What is a socket?
–
–
●
Input / Output port used for transmitting data over a
network
In Unix, a socket is actually a file descriptor! (called a
special file) It's one of those virtualization things
How do I refer or address a specific socket?
–
–
–
A socket can be addressed by an IP address and port.
IP address is a way to find a computer on a network.
Port is a way by which the operating system directs
traffic to specific applications. (EX: Port 80 is used for
HTTP requests.)
Types of Sockets
●
Datagram Socket
–
–
–
●
No connection, simply build a packet and send it to the
destination.
No guarantee of completion, receipt, or message accuracy
Used in UDP protocol
Stream Socket
–
–
–
–
A connection oriented socket. Connection is established
Messages are acknowledged when received. Acknowledge
bytes received to ensure the correctness of message
In actuality, built on top of datagram socket with extra
messages to maintain connection state
Used in TCP protocol
Basics of Networking, Cont.
●
“Network Order”
–
–
–
–
●
You want to send data over a socket, you must put into
serial format, that is, the data must be a continuous
chunk of bits.
There is a specific order of bits as agreed upon on the
internet is known as network order.
Operating system may have different order of bits for
certain data types....
'htonl', 'htons', 'ntohl', 'ntohs' functions
Basic Communication Model
A Simple Socket Client
●
●
Our goal: An application that opens a socket and
downloads google's webpage
Steps
–
–
–
–
–
–
–
Prerequisites to make it compile
Create a socket
Lookup www.google.com IP address via DNS
Create a struct containing the info to connect to Google
Connect to google's HTTP port (80)
Send HTTP GET request
Recv a set of bytes
Create A Socket
●
int socket(int domain, int type, int protocol);
–
–
–
–
Domain:
Type: SOCK_STREAM or SOCK_DGRAM for
stream and datagram sockets, respectively
Protocol: use 0 to have socket automatically pick
correct one
Return: an integer called the socket file descriptor. aka
“sockfd” in future parts of tutorial.
Lookup Google via DNS
●
struct hostent *gethostbyname(const char *name);
–
●
Lookup Call, returns a struct of type hostent containing
the information about the host looked up
hostent is a struct containing host entity
information.
–
Members
●
h_name
Create a struct with Google's
address
–
sin_family: AF_INET (for our purposes)
●
–
sin_port: port number in network order
●
–
–
googleaddr.sin_family = AF_INET;
googleaddr.sin_port = htonl(80);
sin_addr:
sin_zero: must be set to all zero's
●
memset(&(googleaddr.sin_zero), '\0', 8);
Connect to Google
●
int connect(int sockfd, struct sockaddr *serv_addr,
int addrlen);
–
–
–
Sockfd: Socket file decriptor
serv_addr: constructed in previous slide
Addrlen: length of the struct serv_addr
●
–
sizeof(struct sockaddr)
Return: -1 if error.
Make HTTP GET Request
●
●
Send a message using the connected socket
int send(int sockfd, const void *msg, int len, int
flags);
–
–
Sockfd: socket file descriptor to use to send message
Msg: a pointer to a stream of bytes of a message.
●
–
–
Specific byte sequence for HTTP GET: ??
Len: the length of a message in number of bytes
Flags: 0. For our purposes.
Receive response from Google
●
int recv (int sockfd, void *buf, int len, unsigned int
flags);
–
–
–
–
–
sockfd: Socket file descriptor
Buf: pointer to a buffer
Len: maximum length of the buffer
Flags: 0. for our purposes.
Return: number of bytes read
●
-1: error
Extra Special Considerations
●
Serialization
–
–
You want to send data over a socket, you must put into
serial format, that is, the data must be a continuous
chunk of bytes.
Must thing about how to convert data structures into a
serial format