DarkWebCode is a place where you can find a lot of exciting and inspiring stuff about Web Development, Blogging, SEO and many more

Breaking

Applications using TCP and UDP Sockets like DNS, SNMP and File Transfer

Applications using TCP and UDP Sockets like DNS, SNMP and File Transfer

Aim

To write a java program for DNS application

 Algorithm

1.Start the program.

2.Get the frame size from the user

3.To create the frame based on the user request. 4.To send frames to server from the client side.

5.If your frames reach the server it will send ACK signal to client otherwise it will send NACK signal to client.

6.Stop the program


Program


/ UDP DNS Server Udpdnsserver

.java import java.io.*; import java.net.*;

public class udpdnsserver

{

private static int indexOf(String[] array, String str)

{

str = str.trim();

for (int i=0; i < array.length; i++)

{

if (array[i].equals(str)) return i;

}

return -1;

}

public static void main(String arg[])throws IOException

{

String[] hosts = {"yahoo.com", "gmail.com","cricinfo.com", "facebook.com"};
String[] ip = {"68.180.206.184", "209.85.148.19","80.168.92.140", "69.63.189.16"};
System.out.println("Press Ctrl + C to Quit");

while (true)



{

DatagramSocket serversocket=new DatagramSocket(1362); byte[] senddata = new byte[1021];

byte[] receivedata = new byte[1021];
 DatagramPacket recvpack = new DatagramPacket(receivedata, receivedata.length);
 serversocket.receive(recvpack);

String sen = new String(recvpack.getData()); InetAddress ipaddress = recvpack.getAddress(); int port = recvpack.getPort();

String capsent;

System.out.println("Request for host " + sen);
if(indexOf (hosts, sen) != -1) capsent = ip[indexOf (hosts, sen)]; else capsent = "Host Not Found"; senddata = capsent.getBytes();

DatagramPacket pack = new DatagramPacket (senddata, senddata.length,ipaddress,port); serversocket.send(pack);

serversocket.close();

}

}

}


//UDP DNS Client 

Udpdnsclient

.java import java.io.*; import java.net.*;

public class udpdnsclient

{

public static void main(String args[])throws IOException

{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientsocket = new DatagramSocket();

InetAddress ipaddress; if (args.length == 0)

ipaddress = InetAddress.getLocalHost(); else

ipaddress = InetAddress.getByName(args[0]); byte[] senddata = new byte[1024];


byte[] receivedata = new byte[1024];
int portaddr = 1362;
System.out.print("Enter the hostname : ");
String sentence = br.readLine();

Senddata = sentence.getBytes();

DatagramPacket pack = new DatagramPacket(senddata,senddata.length, ipaddress,portaddr);
 clientsocket.send(pack);

DatagramPacket recvpack =new DatagramPacket(receivedata,receivedata.length);
clientsocket.receive(recvpack);

String modified = new String(recvpack.getData()); System.out.println("IP Address: " + modified); clientsocket.close();

}

}


OUTPUT Server

javac udpdnsserver.java
java udpdnsserver
Press Ctrl + C to Quit Request for host yahoo.com
Request for host cricinfo.com
Request for host youtube.com


Client

 javac udpdnsclient.java
 java udpdnsclient
Enter the hostname : yahoo.com
IP Address: 68.180.206.184  
java udpdnsclient
Enter the hostname : cricinfo.com
IP Address: 80.168.92.140
 java udpdnsclient
 Enter the hostname : youtube.com
IP Address: Host Not Found


B.SNMP

Aim
To write a java program for SNMP application program

Algorithm

1.Start the program.
2.Get the frame size from the user

3.To create the frame based on the user request. 4.To send frames to server from the client side.

5.If your frames reach the server it will send ACK signal to client otherwise it will send NACK signal to client.

6.Stop the program


Program
import java.io.IOException;
import org.snmp4j.CommunityTarget; import org.snmp4j.PDU;
import org.snmp4j.Snmp; import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress; import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString; import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SNMPManager {


Snmp snmp = null;

String address = null;

*  Constructor

*  @param add

*/



public SNMPManager(String add)

{

address = add;
public static void main(String[] args) throws IOException {

/**

* Port 161 is used for Read and Other operations

*  Port 162 is used for the trap generation */

SNMPManager client = new SNMPManager("udp:127.0.0.1/161");

client.start();

/**

*  OID - .1.3.6.1.2.1.1.1.0 => SysDec

*  OID - .1.3.6.1.2.1.1.5.0 => SysName

* => MIB explorer will be usefull here, as discussed in previous article */


String sysDescr = client.getAsString(new OID(".1.3.6.1.2.1.1.1.0")); System.out.println(sysDescr);

}


/**

*  get any answers because the communication is asynchronous

*  and the listen() method listens for answers.

*  @throws IOException

*/

private void start() throws IOException {

TransportMapping transport = new DefaultUdpTransportMapping();

snmp = new Snmp(transport);

// Do not forget this line! transport.listen();

}


/**

*  Method which takes a single OID and returns the response from the agent as a String.

*  @param oid

*  @return

*  @throws IOException

*/


public String getAsString(OID oid) throws IOException {

ResponseEvent event = get(new OID[] { oid });

return event.getResponse().get(0).getVariable().toString();

}

/**

*  This method is capable of handling multiple OIDs

*  @param oids

*  @return

*  @throws IOException

*/

public ResponseEvent get(OID oids[]) throws IOException {
PDU pdu = new PDU();

for (OID oid : oids) {

pdu.add(new VariableBinding(oid));

}

pdu.setType(PDU.GET);

ResponseEvent event = snmp.send(pdu, getTarget(), null);
 if(event != null) {

return event;

}

throw new RuntimeException("GET timed out");

}

/**

*  This method returns a Target, which contains information about

*  where the data should be fetched and how.

*  @return

*/

private Target getTarget() {

Address targetAddress = GenericAddress.parse(address); CommunityTarget target = new CommunityTarget();

target.setCommunity(new OctetString("public"));
 target.setAddress(targetAddress);

target.setRetries(2);

target.setTimeout(1500);

target.setVersion(SnmpConstants.version2c);
 return target;

}}


OUT PUT

Hardware: x86 Family 6 Model 23 Stepping 10 AT/AT COMPATIBLE – Software: Windows 2000 Version 5.1 (Build 2600 Multiprocessor Free)




C. File Transfer


AIM

To write a java program for applaction using TCP and UDP Sockets Links

Program

File Client import java.io.*; import java.net.*; import java.util.*; class Clientfile

{              public static void main(String args[])

{

Try

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); Socket clsct=new Socket("127.0.0.1",139);

DataInputStream din=new DataInputStream(clsct.getInputStream()); DataOutputStream dout=new DataOutputStream(clsct.getOutputStream()); System.out.println("Enter the file name:");
String str=in.readLine(); dout.writeBytes(str+'\n'); System.out.println("Enter the new file name:"); String str2=in.readLine();

String str1,ss;

FileWriter f=new FileWriter(str2); char buffer[];

while(true)

{ str1=din.readLine(); if(str1.equals("-1")) break; System.out.println(str1); buffer=new char[str1.length()];

str1.getChars(0,str1.length(),buffer,0);

f.write(buffer);
}

f.close();

clsct.close();

}

catch (Exception e)

{

System.out.println(e);

}}}




Server

import java.io.*; import java.net.*; import java.util.*; class Serverfile

{ public static void main(String args[])

{

Try

{

ServerSocket obj=new ServerSocket(139); while(true)

{

Socket obj1=obj.accept();

DataInputStream din=new DataInputStream(obj1.getInputStream()); DataOutputStream dout=new DataOutputStream(obj1.getOutputStream()); String str=din.readLine();

FileReader f=new FileReader(str); BufferedReader b=new BufferedReader(f); String s;

while((s=b.readLine())!=null) { System.out.println(s);

dout.writeBytes(s+'\n');

}

f.close(); dout.writeBytes("-1\n");

}              }

catch(Exception e)

{              System.out.println(e);}

}

}



Output

File content Computer networks jhfcgsauf

jbsdava jbvuesagv client

Enter the file name: sample.txt

server

Computer networks jhfcgsauf

jbsdava jbvuesagv client

Enter the new file name: net.txt

Computer networks jhfcgsauf

jbsdava jbvuesagv Destination file

Computer networks jhfcgsauf

jbsdava jbvuesagv