* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Socket Programming (Continued)
Survey
Document related concepts
Transcript
Socket Programming (Cont.)
Networking
CS 3470, Section 1
Sarah Diesburg
Control Flow
socket()
bind()
TCP Client
Socket()
connect() Connection establishment
send()
TCP Server
Well-known port
listen()
accept()
blocks until connection from client
recv()
process request
recv()
close()
send()
read()
close()
Byte Ordering
Increasing memory address
Address A+1
Little-endian
byte order
High-order byte
MSB
Big-endian
byte order
Address A
Low-order byte
16-bit value
Low-order byte
Address A+1
LSB
High-order byte
Address A
Increasing memory address
Implications of Byte Order
Unfortunately there is no standard between
these two byte orderings and we encounter
systems that use both formats
We refer to the byte ordering used by a given
system as host byte order
The sender and the receiver must agree on the
order in which the bytes of these multi-byte field
transmitted: specify network byte order, which
is big-endian byte ordering
Byte Order Functions
#include <netinet.h>
/* Host to network */
uint16_t htons(uint16_t host16bitvalue)
Converts a 16-bit integer from host to network byte order
uint32_t htonl(uint32_t host32bitvalue)
Converts a 32-bit integer from host to network byte order
Both return: value in network byte order
/* Network to host */
uint16_t ntohs(uint16_t net16bitvalue)
uint32_t ntohl(uint32_t net32bitvalue)
Both return: value in host byte order
When do we use hton/ntoh
functions?
Use hton the port number in struct
sockaddr_in
If we create a custom struct to hold our
headers and data
Sending our data through send() and recv()
functions
E.g., if our first struct member is a 2-byte header,
and sender/receiver have different memory
orderings, number would look very different to
each machine
6
Socket Address Structures
#include <netinet/in.h>
// Pointers to socket address structures are often cast to pointers
// to this type before use in various functions and system calls:
struct sockaddr {
unsigned short
char
};
sa_family;
sa_data[14];
// address family, AF_xxx
// 14 bytes of protocol address
// IPv4 AF_INET sockets:
struct sockaddr_in {
short
sin_family;
unsigned short
sin_port;
struct in_addr
sin_addr;
char
sin_zero[8];
};
//
//
//
//
struct in_addr {
uint32_t s_addr;
};
// load with inet_pton()
e.g. AF_INET, AF_INET6
e.g. htons(3490)
see struct in_addr, below
pad to fit into sockaddr
Address Conversion Functions
#include <arpa/inet.h>
int inet_pton(int af, const char *src, void *dst); /*
Returns 1 on success, < 1 on error */
Converts the character string src into a network address
structure, then copies the network address structure to
dst.
int inet_aton(const char *strptr, struct in_addr
*addrptr); /* return 1 if string was valid,0 error */
Convert an IP address in string format (x.x.x.x) to the
32-bit packed binary format used in low-level network
functions
Address Conversion Functions
#include <arpa/inet.h>
in_addr_t inet_addr(const char *strptr);
/* return 32-bit binary network byte ordered IPv4
address; INADDR_NONE if error, deprecated and replaced
by inet_aton() */
char *inet_ntoa(struct in_addr inaddr);
/* returns: pointer to dotted-decimal string */
Example
struct sockaddr_in ip4addr;
int s;
ip4addr.sin_family = AF_INET;
ip4addr.sin_port = htons(3490);
inet_pton(AF_INET, "10.0.0.1", &ip4addr.sin_addr);
s = socket(PF_INET, SOCK_STREAM, 0);
bind(s, (struct sockaddr*)&ip4addr, sizeof ip4addr);
Man Pages
Use man pages to look up useful information
$> man cat
$> man bind
Get information about shell commands
Get information about C library/system calls
Also tells you which header files to include
$> man man
Get information about man pages
Man Pages
Sometimes you need to specify a man
section
E.g., printf is both a shell command and a C
library call
Use either
$>man 1 printf
$>man 3 printf
See
http://www.cs.uni.edu/~diesburg/courses/cs3470_
fa13/resources/man_page_levels.htm
C Header Files
Located at /usr/include
Can find out what functions are available for
you to use and struct definitions
(Hint: check out <string.h> and <strings.h>)
13
Byte Manipulation Functions
#include <strings.h>
/* Berkeley-derived functions */
void bzero(void *dest, size_t nbytes)
Set the first part of an object to null bytes
void bcopy(const void *src, void *dest, size_t nbytes);
int bcmp(const void *ptr1, const void *ptr2, size_t nbytes)
/* return:0 if equal, nonzero if unequal */
#include <string.h>
/* ANSI C defined functions */
void *memset(void *dest,int c,size_t len)
Sets the first len bytes in memory dest to the value of c
void *memcpy(void *dest,const void *src, size_t nbytes)
void memcmp(const void *ptr1, const void *ptr2, size_t nbytes)
Demo of Program
15