0% found this document useful (0 votes)
2 views44 pages

10 - Network Programming - Socket Class in Java

The document provides an overview of network programming using Java sockets, detailing the concepts of sockets, their types (TCP and UDP), and the relevant Java classes such as Socket, ServerSocket, and DatagramSocket. It explains how sockets facilitate communication between programs over a network and outlines methods for creating, sending, receiving, and closing connections. Additionally, it includes examples of client-server interactions using both TCP and UDP protocols.

Uploaded by

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

10 - Network Programming - Socket Class in Java

The document provides an overview of network programming using Java sockets, detailing the concepts of sockets, their types (TCP and UDP), and the relevant Java classes such as Socket, ServerSocket, and DatagramSocket. It explains how sockets facilitate communication between programs over a network and outlines methods for creating, sending, receiving, and closing connections. Additionally, it includes examples of client-server interactions using both TCP and UDP protocols.

Uploaded by

yassaleh220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Network Programming

Java Sockets
Programming
(Low-Level Networking)

Network Programming
May Zakarneh
Sockets

A socket is a construct that represents


one end-point of a two-way
communication channel between two
programs running on the network.

Network Programming
May Zakarneh
Sockets (cont)

Using sockets, the OS provides


processes a file-like access to the
channel.
– i.e., sockets are allocated a file descriptor, and
processes can access (read/write) the socket by
specifying that descriptor

Network Programming
May Zakarneh
Sockets (cont)

A specific socket is identified by the


machine's IP and a port within that
machine.

A socket stores the IP and port


number of the other end-point
computer of the channel.
Network Programming
May Zakarneh
Sockets (cont)
 Sockets can be used with both the TCP
and the UDP transport layer protocols.
 When writing to a socket, the written
bytes are sent to the other computer and
port (e.g., over TCP/IP or UDP).
– That is, remote IP and port are
attached to the packets.
Network Programming
May Zakarneh
Sockets (cont)

When OS receives packets on the


network, it uses their destination port
to decide which socket should get the
received bytes.

Network Programming
May Zakarneh
Sockets (cont)
Sockets can:
1. Connect to a remote machine
2. Send data
3. Receive data
4. Close a connection
5. Bind to a port
6. Listen for incoming connection
7. Accept connections from remote
machines on a bound port

Network Programming
May Zakarneh
Java Sockets Programming
(Low-Level Networking)

The package java.net provides support


for sockets programming (and more).

• Typically you import everything


defined in this package with:

import java.net.*;
Network Programming
May Zakarneh
Java Sockets Programming
(Low-Level Networking)

The package java.net provides support


for sockets programming (and more).

• Typically you import everything


defined in this package with:

import java.net.*;
Network Programming
May Zakarneh
Classes
InetAddress

Socket

ServerSocket

DatagramSocket

DatagramPacket
Network Programming
May Zakarneh
Socket class
 Corresponds to active TCP sockets only!
– client sockets
– socket returned by accept();

 Passive sockets are supported by a


different class:
– ServerSocket

 UDP sockets are supported by


– DatagramSocket
Network Programming
May Zakarneh
The Java Socket Class
 The Socket class supports the
1. Connect to a remote machine [socket = new
Socket(…)]
2. Send data [socket.write()]
3. Receive data [socket.read()]
4. Close a connection [socket.close()]
– Normally a socket is encapsulated in a
InputStream class or a Reader class.

Network Programming
May Zakarneh
The Java ServerSocket Class
The ServerSocket class additionally
supports the
5. Bind to a port
[server_socket.bind()]
6. Listen for incoming connection
[server_socket.listen()]
7. Accept connections from remote
machines on a bound port
[server_socket.accept()]

Network Programming
May Zakarneh
JAVA TCP Sockets
 java.net.Socket
– Implements client sockets (also called just
“sockets”).
– An endpoint for communication between two
machines.
– Constructor and Methods
• Socket(String host, int port): Creates a stream socket
and connects it to the specified port number on the
named host.
• InputStream getInputStream()
• OutputStream getOutputStream()
• close()
Network Programming
May Zakarneh
JAVA TCP Sockets
 java.net.ServerSocket
– Implements server sockets.
– Waits for requests to come in over the network.
– Performs some operation based on the request.
– Constructor and Methods
• 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.

Network Programming
May Zakarneh
Sockets

Client socket, welcoming socket (passive) and connection socket (active)

Network Programming
May Zakarneh
Socket Constructors
 Constructor creates a TCP connection to a
named TCP server.
– There are a number of constructors:

 Socket(InetAddress server, int port);

 Socket(InetAddress server, int port,

InetAddress local, int localport);

 Socket(String hostname, int port);

Network Programming
May Zakarneh
Socket Methods
void close();
InetAddress getInetAddress();
InetAddress getLocalAddress();
InputStream getInputStream();
OutputStream getOutputStream();

Lots more (setting/getting socket


options, partial close, etc.)
Network Programming
May Zakarneh
Socket I/O
Socket I/O is based on the Java I/O
support
– in the package java.io

InputStream and OutputStream are


abstract classes
– common operations defined for all kinds
of InputStreams, OutputStreams…

Network Programming
May Zakarneh
ServerSocket Class
(TCP Passive Socket)

 Constructors:

 ServerSocket(int port);

 ServerSocket(int port, int


backlog);

 ServerSocket(int port, int


backlog,InetAddress bindAddr);
Network Programming
May Zakarneh
ServerSocket Methods
Socket accept();

void close();

InetAddress getInetAddress();

int getLocalPort();

throw IOException, SecurityException


Network Programming
May Zakarneh
Socket programming with TCP
 Example client-server app:
 client reads line from standard
input (inFromUser stream) ,
sends to server via socket
(outToServer stream)
 server reads line from socket
 server converts line to uppercase,
sends back to client
 client reads, prints modified line
from socket (inFromServer
stream)
Network Programming
May Zakarneh
Client/server socket interaction:
TCP

Network Programming
May Zakarneh
TCPClient.java

Network Programming
May Zakarneh
TCPClient.java cont.

Network Programming
May Zakarneh
TCPServer.java

Network Programming
May Zakarneh
TCPServer.java cont.

Network Programming
May Zakarneh
UDP Sockets

DatagramSocket class.

DatagramPacket class needed to


specify the payload.
• incoming or outgoing

Network Programming
May Zakarneh
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

Network Programming
May Zakarneh
Socket Programming with UDP
(cont.)
 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.
Network Programming
May Zakarneh
JAVA UDP Sockets
In Package java.net
– java.net.DatagramSocket
• A socket for sending and receiving datagram
packets.
• Constructor and Methods
 DatagramSocket(int port): Constructs a datagram socket
and binds it to the specified port on the local host
machine.
 void receive( DatagramPacket p)
 void send( DatagramPacket p)
 void close()

Network Programming
May Zakarneh
DatagramSocket Constructors
 DatagramSocket();

 DatagramSocket(int port);

 DatagramSocket(int port,
InetAddress a);

– All can throw SocketException or


SecurityException

Network Programming
May Zakarneh
Datagram Methods
 void connect(InetAddress a, int
port);

 void close();

 void receive(DatagramPacket p);

 void send(DatagramPacket p);

Lots more!
Network Programming
May Zakarneh
Datagram Packet

Contain the payload.


– (a byte array).

Can also be used to specify the


destination address.
– when not using connected mode UDP.

Network Programming
May Zakarneh
DatagramPacket Constructors

For receiving:
 DatagramPacket( byte[] buf, int len);

For sending:
 DatagramPacket( byte[] buf, int len,
InetAddress a, int port);

Network Programming
May Zakarneh
DatagramPacket methods

byte[] getData(); (Returns the data

buffer).

void setData(byte[] buf); (Set the

data buffer for this packet).

void setAddress(InetAddress a);


(Sets the IP address of the machine to which
this datagram is being sent.

Network Programming
May Zakarneh
DatagramPacket methods
 void setPort(int port); Sets the port
number on the remote host to which this datagram is
being sent.

 InetAddress getAddress(); Returns the


IP address of the machine to which this datagram is
being sent or from which the datagram was received.

 int getPort(); Returns the port number on


the remote host to which this datagram is being sent
or from which the datagram was received.

Network Programming
May Zakarneh
Example: Java client (UDP)

Network Programming
May Zakarneh
Client/server socket interaction:
UDP

Network Programming
May Zakarneh
UDPClient.java

Network Programming
May Zakarneh
UDPClient.java
cont.

Network Programming
May Zakarneh
UDPServer.java

Network Programming
May Zakarneh
UDPServer.java
cont.

Network Programming
May Zakarneh
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)

Network Programming
May Zakarneh

You might also like