0% found this document useful (0 votes)
15 views20 pages

Unit 5

Unit 5 covers the basics of network programming in Java, explaining key concepts such as IP addresses, port numbers, protocols, and sockets. It details Java networking classes and interfaces, including InetAddress and URL, and their methods for handling network resources. The document also discusses IPv4 and IPv6 addressing schemes, along with the functionality of URLConnection for establishing communication links.

Uploaded by

luckyvdhemane9
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)
15 views20 pages

Unit 5

Unit 5 covers the basics of network programming in Java, explaining key concepts such as IP addresses, port numbers, protocols, and sockets. It details Java networking classes and interfaces, including InetAddress and URL, and their methods for handling network resources. The document also discusses IPv4 and IPv6 addressing schemes, along with the functionality of URLConnection for establishing communication links.

Uploaded by

luckyvdhemane9
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
You are on page 1/ 20

UNIT 5

Basics of Network Programming


The term network programming refers to writing programs that execute across multiple devices
(computers), in which the devices are all connected to each other using a network.
Java Networking is a concept of connecting two or more computing devices together so that we can
share resources.
Java socket programming provides facility to share data between different computing devices.

Java Networking Terminology


In Java Networking, many terminologies are used frequently. These widely used Java Networking
Terminologies are given as follows:

1. IP Address –
An IP address is a unique address that distinguishes a device on the internet or a local
network.
An IP address identifies a machine in an IP network and is used to determine the destination
of a data packet.
IP stands for “Internet Protocol.”
IP Address is referred to as a logical address.
It is composed of octets. The range of each octet varies from 0 to 255.
Range of the IP Address – 0.0.0.0 to 255.255.255.255
For Example – 192.168.0.1

2. Port Number –
A port number is a way to identify a specific process to which an internet or other network
message is to be forwarded when it arrives at a server.
It's part of a network address, along with the IP address, to ensure data is routed to the correct
application. Think of it like an apartment number within a building (the IP address).
Example:
When you visit a website, your request is sent to the server's IP address and port 80, which tells
the server to direct the request to the web server application.

Some reserved Ports for Specific Services:


20 – FTP (File Transfer Protocol) data transfer
21 – File Transfer Protocol (FTP) control (command)
25 – Simple Mail Transfer Protocol (SMTP)
80 – Hypertext Transfer Protocol (HTTP)

Java Programming Unit 5 ARROW COMPUTER ACADEMY


3. Protocol –

It comprises a set of rules governing the format of data sent via the internet or local
network.
Protocol defines how data is transmitted between different devices in the same network.
Eg:-TCP, FTP, Telnet, SMTP, POP (Post Office Protocol) etc.

4. Socket
A socket is one end-point of a two-way communication link between two programs running on
the network.
Socket classes are used to represent the connection between a client program and a server
program.
The java.net package provides two classes, Socket and ServerSocket, that implement the client side
of the connection and the server side of the connection, respectively.

Reserved sockets
Reserved sockets refer to sockets that are pre-defined and assigned to specific protocols or
services. These sockets, typically within the range of 0 to 1023, are used for established services like
HTTP, FTP, and SSH.

Proxy Server
A proxy server acts as a gateway between your device and the internet, masking your IP address
and enhancing online privacy. The proxy server also prevents the identification of the client’s IP
address when the client makes any request to any other servers.

How the Proxy server works


When you are trying to access the online web resource using your web browser, the request goes
to the proxy server firstly. The proxy server modifies or encreypted the system or personal
related infromation along with the IP address and forward it to the web resource.
Now the web resource responded back with the required web content to the proxy server.
The proxy server filters out web information and provides to you.
In this way the proxy server works as a filter and you get the data only you want.

Java Programming Unit 5 ARROW COMPUTER ACADEMY


Internet Addressing
Every computer that is connected to an internet has an address, which is called as IP address
Internet address (IP address) uniquely identifies each computer on the internet.
There are two versions of IP protocols :

1. IPV4

IPv4 address is 32 bit long


IPv4 address is sequence of four octets,where each number is between 0 to 255, these four numbers
are separated by dot (.) Ex: 192.168.1.141
IP address is divided into two parts - one part specifies the network prefix and the other part identifies
the host identifier
There are five classes of IPv4 addresses as follow :

Class A range for first byte is 0-127. Class A type of IP addresses have First byte consisting of Network
address with first bit as 0 and the next 3 bytes with host id.
Hence, number of hosts are more when compared to number of networks.
The default subnet masks for class A networks is 255.0.0.0.
There are 128 possible Class A networks.
However, any address that begins with 127. is considered a loopback address.
Host Ranges from:-

0.0.0.0 to 127.255.255.255
Example
2.134.213.2

Java Programming Unit 5 ARROW COMPUTER ACADEMY


Class B: Class B range for first byte is 128-191.
This type has first two bytes specifying network ID with starting two bits as 10 and last two bytes
referring to host ID.
The default subnet masks for class B is 255.255.0.0.
Host Ranges from
128.0.0.0 to 191.255.255.255
Example

135.58.24.17

Class C: Class C range for first byte is 192-223.


This class has first three bytes referring to network with starting bits as 110 and last byte signifies
Host ID. Here, number of networks is more when compared to number of hosts in each network.
The default subnet masks for class C is 255.255.255.0
Host Ranges from
192.0.0.0 to 223.255.255.255.
Example
192.168.178.1

Class D: Class D range for first byte is 224-239. Class D is used for multicasting and its starting bits are
1110

Class E: Class E range for first byte is 240-255. Class E is reserved for future use and its starting bits
are 1111

2. IPV6 Addressing Scheme


An IPv6 address is made of 128 bits divided into eight 16-bits blocks. Each block is then converted
into 4-digit Hexadecimal numbers separated by colon symbols.
For example, given below is a 128 bit IPv6 address represented in binary format and divided into
eight 16-bits blocks:
0010000000000001 0000000000000000 0011001000111000 1101111111100001
0000000001100011 0000000000000000 0000000000000000 1111111011111011
Each block is then converted into Hexadecimal and separated by ‘:’ symbol:
2001:0000:3238:DFE1:0063:0000:0000:FEFB

The IPv6 address consists mainly of two 64-bit segments where the higher part of the bits is
classified as the network part, and the lower 64 bits are classified as the client ID. The network part
is subdivided into the Global Unicast Address (GUA) and subnet ID.

Java Programming Unit 5 ARROW COMPUTER ACADEMY


Java Networking classes
The java.net package of the Java programming language includes various classes that provide an
easy-to-use means to access network resources.
The classes covered in the java.net package are given as follows –

1. CookieHandler
2. CookieManager
3. DatagramPacket
4. InetAddress
5. ServerSocket
6. Socket
7. DatagramSocket
8. Proxy
9. URL
10. URLConnection

Java Networking Interfaces


The java.net package of the Java programming language includes various interfaces also that
provide an easy-to-use means to access network resources. The interfaces included in
the java.net package are as follows:

1. CookiePolicy
2. CookieStore
3. FileNameMap
4. SocketOption
5. SocketImplFactory
6. ProtocolFamily

Java Programming Unit 5 ARROW COMPUTER ACADEMY


InetAddress

The java.net.InetAddress class provides methods to get the IP address of any hostname.
An IP address is represented by 32-bit or 128-bit unsigned number.
InetAddress can handle both IPv4 and IPv6 addresses.
There are 2 types of addresses :
 Unicast — An identifier for a single interface.
 Multicast — An identifier for a set of interfaces.

There are no public InetAddress() constructors.


To create an InetAddress object, you have to use one of the available factory methods.
Factory methods are static methods in a class that return an instance of that class.
It is often used as an alternative to constructors.

 Factory methods.
Three commonly used InetAddress factory methods are.
a)static InetAddress getLocalHost( )throws UnknownHostException
Returns the InetAddress object that represents the localhost.

b)static InetAddress getByName(String hostName) throws UnknownHostException


Returns the InetAddress for a host name passed to it.

c)static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException


Returns an array of InetAddress that represent all of the addresses that a particular name resolves to.

If these methods are unable to resolve the host name,they throw an UnknownHostException.

 Instance Methods:-
The InetAddress class also has several other methods.

 byte[ ] getAddress( )
Returns a four-element byte array that represents the object's Internet address in
network byte order.
 String getHostAddress( )
Returns a string that represents the host address associated with the InetAddress
object.
 String getHostName( )
Returns a string that represents the host name associated with the InetAddress
object
 boolean isMulticastAddress( )
Returns true if this Internet address is a multicast address. Otherwise, it returns
false.
 Boolean equals(Object other)
Returns true if this object has the same Internet address as other.

Java Programming Unit 5 ARROW COMPUTER ACADEMY


Program to use above Methods
import java.net.*;
class InetAddressDemo
{
public static void main(String args[])throws UnknownHostException
{
InetAddress addr=InetAddress.getLocalHost();
System.out.println(addr);
addr=InetAddress.getByName("www.msbte.com");
System.out.println(addr);
System.out.println("Host Name: "+addr.getHostName());
System.out.println("IP Address: "+addr.getHostAddress());
System.out.println("Whether address of www.msbte.com is Multicast :- "+ addr.isMulticastAddress());
InetAddress a[]=InetAddress.getAllByName("www.google.com");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}

Java Programming Unit 5 ARROW COMPUTER ACADEMY


URL
Class URL in java.net package represents a Uniform Resource Locator, that points to a "resource"
on the World Wide Web.
A resource can be something as simple as a file or a directory.

Format of URL path


In general, a URL can be broken into several parts.

A URL contains many information:

1. Protocol: In this case, http is the protocol.


2. Host name : It is name of the machine on which the resource lives. In this case,
www.example.com is the Host name.
3. Port Number: Port number to which to connect. It is an optional attribute.
4. File/Path Name: The pathname to the file on the machine.

The URL class is a wrapper of standard URL string. It is a final class and is directly inherited from
the Object class.
This class provides a simple way of accessing information across the Internet using URLs.
URL class has following constructors,and each can throw a MalformedURLException.

Constructor and Description

1. URL(String spec)
Creates a URL object from the String representation.

2. URL(String protocol, String host, int port, String file)


Creates a URL object from the specified protocol, host, port number, and file.

3. URL(String protocol, String host, String file)


Creates a URL object from the specified protocol name, host name, and file name.

4. URL(URL context, String spec)


Creates a URL object by parsing the given spec within a specified context.

Java Programming Unit 5 ARROW COMPUTER ACADEMY


Methods of URL Class

1. public String getPath()


Returns:
the path part of this URL, or an empty string if one does not exist

2. public String getAuthority()


Returns:
the authority part of this URL

3. public int getPort()


Returns the port number, or -1 if the port is not set

4. public int getDefaultPort()


Gets the default port number of the protocol associated with this URL. If the URL scheme do not define
a default port number, then -1 is returned.

5. public String getProtocol()


Returns:
the protocol of this URL.

6. public String getHost()


Returns:
the host name of this URL.

7. public String getFile()


Returns:
the file name of this URL, or an empty string if one does not exist

Example 1:
import java.net.*;
public class URLDemo{
public static void main(String[] args){
try
{
URL url=new URL("https://msbte.org.in/file/ALearningManualBAsicScienceM_090220210025.pdf");
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("Default Port Number: "+url.getDefaultPort());
System.out.println("File: "+url.getFile());
}
catch(Exception e){System.out.println(e);}
}
}

Java Programming Unit 5 ARROW COMPUTER ACADEMY


Output:
Protocol: https
Host Name: msbte.org.in
Port Number: -1
Default Port Number: 443
File: /file/ALearningManualBAsicScienceM_090220210025.pdf

URLConnection
The Java URLConnection class is an abstract class that represents a communication link
between the URL and the application. It can be used to read and write data to the specified
resource referred by the URL.

Constructor
protected URLConnection(URL url)
It constructs a URL connection to the specified URL.

Features:-
1. URLConnection is an abstract class. The two subclasses HttpURLConnection and
JarURLConnection makes the connetion between the client Java program and URL
resource on the internet.
2. With the help of URLConnection class, a user can read and write to and from any resource
referenced by an URL object.
3. Once a connection is established and the Java program has an URLConnection object, we can
use it to read or write or get further information like content length, etc.

openConnection()
The openConnection() method of the URL class returns the object of URLConnection class.
Instance of URLConnection can be obtained by calling the openConnection() method on a URL object.

Syntax: public URLConnection openConnection()throws IOException{}


Example:-
URL url=new URL("https://msbte.ac.in/");
URLConnection con=url.openConnection();

Java Programming Unit 5 ARROW COMPUTER ACADEMY


URLConnection Class Methods
Method Description

Object getContent() It retrieves the contents of the URL connection.

int getContentLength() It returns the value of the content-length header field.

String getContentType() It returns the value of the content-type header field.

long getDate() It returns the value of the date header field.

long getExpiration() It returns the value of the expires header filed.

long getLastModified() It returns the value of the last-modified header field.

URL getURL() It returns the value of the URLConnection's URL field.

InputStream getInputStream() It returns an input stream that reads from the open condition.

OutputStream getOutputStream() It returns an output stream that writes to the connection.

Program to display Date, Content Type, Content Length and Contents of Web Page.
import java.io.*;
import java.net.*;
import java.util.Date;
public class URLDemo
{
public static void main(String[] args)
{
try
{
URL url=new URL("https://msbte.ac.in/");
URLConnection con=url.openConnection();
System.out.println("Date="+new Date(con.getDate()));
System.out.println("Type of contents="+(con.getContentType()));
System.out.println("Length of contents="+(con.getContentLength()));
System.out.println("Expiration Date="+new Date(con.getExpiration()));
System.out.println("Last Modified Date="+new Date(con.getLastModified()));
/* This Block prints the contents of Web Page
InputStream stream=con.getInputStream();
int i;
while((i=stream.read())!=-1)
{
System.out.print((char)i);
}

Java Programming Unit 5 ARROW COMPUTER ACADEMY


*/
}
catch(Exception e)
{
}
}
}

Output:-
Date=Tue Jan 17 13:09:25 IST 2023
Type of contents=text/html
length of contents=1121949
Expiration Date=Thu Jan 01 05:30:00 IST 1970
Last Modified Date=Tue Jan 17 11:12:56 IST 2023

Java Programming Unit 5 ARROW COMPUTER ACADEMY


TCP/IP Sockets
TCP/IP sockets are used to implement reliable two-way, persistent, point-to-point connections
between hosts on the Internet.
Client sends a message to the server, server reads the message and prints it.
The java.net package provides two classes that allow you to create the two kinds of sockets.
The classes are Socket and ServerSocket respectively.
The Socket class, basically used to create client sockets, is designed to connect to server sockets.
The ServerSocket class,basically used to create server sockets, is designed to be a listener, which
waits for clients to connect before doing anything.

The following steps occur when establishing a TCP connection between two computers using sockets

1. The server instantiates a ServerSocket object, denoting which port number communication
is to occur on.
2. The server invokes the accept() method of the ServerSocket class. This method waits until a
client connects to the server on the given port.
3. After the server is waiting, a client instantiates a Socket object, specifying the server name
and the port number to connect to.
4. The constructor of the Socket class attempts to connect the client to the specified server and
the port number.
5. If communication is established, the client now has a Socket object capable of communicating
with the server.

1. Socket class
The java.net.Socket class in Java facilitates client-side TCP communication, enabling interaction
between a client and a server.
It represents a socket, which serves as an endpoint for communication between two machines over
a network.
When you create a Socket object, it implicitly establishes the connection between the client and the
server as well as send and receive data.

Constructors to create client sockets:-

1. Socket(String hostName, int port) throws UnknownHostException, IOException: Creates


a socket connected to the specified host and port.
2. Socket(InetAddress ipAddress, int port) throws IOException: Creates a socket using a pre-
existing InetAddress object and a port.

Java Programming Unit 5 ARROW COMPUTER ACADEMY


Methods of Socket class

After you have created a socket, you can get the address and port details associated with the socket by
the use of the following methods:

1. InetAddress getInetAddress( ): It returns the InetAddress associated with the Socket object. It
returns null if the socket is not connected.
2. int getPort( ): It returns the remote port to which the invoking Socket object is connected. It
returns 0 if the socket is not connected.
3. int getLocalPort( ): Returns the local port to which the invoking Socket object is bound. It returns
-1 if the socket is not bound.
4. InputStream getInputStream( ) throws IOException: Returns the InputStream associated with
the invoking socket.
5. OutputStream getOutputStream( ) throws IOException: Returns the OutputStream associated
with the invoking socket.
6. void connect(SocketAddress endpoint) :- Connects this socket to the server.
7. void bind(SocketAddress bindpoint):- Binds the socket to a local address.
8. isConnected( ): Returns true if the socket is connected to a server
9. isBound( ): Returns true if the socket is bound to an address
10. isClosed( ): Returns true if the socket is closed.

2. ServerSocket class:-
The ServerSocket class is used to create servers that listen for either local or remote client
programs to connect to them on standard ports.
The ServerSocket object can create a Socket object to receive and transmit data through the
socket.

Constructor:-
ServerSocket(int port) throws IOException
This constructor is used to create a server socket on the specified port. It throws an IOException.

Methods:
Socket accept()
This method listens for a connection request and accepts the connection. When the connection
is accepted, a Socket object is returned, that can be used to send and receive data through the
connection.

bind(SocketAddress endpoint) Binds the ServerSocket to a specific address (IP address and
port number).

InetAddress getInetAddress()
These methods returns an IP address used by the invoking ServerSocket object.

Java Programming Unit 5 ARROW COMPUTER ACADEMY


Int getLocalPort()
These methods returns port number used by the invoking ServerSocket object.

void close()
Last method closes the socket and releases any system resources associated with it.

Example of Java Socket Programming


Creating Server:
To create the server application, we need to create the instance of ServerSocket class. Here, we are
using 6666 port number for the communication between the client and server. You may also choose
any other port number. The accept() method waits for the client. If clients connects with the given
port number, it returns an instance of Socket.
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept(); //establishes connection and waits for the client

Creating Client:
To create the client application, we need to create the instance of Socket class. Here, we need to pass
the IP address or hostname of the Server and a port number. Here, we are using "localhost"
because our server is running on same system.
Socket s=new Socket("localhost",6666);

Let's see a simple of Java socket programming where client sends a text and server receives and prints
it.

// MyServer.java
import java.io.*;
import java.net.*;
public class MyServer
{
public static void main(String[] args)
{
try
{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept(); //establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=dis.readLine();
System.out.println("message= "+str);
ss.close();
}
catch(Exception e)
{
System.out.println(e);

Java Programming Unit 5 ARROW COMPUTER ACADEMY


}
}
}
// MyClient.java
import java.io.*;
import java.net.*;
public class MyClient
{
public static void main(String[] args)
{
try
{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeBytes("Hello Server");
dout.flush();
dout.close();
s.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

To execute this program open two command prompts and execute each program at each command
prompt.
After running the client application, a message will be displayed on the server console.

Example of Java Socket Programming (Read-Write both side)


Q] Write a program using Socket and ServerSocket to create Chat Application
In this example, client will write first to the server then server will receive and print the text. Then
server will write to the client and client will receive and print the text. The step goes on.

MyServer.java

import java.net.*;
import java.io.*;
class MyServer{
public static void main(String args[])throws Exception
{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());

Java Programming Unit 5 ARROW COMPUTER ACADEMY


DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop"))
{
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}}

//MyClient.java

import java.net.*;
import java.io.*;
class MyClient{
public static void main(String args[])throws Exception
{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str="",str2="";
while(!str.equals("stop"))
{
str=br.readLine(); // Write something to stream
dout.writeUTF(str);
dout.flush();

str2=din.readUTF(); //Read from server


System.out.println("Server says: "+str2);
}

dout.close();
s.close();
}
}

Java Programming Unit 5 ARROW COMPUTER ACADEMY


Datagram
Datagrams are collection of information sent from one device to another device via the established
network. When the datagram is sent to the targeted device, there is no assurance that it will reach to
the target device safely and completely. It may get damaged or lost in between. Likewise, the receiving
device also never know if the datagram received is damaged or not.
The UDP protocol is used to implement the datagrams in Java.

Java DatagramSocket and DatagramPacket


Java DatagramSocket and DatagramPacket classes are used for connection-less socket programming
using the UDP instead of TCP.

1. Java DatagramSocket class


Java DatagramSocket class represents a connection-less socket for sending and receiving
datagram packets. It is a mechanism used for transmitting datagram packets over network.
A datagram is basically an information but there is no guarantee of its content, arrival or arrival time.

 Constructors of DatagramSocket class


1. DatagramSocket() throws SocketEeption: it creates a datagram socket and binds it with the
available Port Number on the localhost machine.
2. DatagramSocket(int port) throws SocketEeption: it creates a datagram socket and binds it with
the given Port Number.
3. DatagramSocket(int port, InetAddress address) throws SocketEeption: it creates a datagram
socket and binds it with the specified port number and host address.

 Methods of DatagramSocket
1. void send(DatagramPacket packet)throws IOException
This method sends the datagram’s data to the previously defined host and port by taking a
DatagramPacket object as a parameter.

2. void receive(DatagramPacket packet)throws IOException


This method receives a datagram by taking a DatagramPacket object as a parameter.
To extract data from the received datagram use the getData() method defined in the
DatagramPacket class.

3. void close() :This method closes the opened datagram socket.

Java Programming Unit 5 ARROW COMPUTER ACADEMY


2. Java DatagramPacket Class
Java DatagramPacket is a message that can be sent or received. It is a data container. If you send
multiple packet, it may arrive in any order. Additionally, packet delivery is not guaranteed.

 Constructors of DatagramPacket class


1. DatagramPacket(byte[] barr, int length): it creates a datagram packet. This constructor is used
to receive the packets.
2. DatagramPacket(byte[] barr, int length, InetAddress address, int port): it creates a datagram
packet. This constructor is used to send the packets.

 Methods of DatagramPacket class

Method Description

1) InetAddress It returns the IP address of the machine to which the datagram is being sent
getAddress() or from which the datagram was received.

2) byte[] getData() It returns the data contained in Datagram. This is stored in array of bytes

3) int getLength() It returns the length of the data to be sent or the length of the data received.

It returns the port number on the remote host to which the datagram is being
4) int getPort()
sent or from which the datagram was received.

5) SocketAddress It gets the SocketAddress (IP address + port number) of the remote host that
getSocketAddress() the packet is being sent to or is coming from.

6) void setAddress
It sets the IP address of the machine to which the datagram is being sent.
(InetAddress iaddr)

Example of Sending DatagramPacket by DatagramSocket


// Dsender.java
import java.net.*;
public class Dsender
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket();
String str = "Welcome java";
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
ds.send(dp);

Java Programming Unit 5 ARROW COMPUTER ACADEMY


ds.close();
}
}

Example of Receiving DatagramPacket by DatagramSocket


//DReceiver.java
import java.net.*;
public class Dreceiver
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(str);
ds.close();
}
}

Java Programming Unit 5 ARROW COMPUTER ACADEMY

You might also like