Java Socket Programming
Java Socket Programming
• Socket pair
– Specified the two end points that uniquely identifies each TCP
connection in an internet.
– 4-tuple: (client IP address, client port number, server IP address,
server port number)
Sockets for server and client
• Server
– Welcoming socket
• Welcomes some initial contact from a client.
– Connection socket
• Is created at initial contact of client.
• New socket that is dedicated to the particular client.
• Client
– Client socket
• Initiate a TCP connection to the server by creating a socket
object. (Three-way handshake)
• Specify the address of the server process, namely, the IP
address of the server and the port number of the process.
Socket functional calls
• socket (): Create a socket
• bind(): bind a socket to a local IP address and port #
• listen(): passively waiting for connections
• connect(): initiating connection to another socket
• accept(): accept a new connection
• Write(): write data to a socket
• Read(): read data from a socket
• sendto(): send a datagram to another UDP socket
• recvfrom(): read a datagram from a UDP socket
• close(): close a socket (tear down the connection)
Sockets
Socket-programming using TCP
socket( )
socket( ) bind( ) server
listen( )
client bind( )
connect( ) TCP conn. request
accept( )
send( ) TCP ACK
recv( )
recv( )
send( )
close( ) close( )
controlled by
application process
process
developer socket
socket
controlled by TCP with
TCP with
operating buffers, internet
system buffers,
variables
variables
Socket programming with TCP
keyboard monitor
inFromUser
input
input (inFromUser stream) , stream
inFromServer
outToServer
• client reads, prints modified output
stream
input
stream
line from socket
(inFromServer stream) client TCP
clientSocket
socket TCP
socket
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
Socket Programming with UDP
• UDP
– Connectionless and unreliable service.
– There isn’t an initial handshaking phase.
– Doesn’t have a pipe.
– transmitted data may be received out of order, or lost
inFromUser
input
stream
Client
Process
Input: receives
process
packet (UDP
received “byte
Output: sends
stream”)
packet (UDP sent
receivePacket
sendPacket
“byte stream”) UDP UDP
packet packet
client UDP
clientSocket
socket UDP
socket
write reply to
serverSocket
specifying client read reply from
host address, clientSocket
port umber close
clientSocket
Concurrent server
• Servers need to handle a new connection
request while processing previous requests.
– Most TCP servers are designed to be concurrent.
• When a new connection request arrives at a
server, the server accepts and invokes a new
process to handle the new client.
Socket programming: references
Java-tutorials:
• “All About Sockets” (Sun tutorial),
http://www.javaworld.com/javaworld/jw-12-1996/jw-12-
sockets.html
• “Socket Programming in Java: a tutorial,”
http://www.javaworld.com/javaworld/jw-12-1996/jw-12-
sockets.html
More Examples