0% found this document useful (0 votes)
31 views5 pages

Finding Name and IP Address of The Host

This document contains code snippets for Java programs that demonstrate how to get the name and IP address of the local host, create a basic server-client program using sockets, and create UDP client-server programs. The host program uses InetAddress to get the local host name and IP. The socket server program listens on port 8080 and the client connects and sends/receives data. The UDP programs demonstrate sending and receiving datagrams between a server and client.

Uploaded by

anon_269824458
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Finding Name and IP Address of The Host

This document contains code snippets for Java programs that demonstrate how to get the name and IP address of the local host, create a basic server-client program using sockets, and create UDP client-server programs. The host program uses InetAddress to get the local host name and IP. The socket server program listens on port 8080 and the client connects and sends/receives data. The UDP programs demonstrate sending and receiving datagrams between a server and client.

Uploaded by

anon_269824458
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Finding Name and IP Address of the host import java.io.*; import java.net.

*; class addr { public static void main(String args[]) throws IOException { String s1,s2; InetAddress adr= InetAddress.getLocalHost(); s1=adr.getHostName(); s2=adr.getHostAddress(); System.out.println("\n The Host Name: "+s1); System.out.println("\n The Host Address: "+s2); } } Output C:\java\ssb>javac addr.java C:\java\ssb>java addr The Host Name: cse193 The Host Address: 192.168.0.193

Server Program and Execution import java.io.*; import java.net.*; public class servaddr { static final int port = 8080; public static void main(String args[]) { try { ServerSocket ss= new ServerSocket(port); System.out.println("\n Server Started :"+ss); Socket s = ss.accept(); System.out.println("\n Connection accepted, socket:"+s); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter out= new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true); int r = Integer.parseInt(in.readLine()); System.out.println("\n radius :"+r); double area = 3.14 * r * r; System.out.println("\n area :"+area); out.println(area); System.out.println("\n Closing..."); s.close(); } catch (Exception e){} } } Output C:\java\ssb>javac servaddr.java C:\java\ssb>java servaddr Server Started :ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8080] Connection accepted, socket:Socket[addr=/127.0.0.1,port=1063,localport=8080] radius :5 area :78.5

Closing... Client Program and Execution import java.io.*; import java.net.*; public class cliaddr { static final int port = 8080; public static void main(String args[]) { try { InetAddress addr = InetAddress.getByName("localhost"); System.out.println("\n address :"+addr); Socket s = new Socket(addr,port); System.out.println("\n socket:"+s); System.out.println("\n Enter the radius value: "); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out= new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true); out.println(in.readLine()); BufferedReader brr = new BufferedReader(new InputStreamReader(s.getInputStream())); double area = Double.parseDouble(brr.readLine()); System.out.println("\n The area is : " +area); } catch (Exception e){} } } Output C:\java\ssb>javac cliaddr.java C:\java\ssb>java cliaddr address :localhost/127.0.0.1 socket:Socket[addr=localhost/127.0.0.1,port=8080,localport=1063] Enter the radius value: 5 The area is : 78.5

Server UDP program import java.io.*; import java.net.*; public class udpser { public static void main(String s[]) { try { DatagramSocket socket=new DatagramSocket(4545); while(socket!=null) { DatagramPacket recvPacket=new DatagramPacket(new byte[512],512); socket.receive(recvPacket); System.out.write(recvPacket.getData(),0,recvPacket.getLength()); System.out.println("\n"); BufferedReader userdata=new BufferedReader(new InputStreamReader(System.in)); String userString=userdata.readLine(); if(userString==null || userString.equals("")) return; byte sendbuf[]=userString.getBytes(); DatagramPacket sendPacket=new DatagramPacket(sendbuf,sendbuf.length,recvPacket.getAddress(),recvPacket.getPort()); socket.send(sendPacket); } } catch(SocketException se) { System.out.println("error in simple Datagram Server:"+se); } catch(IOException IOC) { System.out.println("error in simple Datagram ServerI/O:"+IOC); } } } Output C:\java\udp>javac udpser.java C:\java\udp>java udpser psna college of engineering and technology

Client UDP socket program import java.io.*; import java.net.*; public class udpcli { public static void main(String s[]) { try { DatagramSocket socket=new DatagramSocket(4546); InetAddress hostAddress=InetAddress.getLocalHost(); BufferedReader userdata=new BufferedReader(new InputStreamReader(System.in)); while(socket!=null) { String userString=userdata.readLine(); if(userString==null || userString.equals("")) return; byte sendbuf[]=userString.getBytes(); DatagramPacket sendPacket=new DatagramPacket(sendbuf,sendbuf.length,hostAddress,4545); socket.send(sendPacket); DatagramPacket recvPacket=new DatagramPacket(new byte[512],512); socket.receive(recvPacket); System.out.write(recvPacket.getData(),0,recvPacket.getLength()); System.out.println("\n"); } } catch(Exception e) { System.out.println("error in socket"); } } } Output C:\java\udp>javac udpcli.java C:\java\udp>java udpcli psna college of engineering and technology

You might also like