You are on page 1of 12

Program to determine class of IP address :

#include<stdio.h>
#include<conio.h>
void binary(int a);
int count=0;
int main()
{
int a[4],i,j,k,cnt=0,x;
char p;
printf("Enter valid IP address :");
{
for(i=0;i<=3;i++)
scanf("%d%c",&a[i],&p);
}
x=a[1];
if(x>=1 && x<=127)
printf("IP address belongs to class A \n");
else if(x>=128 && x<191)
printf("IP address belongs to class B \n");
else if(x>=192 && x<223)
printf("IP address belongs to class C \n");
else if(x>=224 && x<239)
printf("IP address belongs to class D \n");
else if(x>=240 && x<254)
printf("IP address belongs to class E \n");

printf("IP address is :");


for(j=0;j<=3;j++)
{
printf("%d",a[j]);
cnt++;
if(cnt<=3)
printf(".");
}
printf("\n");
printf("Binary representation of IP address is :\n");
for(k=0;k<=3;k++)
binary(a[k]);
return 0;
}
void binary(int a)
{
int bin[8],y=7,b,i,j;
for(j=0;j<=7;j++)
bin[j]=0;
while(a!=0)
{
b=a%2;
bin[y]=b;
y--;
a=a/2;
}
for(i=0;i<=7;i++)
printf("%d",bin[i]);
count++;
if(count<=3)
printf(".");
}

Output :
Program to build chat server using UDP:
UDPClient.java
import java.io.*;
import java.net.*;
class UDPClient
{
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
System.out.println("UDP Client");
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}

Output :
UDPServer.java
import java.io.*;
import java.net.*;

class UDPServer
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData());
System.out.println("UDP Server");
System.out.println("RECEIVED: " + sentence);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}
Output:

Program to trace port number of localhost:


import java.net.ServerSocket;
class Aport {
public static void main(String[] args) {
try
int i;
for(i=1;i<30;i++)
{
ServerSocket ss = new ServerSocket(i);
System.out.println("port "+i+" is active");
}
}
catch(Exception e) {
System.out.println("Not active");
}
}
}

Output:
Program to determine logical address of local and remote host:
import java.net.InetAddress;

public class LogicalAddress


{
public static void main(String[] args)
throws Exception
{
try
{
InetAddress addr = InetAddress.getLocalHost();
System.out.println("Local HostAddress: "+addr.getHostAddress());
String hostname = addr.getHostName();
System.out.println("Local host name: "+hostname);
}
catch(Exception e)
{
System.out.println(“Error”);
}
}
}

Output :

Program to build client server using TCP such that client send number
to get its factorial :
TCPClient.java
import java.net.*;
import java.io.*;
class TCPClient
{
public static void main(String ar[])
{
int myPort = 1234;
try {
DatagramSocket ds = new DatagramSocket();
DatagramPacket pack;
InetAddress addr = InetAddress.getLocalHost();
BufferedReader b=new BufferedReader (new InputStreamReader(System.in));
{
System.out.print("Enter the number to find factorial : ");
String message=b.readLine();
byte [] data = new byte [ message.length() ];
message.getBytes(0, data.length, data, 0);
pack = new DatagramPacket(data, data.length, addr, myPort);
ds.send( pack );
}
}
catch ( IOException e )
{
System.out.println( e );
}
}
}

Output :

TCPServer.java
import java.net.*;
import java.io.*;
class TCPServer
{
public static void main(String ar[])
{
try{
DatagramSocket s = new DatagramSocket(1234);
while ( true )
{
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
s.receive( packet );
String message = new String(packet.getData(), 0, 0, packet.getLength());
int res=1;
int ms=Integer.parseInt(message);
for(int i=1;i<=ms;i++) res=res*i;
String str1=res+" ";
System.out.println( "Factorial of " +
message + " is " + str1);
}
}
catch(Exception e)
{}
}
}

Output :

Program to implement Daytime protocol :


TCPDaytimeClient.java
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.Socket;

public class TCPDaytimeClient {


public TCPDaytimeClient() {}

public TCPDaytimeClient(String host, int port) {

try (Socket connSocket = new Socket(host, port)) {


Reader isr = new InputStreamReader(connSocket.getInputStream(), "UTF-8");

StringBuilder response = new StringBuilder();

for (int inp = isr.read(); inp != -1; inp = isr.read()) response.append((char) inp);

System.out.println(response.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {


if (args.length != 2) {
System.out.println("Usage: TcpDaytimeTestClient [<host>] [<port>]");
System.exit(0);
}

new TCPDaytimeClient(args[0], Integer.valueOf(args[1]));


}
}

Output :

TCPDaytimeServer.java
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
class TCPDaytimeServer {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Starts TCP daytime server on the specified port.");
System.out.println("Usage: TcpDaytimeServer [<port>]");
System.exit(0);
}
new TCPDaytimeServer(Integer.valueOf(args[0]));
}
public TCPDaytimeServer(int port) {
try (ServerSocket socket = new ServerSocket(port)) {
System.out.println("TCP daytime server is listening on port " + port);
while (true)
{
try (Socket conn = socket.accept()) {
Date date = new Date();

Writer osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");


osw.write(date.toString() + "\r\n");
osw.flush();
System.out.println("Served a client from " +
conn.getInetAddress().getHostName() + ":" + conn.getPort());
} } }
catch (IOException ex) {
ex.printStackTrace();
} }
}

Output :

You might also like