Socket Programming: Babak Esfandiari (Based On Slides by Qusay Mahmoud)
Socket Programming: Babak Esfandiari (Based On Slides by Qusay Mahmoud)
Socket Programming: Babak Esfandiari (Based On Slides by Qusay Mahmoud)
Babak Esfandiari
(based on slides by
Qusay Mahmoud)
Sockets Programming
Client-Server Computing
What are Sockets
Sockets Programming in Java
Programming Examples
Client/Server Computing
Simple idea:
Some hosts (clients, typically desk top computers) are
specialized to interact with users:
– Gather input from users
– Present information to users
Other hosts (servers) are specialized to manage large
data, process that data
The Web is a good example: Client (Browser) & Server
(HTTP server)
Client/Server Computing
Other examples:
– E-mail
Server Client
Client
Client/Server Computing
Other examples:
– Chatroom
Tiered Client/Server Architecture
Constructors:
– One for sending datagrams:
DatagramPacket{byte buffer[], int
length, InetAddress, int port}
– One for receiving datagrams:
Constructors:
– One for the sender (randomly chosen port number):
DatagramSocket() throws SocketException
– One for the receiver (port needs to be specified):
void close()
Receiving UDP packets
DatagramSocket socket = new DatagramSocket(port);
Byte buffer[] = new byte[65508];
DatagramPacket packet = new DatagramPacket(buffer,
buffer.length);
Socket.receive(packet);
InetAddress fromAddress = packet.getAddress();
int fromPort = packet.getPort();
int length = packet.getLength();
byte[] data = packet.getData();
socket.close();
Sending UDP Packets
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(data,
data.length,
InetAddress.getByName(“eureka.sce.carleton.ca”),
1728);
socket.send(packet);
socket.close();
TCP Socket Communication
Streams
– The basic of all I/O in Java is the data stream
– A pipeline of data
put info into the pipeline (write) and get it (read)
Programming with Sockets (TCP)
– Opening a Socket
– Creating a data input stream
– Creating a data output stream
– Closing the socket(s)
Opening a socket
Client-side:
Client-side:
BufferedReader is = null;
try {
is = new BufferedReader(new
InputStreamReader(myClient.getInputStream()));
} catch (IOException ioe) {
ioe.printStackTrace();
}
Creating an input stream
Server-side:
BufferedReader is = null;
try {
is = new BufferedReader(new
InputStreamReader(serviceClient.getInputStream()));
} catch(IOException ioe) {
ioe.printStackTrace();
}
Creating an output stream
Client-side:
DataOutputStream os = null;
try {
os = new
DataOutputStream(myClient.getOutputStream());
} catch (IOException e) {
e.printStrackTrace();
}
Creating an output stream
Server-side:
DataOutputStream os = null;
try {
os = new
DataOutputStream(serviceClient.getOutputStream());
} catch(IOException e) {
e.printStackTrace();
}
Closing sockets
Client-side:
try {
os.close();
is.close();
myClient.close();
} catch(IOException e) {
e.printStrackTrace();
}
Closing sockets
Server-side:
try {
os.close();
is.close();
serviceSocket.close();
myService.close();
} catch(IOException e) {
e.printStackTrace();
}
An Example