You are on page 1of 7

Acropolis Institute of Technology & Research, Indore

Department of Computer Science & Engineering

Subject: Computer Networks Subject code: CS6004


Name: Priyash Yagnik Enrollment No.: 0827CS151155
Program 9

AIM : Write a program to find IP address of a given host name and to find host name of given
IP address .

import java.util.*;
import java.net.*;
public class JavaApplication38 {
public static void main(String[] args) throws UnknownHostException{
System.out.println("enter the IP address");
Scanner sc= new Scanner(System.in);
String str= sc.next();
InetAddress host =InetAddress.getByName(str);
System.out.println(host.getHostName());
String str1= sc.next();
InetAddress host1= InetAddress.getByName(str1);
System.out.println(host1.getHostAddress());
}
}

OUTPUT

Signature
Acropolis Institute of Technology & Research, Indore
Department of Computer Science & Engineering

Subject: Computer Networks Subject code: CS6004


Name: Priyash Yagnik Enrollment No.: 0827CS151155
Program 10

AIM : Write a program to find implement Simple Client Server .


Server-

import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket server=new ServerSocket(3000);
Socket s=server.accept();
system.out.println("Connected");
} catch (IOException e) {
}
}
}

Client-
import java.io.IOException;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket s=new Socket("192.168.0.4",3000);
System.out.println("Connected");
} catch (IOException e) {
}
}
}

Signature
Acropolis Institute of Technology & Research, Indore
Department of Computer Science & Engineering

Subject: Computer Networks Subject code: CS6004


Name: Priyash Yagnik Enrollment No.: 0827CS151155

OUTPUT

Signature
Acropolis Institute of Technology & Research, Indore
Department of Computer Science & Engineering

Subject: Computer Networks Subject code: CS6004


Name: Priyash Yagnik Enrollment No.: 0827CS151155
Program 11
AIM : Write a program to create a program to send message from server to client.

Server-.

import java.io.*;
import java.net.*;
public class ServerMsg {
public static void main(String[] args) {
try {
ServerSocket server=new ServerSocket(4000);
Socket s=server.accept();
system.out.println("Connected");
DataOutputStream dos=new
DataOutputStream(s.getOutputStream());
dos.writeUTF("Welcome to karachi");
} catch (IOException e) {
}
}
}

Client-.
import java.io.*;
import java.net.*;
public class ClientMsg {
public static void main(String[] args) {
try {
Socket s=new Socket("192.168.0.4",4000);
system.out.println("Connected");
DataInputStream dis=new DataInputStream(s.getInputStream());
String msg=dis.readUTF();
system.out.println(msg);
} catch (IOException e) {
}
}
}

Signature
Acropolis Institute of Technology & Research, Indore
Department of Computer Science & Engineering

Subject: Computer Networks Subject code: CS6004


Name: Priyash Yagnik Enrollment No.: 0827CS151155

OUTPUT

Signature
Acropolis Institute of Technology & Research, Indore
Department of Computer Science & Engineering

Subject: Computer Networks Subject code: CS6004


Name: Priyash Yagnik Enrollment No.: 0827CS151155

Program 12
AIM : Write a program to create a program to send message from Client to server.

Server-

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

public class Server1{


public static void main(String args[]) throws Exception {
ServerSocket ss =new ServerSocket(4444);
while(true){
Socket ls =ss.accept();
BufferedReader ci =new BufferedReader(new InputStreamReader(ls.getInputStream()));
String cstr;
cstr= ci.readLine();
system.out.println(cstr);
}
}
}

Client-
import java.io.*;
import java.net.*;

public class Client1{


public static void main(String args[]) throws Exception {
Socket cs= new Socket("localhost",4444);
BufferedReader ui = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream sout= new DataOutputStream(cs.getOutputStream());
String str;
str = ui.readLine();
sout.writeBytes(str +'\n');
cs.close();
}
}

Signature
Acropolis Institute of Technology & Research, Indore
Department of Computer Science & Engineering

Subject: Computer Networks Subject code: CS6004


Name: Priyash Yagnik Enrollment No.: 0827CS151155

OUTPUT

Signature

You might also like