TCP Socket Programming Concepts
TCP Socket Programming Concepts
1
What is a socket?
• An abstract interface provided to the application
programmer
– File descriptor, allows apps to read/write to the network
• Allows to processes on remotely connected computers to
talk to each other
2
Three types of sockets
• SOCK_STREAM SOCK_DGRAM
– TCP
– connection oriented,
UDP
bidirectional no connection
– reliable, in-order delivery
unreliable delivery, no
guarantee on the
SOCK_RAW order
can send/receive
UNIX Domain
they're used for interposes
communication
can send/receive
3
Socket-programming using TCP
Socket: a door between application process and end-
end-transport protocol (UDP or TCP)
TCP service: reliable transfer of bytes from one
process to another
controlled by
controlled by process application
application process
developer
developer socket socket
controlled by TCP with TCP with controlled by
buffers, operating
operating buffers, internet system
system variables variables
host or host or
server server
4
Socket programming with TCP
Client must contact server • When contacted by client,
• server process must first be server TCP creates new
running socket for server process to
• server must have created communicate with client
socket (door) that welcomes – allows server to talk with
client’s contact multiple clients
– source port numbers used
Client contacts server by:
to distinguish clients
• creating client-local TCP
socket
• specifying IP address, port
number of server process application viewpoint
• When client creates socket:
client TCP establishes TCP provides reliable, in-order
connection to server TCP transfer of bytes (“pipe”)
between client and server
5
Ports
• Used to address processes on a host
– 0-1024 is usually reserved for known service
FTP Web
Server Server
21 80
Transport
Layer
Network
Layer
DLL/Physical
6
Socket Programming - Flow
socket() socket()
bind()
connect()
listen()
Client Server
send()
accept()
wait for
recv() connection
.. recv() request from next
.
client
send()
close() close()
7
Socket call
• Means by which an application attached to the network
• int socket(int family, int type, int protocol)
• Family: address family (protocol family)
– AF_UNIX, AF_INET, AF_NS, AF_IMPLINK
• Type: semantics of communication
– SOCK_STREAM, SOCK_DGRAM, SOCK_RAW
– Not all combinations of family and type are valid
• Protocol: Usually set to 0 but can be set to specific value.
– Family and type usually imply the protocol
• Return value is a handle for new socket
8
Bind call
9
Listen call
• Used by connection-oriented servers to indicate an
application is willing to receive connections
• Int(int socket, int backlog)
• Socket: handle of newly creates socket
• Backlog: number of connection requests that can be queued
by the system while waiting for server to execute accept call.
CS 640 10
Accept call
11
Connect call
12
Send(to), Recv(from)
• After connection has been made, application uses send/recv
to data
• Int send(int socket, char *message, int msg_len, int
flags)
– Send specified message using specified socket
• Int recv(int scoket, char *buffer, int buf_len, int flags)
– Receive message from specified socket into specified buffer
13
Socket Implimentation
• Protocol implementation
– Process per protocol
• Use a separate process to implement each protocol
• Messages are passes between processes
– Process per message
• Use one process to handle each message/communication
• Generally more efficient
• Buffer use
– Applications use buffers as do protocols
• Copies are VERY expensive
• Message abstraction enables pointers to be used and minimal copies
14
Practical issues – using sockets
• You have to be very careful when using these calls
– Specific data structures and formats
– Ports cannot be less than 1024
• You can use other tools to see if things are working
– Tcpdump
– /proc
– netstat
• Client and server can be on same system
• Think about error handling methods
• Refer to Stevens
• Baby steps!!
15
Address Structure
• Declare address structure
– struct sockaddr_in sockAdd;
• Set family
– sockAdd.sin_family = AF_INET;
• Set IP address (2 ways)
//specify address to listen to
– inet_pton(AF_INET, “127.0.0.1”, &sockAdd.sin_addr.s_addr)
//listen to any local address
– sockAdd.sin_addr.s_addr = htonl(INADDR_ANY)
• Set port
– sockAdd.sin_port = htons(9999);
16
Stream jargon
keyboard monitor
• A stream is a sequence of
characters that flow into or out
of a process.
inFromUser
input
stream
• An input stream is attached to Client
some input source for the Process
process
process, e.g., keyboard or
socket.
• An output stream is attached
to an output source, e.g.,
inFromServer
outToServer
output input
monitor or socket. stream stream
client
clientSocket
TCP
socket TCP
socket
17
Demo Ends Here
Questions?
18