8 Sockets Part2
8 Sockets Part2
8 Sockets Part2
What is a socket?
Socket
Socket pair
Client-server applications
Implementation of a protocol standard defined in an
RFC. (FTP, HTTP, SMTP)
Welcoming socket
Connection socket
Client
Client socket
Sockets
socket( )
bind( )
connect( )
send( )
socket( )
bind( )
listen( )
TCPconn.request
TCPACK
accept( )
recv( )
recv( )
close( )
controlled by
application
developer
controlled by
operating
system
send( )
close( )
process
socket
TCP with
buffers,
variables
process
internet
socket
TCP with
buffers,
variables
server
monitor
Client
Process
process
output stream:
sequence of bytes
out of process
output
stream
Input stream:
sequence of
bytes
into process
inFromServer
input
stream
outToServer
inFromUser
input
stream
client
TCP
clientSocket
socket
to network
from network
TCP
socket
Server
(running on hostid)
Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
TCP
create socket,
setup connect to hostid, port=x
clientSocket =
Socket()
send request using
clientSocket
java.net.ServerSocket
ServerSocket(int port)
Socket Accept(): Listens for a connection to be made to this socket
and accepts it. This method blocks until a connection is made.
TCPClient.java
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer =
new
DataOutputStream(clientSocket.getOutputStream());
TCPClient.java
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
clientSocket.close();
}
}
TCPServer.java
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
while(true) {
Socket connectionSocket = welcomeSocket.accept();
TCPServer.java
DataOutputStream outToClient =
new
DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
}
}
}
outToClient.writeBytes(capitalizedSentence);
(running on hostid)
create socket,
port=x, for
incoming request:
serverSocket =
DatagramSocket()
Client
create socket,
clientSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
using clientSocket
Client
Process
monitor
inFromUser
keyboard
Input: receives
process
packet (TCP
received byte
stream)
UDP
packet
receivePacket
sendPacket
Output: sends
client
UDP
clientSocket
socket
to network
from network
UDP
packet
UDP
socket
UDPClient.java
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
InetAddress IPAddress =
InetAddress.getByName("hostname");
sendData = sentence.getBytes();
UDPClient.java
DatagramPacket sendPacket =
new DatagramPacket(sendData,
sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence =
new String(receivePacket.getData());
clientSocket.close();
}
}
UDPServer.java
import java.io.*;
import java.net.*;
class UDPServer {
public static void main(String args[]) throws Exception
{
while(true)
{
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
UDPServer.java
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress,
port);
serverSocket.send(sendPacket);
}
}
}
WebServer.java
import java.io.*;
import java.net.*;
import java.util.*;
class WebServer{
public static void main(String argv[]) throws Exception {
String requestMessageLine;
String fileName;
ServerSocket listenSocket = new ServerSocket(6789);
Socket connectionSocket = listenSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
WebServer.java
requestMessageLine = inFromClient.readLine();
StringTokenizer tokenizedLine =
new StringTokenizer(requestMessageLine);
if (tokenizedLine.nextToken().equals("GET")){
fileName = tokenizedLine.nextToken();
if (fileName.startsWith("/") == true )
fileName = fileName.substring(1);
inFile.read(fileInBytes);
WebServer.java
outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");
if (fileName.endsWith(".jpg"))
outToClient.writeBytes("Content-Type: image/jpeg\r\n");
if (fileName.endsWith(".gif"))
outToClient.writeBytes("Content-Type: image/gif\r\n");
outToClient.writeBytes("\r\n");
outToClient.write(fileInBytes, 0, numOfBytes);
connectionSocket.close();
}
else System.out.println("Bad Request Message");
}
}
Concurrent server
Servers need to handle a new
connection request while
processing previous requests.
Most TCP servers are designed to be
concurrent.