0% found this document useful (0 votes)
292 views

Java Socket Programming

Socket programming allows for communication between applications using TCP/IP protocols. Sockets represent the combination of an IP address and port number that uniquely identify each endpoint of communication. There are stream sockets for reliable two-way connections and datagram sockets for unreliable datagrams. Client sockets initiate connections to server sockets which listen for incoming requests and create new sockets for each client connection. Key socket calls include socket(), bind(), listen(), accept(), connect(), read(), write(), and close().

Uploaded by

Pratham Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
292 views

Java Socket Programming

Socket programming allows for communication between applications using TCP/IP protocols. Sockets represent the combination of an IP address and port number that uniquely identify each endpoint of communication. There are stream sockets for reliable two-way connections and datagram sockets for unreliable datagrams. Client sockets initiate connections to server sockets which listen for incoming requests and create new sockets for each client connection. Key socket calls include socket(), bind(), listen(), accept(), connect(), read(), write(), and close().

Uploaded by

Pratham Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

JAVA Socket Programming

Source: by Joonbok Lee, KAIST, 2003


What is a socket?
• Socket
– The combination of an IP address and a port number.
– The name of the Berkeley-derived application programming
interfaces (APIs) for applications using TCP/IP protocols.
– Two types
• Stream socket : reliable two-way connected communication streams
• Datagram socket

• 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

Example client-server app:


• client reads line from standard

inFromUser
input
input (inFromUser stream) , stream

sends to server via socket Client


Process Input stream:
(outToServer stream) process sequence of bytes
• server reads line from socket output stream: into process
• server converts line to sequence of bytes
uppercase, sends back to client out of process

inFromServer
outToServer
• client reads, prints modified output
stream
input
stream
line from socket
(inFromServer stream) client TCP
clientSocket
socket TCP
socket

to network from network


Client/server socket interaction: TCP
Server (running on hostid) Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()

TCP create socket,


wait for incoming
connection request connection setup connect to hostid, port=x
connectionSocket = clientSocket =
welcomeSocket.accept() Socket()

send request using


read request from clientSocket
connectionSocket

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

• Socket Programming with UDP


– No need for a welcoming socket.
– No streams are attached to the sockets.
– the sending hosts creates “packets” by attaching the IP destination
address and port number to each batch of bytes.
– The receiving process must unravel to received packet to obtain the
packet’s information bytes.
Example: Java client (UDP)
keyboard monitor

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

to network from network


Client/server socket interaction: UDP
Server (running on hostid) Client

create socket, create socket,


port=x, for 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 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

C-language tutorial (audio/slides):


• “Unix Network Programming” (J. Kurose),
http://manic.cs.umass.edu/~amldemo/courseware/intro.html

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

• You can download more sample


programs here:
http://www.cs.uic.edu/~troy/spring05/cs45
0/sockets/socket.html

You might also like