Socket Programming in C
Socket Programming in C
Socket Programming in C
Socket programming
Goal: learn how to build client/server application that
communicate using sockets
Socket API socket
• introduced in BSD4.1 UNIX, a host-local,
1981 application-created,
OS-controlled interface
• explicitly created, used, released (a “door”) into which
by apps application process can
• client/server paradigm both send and
receive messages to/from
• two types of transport service another application
via socket API: process
– unreliable datagram
– reliable, byte stream-oriented
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
Socket programming with TCP
Client must contact server
• server process must first be • When contacted by client,
running server TCP creates new
• server must have created socket socket for server process to
(door) that welcomes client’s communicate with client
contact – allows server to talk
with multiple clients
Client contacts server by:
– source port numbers
• creating client-local TCP
used to distinguish
socket clients (more in Chap 3)
• specifying IP address, port application viewpoint
number of server process
TCP provides reliable, in-order
• When client creates socket: transfer of bytes (“pipe”)
client TCP establishes between client and server
connection to server TCP
Stream jargon
• 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, eg,
keyboard or socket.
• An output stream is
attached to an output
source, eg, monitor or
socket.
Socket programming with TCP
keyboard monitor
inFromUser
input
standard input stream
Client
(inFromUser stream) , Process
process
sends to server via socket
(outToServer stream)
2) server reads line from socket
inFromServer
3) server converts line to
outToServer
output input
stream stream
(inFromServer stream)
Client/server socket interaction: TCP
Server (running on hostid, port x) Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
Example: C client (TCP)
/* client.c */
void main(int argc, char *argv[])
{
struct sockaddr_in sad; /* structure to hold an IP address */
int clientSocket; /* socket descriptor */
struct hostent *ptrh; /* pointer to a host table entry */
Close close(clientSocket);
connection
}
Example: C server (TCP)
/* server.c */
void main(int argc, char *argv[])
{
struct sockaddr_in sad; /* structure to hold an IP address */
struct sockaddr_in cad;
int welcomeSocket, connectionSocket; /* socket descriptor */
struct hostent *ptrh; /* pointer to a host table entry */
close(connectionSocket);
} Write out the result to socket
}
End of while loop,
loop back and wait for
another client connection
Socket programming with UDP
UDP: no “connection”
between client and server
• no handshaking
application viewpoint
• sender explicitly attaches
UDP provides unreliable transfer
IP address and port of
of groups of bytes (“datagrams”)
destination to each packet between client and server
• server must extract IP
address, port of sender
from received packet
UDP: transmitted data may
be received out of order,
or lost
Client/server socket interaction: UDP
Server (running on hostid, port x) Client
create socket,
port=x, for create socket,
clientSocket =
incoming request: DatagramSocket()
serverSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
using clientSocket
read request from
serverSocket
write reply to
serverSocket
specifying client read reply from
host address, clientSocket
port number close
clientSocket
Example: Java client (UDP)
keyboard monitor
inFromUser
input
stream
Client
Process
Input: receives
process
packet (TCP
Output: sends received “byte
packet (TCP sent stream”)
receivePacket
sendPacket
“byte stream”) UDP UDP
packet packet
client
clientSocket UDP
socket UDP
socket
char Sentence[128];
char modifiedSentence[128]; Create client socket,
NO connection to server
host = argv[1]; port = atoi(argv[2]);
Close close(clientSocket);
connection }
Example: C server (UDP)
/* server.c */
void main(int argc, char *argv[])
{
struct sockaddr_in sad; /* structure to hold an IP address */
struct sockaddr_in cad;
int serverSocket; /* socket descriptor */
struct hostent *ptrh; /* pointer to a host table entry */
close(connectionSocket);
} Write out the result to socket
}
End of while loop,
loop back and wait for
another client connection