TCP Socket Programming
TCP Socket Programming
TCP Socket Programming
Kernel Support
Hardware
Ports
Used to address processes on a host 0-1024 is usually reserved for known service
What is a socket?
An abstract interface provided to the
application programmer
Socket
A socket is a communication path to a port.
Sockets
Socket
are
sort
of
the
programming
interface from the programmer's side and the communications endpoint from the networking
side.
In the Internet case, a socket would have an IP
address and a port number. IP address and a port represent a unique reception point or transmission point in an operating system for it
SOCK_DGRAM
UDP
no connection
unreliable delivery, no guarantee on the order can send/receive
internet
host or server
host or server
9
server TCP creates new socket for server process to communicate with client allows server to talk with multiple clients source port numbers used to distinguish clients
application viewpoint
TCP provides reliable, in-order transfer of bytes (pipe) between client and server
10
Socket Programming in C
socket()
bind() listen()
connect()
Client
send() recv()
Server
wait for connection request from next client
accept() recv()
. . .
send()
close()
close()
12
socket()
int s_listen = socket(family, type, protocol);
family: AF_INET specifies Ipv4 type: SOCK_STREAM, SOCK_DGRAM protocol: 0 (pseudo, IP ). See /etc/protocols
13
bind()
bind(s_listen, localAdd, addLength) Server specifies which port and address it will be listening to s_listen: our listening socket descriptor localAdd: socket address structure addLength: length of localAdd
14
Address Structure
struct sockaddr_in { u_char sin_len; // length of address u_char sin_family; // family of address u_short sin_port; // protocol port num struct in_addr sin_addr; // IP Addr char sin_zero[8]; // set to zero, used for padding };
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)
sockAdd.sin_port = htons(9999);
16
names
(ports)
and
they
establish
themselves
with listen(). You need to perform binding regardless of socket family for server sockets.
For client sockets it depends. For AF_INET type of
17
listen()
Calling listen() on a socket causes the kernel's tcp/ip
implementation to begin accepting connections sent to the socket's bound name (port).
This will happen whether or not you ever call accept().
accept()
accept() simply gives your server a way to access and
s_new: new socket for communication with client s_listen: the listening socket clientAddress: struct sockaddr, address of client addLength: size of client address structure accept is blocking: waits for connection before returning
19
Talking
int send(int s_new, const void *buf, int len, int flags);
s_new socket descriptor buf pointer to buffer len size of buffer flags can be safely set to 0 int recv(int s_new, void *buf, int len, unsigned int flags); similar to send buf holds the data to be transferred
20
Concurrent Server
The key in understanding how the listening
socket continues to listen while the accepted connection is doing its thing is in understanding
21
kernel used these values to create various structures so that in collaboration with the tcp/ip stack all traffic with this tuple will go to the connected socket.
Even though your server may have a thousand
connections with local address 192.168.1.100 port 80, the client combination of address and port will always be different and thus the tuple is always unique.
22
processes
Execution for both child and parent process continues at the next instruction fork() returns
0 if this is the child process PID (>0) of the child process if this is the parent <0 if fork() fails
on another socket
23
24
Stream jargon
keyboard monitor
A stream is a sequence of
characters that flow into or out of a process. An input stream is attached to some input source for the process, e.g., keyboard or socket. An output stream is attached to an output source, e.g., monitor or socket.
input stream
inFromUser
output stream
inFromServer
outToServer
input stream
TCP socket
from network
26
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } }
28
Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket
29
End of while loop, loop back and wait for another client connection
30
Demo
References
Socket Programming, Dan Rubinstein, http://www1.cs.columbia.edu/~danr/courses/6761/Fall00/intro/6761-1bsockets.ppt 15-441 Socket Programming, www.cs.cmu.edu/afs/cs/academic/class/15441f01/www/lectures/lecture03.ppt Network Programming, Geoff Kuenning, www.cs.hmc.edu/~geoff/classes/hmc.cs105.200701/slides/class21_net2.ppt Socket Programming, Abhinav Jain, www.cs.purdue.edu/homes/jain8/cs422/pso3.ppt
32