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

Networking Programs

The document contains Java code examples demonstrating the use of InetAddress for retrieving local and remote IP addresses, as well as checking for multicast addresses. It also includes a simple client-server application where the client sends a number to the server, which returns the square of that number. The code showcases the basic networking capabilities in Java using sockets and data streams.

Uploaded by

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

Networking Programs

The document contains Java code examples demonstrating the use of InetAddress for retrieving local and remote IP addresses, as well as checking for multicast addresses. It also includes a simple client-server application where the client sends a number to the server, which returns the square of that number. The code showcases the basic networking capabilities in Java using sockets and data streams.

Uploaded by

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

--- Example on InetAddress ---

==============================

import [Link].*;

class InetDemo1
{
public static void main(String ar[]) throws UnknownHostException
{
InetAddress ip1 = [Link]();
[Link](ip1);

InetAddress ip2 = [Link]("[Link]");


[Link](ip2);

InetAddress ip3 = [Link]("[Link]");


[Link](ip3);

InetAddress ip4[] = [Link]("[Link]");


for(int i=0; i<[Link]; i++)
{
[Link](ip4[i]);
}

[Link]("\n");
[Link]("MSBTE has multicast address : " +
[Link]() );
[Link]("YAHOO has multicast address : " +
[Link]() );
[Link]("GOOGLE has multicast address : " +
ip4[0].isMulticastAddress() );

[Link]("\n");
[Link]("Host name in ip1 is : " + [Link]() );
[Link]("Host name in ip2 is : " + [Link]() );
[Link]("Host name in ip3 is : " + [Link]() );

}
}

===================================================================================
============
===================================================================================
============

// preparing client application

import [Link].*;
import [Link].*;

class ClientApp
{
public static void main(String ar[]) throws IOException, UnknownHostException
{
// step 1:
InetAddress ip1 = [Link]();
// step 2:
Socket soc = new Socket(ip1, 100);

// step 3:
DataInputStream dis = new DataInputStream([Link]() );
DataOutputStream dos = new DataOutputStream([Link]() );

// step 4:
[Link]("5");

// step 5:
String ans = [Link]();

// step 6:
[Link]("Square returned from server is : " + ans);
[Link]();

}
}

===================================================================================
============

// preparing server application

import [Link].*;
import [Link].*;

class ServerApp
{
public static void main(String ar[]) throws IOException
{
ServerSocket ssoc = new ServerSocket(100);
Socket s = [Link]();

DataInputStream dis = new DataInputStream([Link]() );


DataOutputStream dos = new DataOutputStream([Link]() );

String val = [Link]();

int num = [Link](val);


int sq = num * num;

[Link](sq + "");

[Link]();
[Link]();

}
}

===================================================================================
==============

You might also like