Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Socket Programming: Babak Esfandiari (Based On Slides by Qusay Mahmoud)

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 37

Socket Programming

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

 1-tier: single program


 2-tier: client/server (e.g. the Web)
 3-tier: application logic and databases on
different servers (e.g. the Web with CGI and
databases)
Client/Server Communication

 Two related processes on a single machine


may communicate through a pipe

 A pipe is a pseudo-file that can be used to


connect two processes together
Client/Server Communication

 Two UNRELATED processes may


communicate through files (process A write to a
file and process B reads from it)

 But HOW two processes located on two


different machines communicate? Solution:
Berkeley sockets.
What are sockets

 A socket is an end point of a connection


 Or: the interface between user and network
 Two sockets must be connected before they
can be used to transfer data (case of TCP)
 A number of connections to choose from:
– TCP, UDP, Multicast
 Types of Sockets
– SOCK_STREAM, SOCK_DGRAM, SOCK_RAW
Sockets
 Message destinations are specified as socket adresses
 Each socket address is a communication identifier:
– Internet address
– Port number
 The port number is an integer that is needed to
distinguish between services running on the same
machine
 Port numbers between 0 .. 1023 are reserved
Ports

 Some “well-known” ports:


– 21: ftp
– 23: telnet
– 80: http
– 161: snmp
 Check out /etc/services file for complete list of
ports and services associated to those ports
Which transport protocol (TCP v.
UDP)

 TCP -- Transmission Control Protocol


 UDP -- User Datagram Protocol
 What should I use?
– TCP is a reliable protocol, UDP is not
– TCP is connection-oriented, UDP is
connectionless
– TCP incurs overheads, UDP incurs fewer overheads
– UDP has a size limit of 64k, in TCP no limit
Unix and C specific data structures
 The <netdb.h> library provides the following data structures:
struct hostent { // for host info
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
#define h_addr h_addr_list[0]
};
Unix and C specific data structures

struct servent { //service info


char *s_name;
char **s_aliases;
int s_port;
char *s_proto;
};
Unix and C specific data structures

 The following functions return information


about a given host or service:
struct hostent *gethostbyname (char
*hostname)
struct servent *getservbyname (char
*service, char *protocol)
UDP Socket programming

 UDP is simple and efficient, but not reliable


 Communication takes place in a symmetric
manner: both ends send and receive
messages following an agreed upon protocol
UDP Socket Programming

 Since no connection is created, each message


should contain the address of the recipient.
 In Java, messages are contained in instances
of class DatagramPacket
The DatagramPacket class

 Constructors:
– One for sending datagrams:
DatagramPacket{byte buffer[], int
length, InetAddress, int port}
– One for receiving datagrams:

DatagramPacket{byte buffer[], int


length}
The DatagramPacket class

 The useful methods are the accessors:


InetAddress getAddress()
Int getPort()
Byte[] getData()
Int getLength()
Sending and receiving Datagram
packets: using sockets

 Both ends need to create sockets to


communicate
 Sockets will be “bound” to a given host and
port number
 The receiver needs to “listen” on a port number
that is known by the sender
 The sender can a priori use any port number
The DatagramSocket class

 Constructors:
– One for the sender (randomly chosen port number):
DatagramSocket() throws SocketException
– One for the receiver (port needs to be specified):

DatagramSocket(int port) throws


SocketException
The DatagramSocket class

 Sending and receiving messages:


void send(DatagramPacket packet)
throws IOException
void receive(DatagramPacket packet)
throws IOException
 Close the socket when you’re done!

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

 Sequence of steps normally taken to set up


socket communication and exchange data
between C/S
Sockets Programming in Java

 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:

Socket myClient = null;


try {
MyClient = new Socket(“host”, PotNumber);
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
}
 “host” can be symbolic name or IP address
Opening a socket
 Server-side
ServerSocket myService = null;
try {
myService = new ServerSocket(portNumber);
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
}
Socket serviceSocket;
serviceSocket = myService.accept();
Creating an input stream

 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

 See the “Counter” class example


Multi-threaded Servers

 A server should be able to serve multiple


clients simultaneously
Multi-threaded Servers

 See the MTEchoServer example

You might also like