Professional Documents
Culture Documents
TCP/IP Sockets in C: Practical Guide For Programmers: Michael J. Donahoo Kenneth L. Calvert
TCP/IP Sockets in C: Practical Guide For Programmers: Michael J. Donahoo Kenneth L. Calvert
Computer Chat
Loss
Reordering
Duplication
Delay
Host-to-host delivery
(not application-to-application)
IP Address
32-bit identifier
Dotted-quad: 192.118.56.25
www.mkp.com -> 167.208.101.28
192.18.22.13
209.134.16.123
Transport Protocols
Best-effort not sufficient!
Data checksum
Best-effort
Data checksum
Reliable byte-stream delivery
Flow and congestion control
Ports
Identifying the ultimate destination
WWW
Port
80
Telnet
25
23
192.18.22.13
Socket
How does one speak TCP/IP?
Sockets
TCP/IP Sockets
TCP
PF_INET
UDP
Type
Protocol
SOCK_STREAM
IPPROTO_TCP
SOCK_DGRAM
IPPROTO_UDP
Socket reference
Generic
IP Specific
struct sockaddr
{
unsigned short sa_family; /* Address family (e.g., AF_INET) */
char sa_data[14];
/* Protocol-specific address information */
};
struct sockaddr_in
{
unsigned short sin_family;/* Internet protocol (AF_INET) */
unsigned short sin_port; /* Port (16-bits) */
struct in_addr sin_addr; /* Internet address (32-bits) */
char sin_zero[8];
/* Not used */
};
struct in_addr
{
unsigned long s_addr;
/* Internet address (32-bits) */
};
sockaddr
sockaddr_in
Family
Blob
2 bytes
2 bytes
4 bytes
Family
Port
Internet address
8 bytes
Not used
Server: Jane
Hi. Im Bob.
Hi, Bob. Im Jane
Nice to meet you, Jane.
1.
2.
3.
4.
Client
Create a TCP socket
Establish connection
Communicate
Close the connection
1.
2.
3.
4.
Server
Create a TCP socket
Assign a port to socket
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
1.
2.
3.
4.
Client
Create a TCP socket
Establish connection
Communicate
Close the connection
1.
2.
3.
4.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
1.
2.
3.
4.
Client
Create a TCP socket
Establish connection
Communicate
Close the connection
1.
2.
3.
4.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
1.
2.
3.
4.
Client
Create a TCP socket
Establish connection
Communicate
Close the connection
1.
2.
3.
4.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
1.
2.
3.
4.
Client
Create a TCP socket
Establish connection
Communicate
Close the connection
1.
2.
3.
4.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
Client
Create a TCP socket
Establish connection
Communicate
Close the connection
1.
2.
3.
4.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
1.
2.
3.
4.
Client
Create a TCP socket
Establish connection
Communicate
Close the connection
1.
2.
3.
4.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
1.
2.
3.
4.
Client
Create a TCP socket
Establish connection
Communicate
Close the connection
1.
2.
3.
4.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
1.
2.
3.
4.
Client
Create a TCP socket
Establish connection
Communicate
Close the connection
1.
2.
3.
4.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
1.
2.
3.
4.
Client
Create a TCP socket
Establish connection
Communicate
Close the connection
1.
2.
3.
4.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
1.
2.
3.
4.
Client
Create a TCP socket
Establish connection
Communicate
Close the connection
1.
2.
3.
4.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
3.
Client
Create a TCP socket
Establish connection
Communicate
4.
1.
2.
close(clntSocket)
1.
2.
3.
4.
Server
Create a TCP socket
Bind socket to a port
Set socket to listen
Repeatedly:
a.
Accept new connection
b.
Communicate
c.
Close the connection
TCP Tidbits
Client Server
send(Hello Bob)
Closing a Connection
Echo Client
Echo Server
send(string)
while (not received entire string)
recv(buffer)
print(buffer)
recv(buffer)
while(client has not closed connection)
send(buffer)
recv(buffer)
close(socket)
close(client socket)
Constructing Messages
beyond simple strings
Application
byte stream
byte stream
TCP/IP
TCP/IP
Ill pass
these to
the app. It
knows
what to do.
Application Protocol
Primitive Types
String
77
111
109
77
111
109
10
\n
Primitive Types
Integer
49
55
57
57
56
55
48
10
\n
Advantage:
Disadvantage:
1.
2.
1.
2.
Human readable
Arbitrary size
Inefficient
Arithmetic manipulation
Primitive Types
Integer
Native representation
Little-Endian
92
246
23,798
Big-Endian
246
92
4-byte
twos-complement
integer
Message Composition
Fixed-length fields
integer
short
short
Variable-length fields
i
\n
struct tst {
short x;
int y;
short z;
};
[pad]
[pad]