[go: up one dir, main page]

0% found this document useful (0 votes)
32 views19 pages

Introduction To Sockets

Sukbsmbava aknfnsks hooch n bich

Uploaded by

sohamastane077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views19 pages

Introduction To Sockets

Sukbsmbava aknfnsks hooch n bich

Uploaded by

sohamastane077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Introduction to Sockets

• Socket
• Why Sockets? –
• Used for Interprocess communication.
• • The Client-Server model – Most interprocess
communication uses client-server model
• – Client & Server are two processes that wants
to communicate with each other
• – The Client process connects to the Server
process, to make a request for
information/services own by the Server.
• – Once the connection is established between
Client process and Server process, they can start
sending / receiving information.
• What are Sockets? –
• End-point of interprocess communication.
• – An interface through which processes can
send / receive information
• What exactly creates a Socket?
• – tuple – {Source , Destination }
• What makes a connection?
• i.e. source socket – destination socket pair
uniquely identifies a connection.
• Socket Types –
• STREAM – uses TCP which is reliable, stream
oriented protocol,
• DATAGRAM – uses UDP which is unreliable,
message oriented protocol
• RAW – provides RAW data transfer directly over
IP protocol (no transport layer)
• Sockets can use –
• “unicast” ( for a particular IP address destination) –
• “multicast” ( a set of destinations – 224.x.x.x)
• “broadcast” (direct and limited)
• Transmission Control Protocol
• A connection-based protocol that provides a reliable flow
of data between two computers.
• Provides a point-to-point channel for applications that
require reliable communications.
• – The Hypertext Transfer Protocol (HTTP),
File Transfer Protocol (FTP),
• and Telnet are all examples of applications that require a
reliable communication channel
• Guarantees that data sent from one end of the
connection actually gets to the other end and in the same
order it was sent.
• Otherwise, an error is reported.
User Datagram Protocol
• A protocol that sends independent packets of data,
called datagrams, from one computer to another with
no guarantees about arrival.
• UDP is not connection based like TCP and is not
reliable:
• – Sender does not wait for acknowledgements
• Arrival order is not guaranteed – Arrival is not
guaranteed
• Used when speed is essential, even in cost of
reliability –
• e.g. streaming media, games, Internet telephony, etc
Client-Side Programming
• To connect to another machine we need a socket connection.
• A socket connection means the two machines have information about each other’s
network location (IP Address) and TCP port.
• The java.net.Socket class represents a Socket.
• To open a socket:
• Socket socket = newSocket(“127.0.0.1”, 5000)

• The first argument – IP address of Server. ( 127.0.0.1 is the IP address of localhost,


where code will run on the single stand-alone machine).
• The second argument – TCP Port. (Just a number representing which application to
run on a server. For example, HTTP runs on port 80.
• Port number can be from 0 to 65535)
• Communication
To communicate over a socket connection, streams are used to both input and
output the data.
• Closing the connection
• The socket connection is closed explicitly once the message to the server is sent.


The TCP/IP Client Socket or Socket Class

• A TCP/IP client socket is used to create a reliable, bi-directional,


stream-based connection between two computers on a
network.
• A socket is simply an endpoint for communications between the
machines
• Socket class is used to connect to server sockets & initiate
protocol exchange
• It Establish connection between client and server.
Constructor
Socket (String hostname, int port)
Socket(InetAddress ipaddr, int port)
Methods of the Socket Class
• InetAddress getInetAddress() -- Returns the InetAddress that is
associated with the socket object.

int getPort() -- Returns the port number on which the socket is connected

int getLocalPort() -- Returns the local port number on which the socket is
created

InputStream getInputStream() -- Returns the InputStream associated with


the calling object.

OutputStream getOutputStream() -- Returns the OutputStream


associated with the calling object.

void close() -- Closes the InputStream() and OutputStream() of the socket.


TCP/IP Sockets
• Server sockets –
Wait for requests to come in over the network
– Implemented by java.net.ServerSocket class
• Client sockets
–Used to send and receive data
– Can be thought of as a pair of input and output
streams
– Implemented by java.net.Socket class
Server vr. Client Socket
• Server socket: waiting for connection requests
• Client socket: transmission of data
Server socket
• The constructors
1) ServerSocket (int port) throws BindException,
IOException
creates a server socket bounded to the specified
port with a queue length 50.
2) ServerSocket (int port, int maxQueue) throws
BindException, IOException
-creates a server socket bounded to the specified
port with a queue length of maxQueue.
3)ServerSocket (int port, int maxQ, InetAddress
ip) throws IOException
creates a server socket bounded to the specified port
with a queue length of maxQueue.
ip specifies the IP Address to which this socket binds.
Client socket
• The constructors
1) public Socket(String host, int port) throws
UnknownHostException, IOException.
❖Creates a socket connecting to the local host to
the named host and port
2) public Socket(InetAddress host, int port)
throws IOException
❖Creates a socket using a preexisting InetAddress
and port
steps occur when establishing a TCP connection
between two computers using sockets −
• The server instantiates a ServerSocket object,
denoting which port number communication is to
occur on.
• The server invokes the accept() method of the
ServerSocket class. This method waits until a client
connects to the server on the given port.
• After the server is waiting, a client instantiates a
Socket object, specifying the server name and the
port number to connect to.
• The constructor of the Socket class attempts to connect
the client to the specified server and the port number.
• If communication is established, the client now has a
Socket object capable of communicating with the
server.
• On the server side, the accept() method returns a
reference to a new socket on the server that is
connected to the client's socket.
• Once aconnection are established,communication
occurs using I/O Streams ie InputStream &
OutputStream.

You might also like