1.
TCP MULTIPLE CHAT:
[Link]:
import [Link].*;
import [Link].*;
public class Client {
public static void main(String argv[]) throws Exception {
String ip;
String op;
Socket clientSocket=null;
BufferedReader in2 = new BufferedReader(new InputStreamReader([Link]));
clientSocket = new Socket("localhost", 8583);
PrintWriter out = new PrintWriter([Link](), true);
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
[Link]("Ready");
op = [Link]();
[Link](op);
while(!([Link]())){
if((ip = [Link]())!=null){
[Link]("Server-->"+ip);
[Link]("Server-->"+(ip=[Link]()));
if([Link]("Bye")) break;
op = [Link]();
[Link](op);
}
if ([Link]("Bye"))
break;
}
[Link]("Connection Closed");
[Link]();
}
}
[Link]:
import [Link].*;
import [Link].*;
public class Server {
private static int counter = 0;
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listeningSocket = true;
try {
serverSocket = new ServerSocket(8583);
} catch (IOException e) {
[Link]("Could not listen on port: 8583");
}
while(listeningSocket){
Socket clientSocket = [Link]();
TServer mini = new TServer(clientSocket,++counter);
[Link]();
}
[Link]();
}
}
[Link]:
import [Link].*;
import [Link].*;
public class TServer extends Thread{
private Socket socket = null;
private String id;
public TServer(Socket socket,int c) {
super("TServer");
id="Client "+c;
[Link] = socket;
}
public void run(){
try (
PrintWriter out = new PrintWriter([Link](), true);
BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));
) {
String ip,op=null;
boolean i=true;
BufferedReader in2 = new BufferedReader(new InputStreamReader([Link]));
while (!([Link]())&&i) {
long StartTime = [Link]() / 1000;
while(i){
if((ip = [Link]())!=null){
[Link](id+"-->"+ip);
[Link]("1");
if([Link]("Bye")) break;
[Link]("Reply to "+id+":");
op = [Link]();
[Link](op);
}
if ([Link]("Bye"))
break;
if(200<(([Link]() / 1000)-StartTime)){
i=false;
[Link]("200 seconds up");
}
}
}
[Link]("Connection Closed with "+id);
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
[Link] CLIENT IP:
[Link]:
import [Link].*;
import [Link].*;
import [Link].*;
class aServer
{
public static void main(String args[])
{
ServerSocket ss;
Socket s;
String inet;
DataInputStream dis;
try
{
ss=new ServerSocket(8020);
while(true)
{
s=[Link]();
dis=new DataInputStream([Link]());
inet=[Link]();
[Link]("THE CLIENT SYSTEM ADDRESS IS :"+inet);
}
}
catch(IOException e)
{
[Link]("The exception is :"+e);
}
}
}
[Link]:
import [Link].*;
import [Link].*;
class aClient
{
public static void main (String args[])
{
Socket soc;
PrintStream ps;
try
{
InetAddress ia=[Link]();
soc=new Socket(ia,8020);
ps=new PrintStream([Link]());
[Link](ia);
}
catch(IOException e)
{
[Link]("THE EXCEPTION is :"+e);
}
}
}
[Link] MULTICAST MESSAGE:
[Link]:
import [Link].*;
import [Link].*;
public class mServer {
public static void main(String[] args) {
DatagramSocket socket = null;
DatagramPacket outPacket = null;
BufferedReader dis;
byte[] outBuf;
final int PORT = 8888;
try {
socket = new DatagramSocket();
dis=new BufferedReader(new InputStreamReader([Link]));
String msg;
while (!((msg =[Link]()).equalsIgnoreCase("bye"))) {
outBuf = [Link]();
InetAddress address = [Link]("[Link]");
outPacket = new DatagramPacket(outBuf, [Link], address, PORT);
[Link](outPacket);
[Link]("Server sends : " + msg);
}
} catch (IOException ioe) {
[Link](ioe);
}
}
}
[Link]:
import [Link].*;
import [Link].*;
public class mClient {
public static void main(String[] args) {
MulticastSocket socket = null;
DatagramPacket inPacket = null;
byte[] inBuf = new byte[256];
try {
socket = new MulticastSocket(8888);
InetAddress address = [Link]("[Link]");
[Link](address);
while (true) {
inPacket = new DatagramPacket(inBuf, [Link]);
[Link](inPacket);
String msg = new String(inBuf, 0, [Link]());
[Link]("From " + [Link]() + " Msg : " + msg);
}
} catch (IOException ioe) {
[Link](ioe);
}
}
}
[Link] WEBPAGE:
import [Link].*;
import [Link].*;
public class webPage
{
public static void main(String[] paramArrayOfString)
throws Exception
{
BufferedReader in1 = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the URL: ");
String str1 = [Link]();
URL localURL = new URL(str1);
BufferedReader in2 = new BufferedReader(new InputStreamReader([Link]()));
BufferedWriter out = new BufferedWriter(new FileWriter("[Link]"));
String str2;
while ((str2 = [Link]()) != null) {
[Link](str2);
[Link](str2);
[Link]();
}
[Link]();
[Link]();
}
}
[Link] PGM:
import [Link].*;
import [Link].*;
public class ping{
public static void main(String[] args)
throws Exception {
String s;
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the URL: ");
s=[Link]();
boolean chk = pg(s);
if(chk) [Link]("The host "+s+" is reachable");
else [Link]("The host "+s+" is not reachable");
}
public static boolean pg(String host)
{
boolean isReachable = false;
try {
Process proc = new ProcessBuilder("ping", host).start();
int exitValue = [Link]();
[Link]("Exit Value:" + exitValue);
if(exitValue == 0)
isReachable = true;
} catch (IOException e1) {
[Link]([Link]());
[Link]();
} catch (InterruptedException e) {
[Link]();
}
return isReachable;
}
}
[Link] FILE TRANSFER:
CLIENT:
import [Link].*;
import [Link].*;
class udpClient
{
public static void main(String args[])throws IOException
{
byte b[]=new byte[1024];
DatagramSocket ds=new DatagramSocket(5000);
DatagramPacket dp=new DatagramPacket(b,1024);
DataInputStream dis=new DataInputStream([Link]);
[Link]("Enter file name\n");
String str1;
str1=[Link]();
b=[Link]();
[Link](new DatagramPacket(b,[Link],[Link](),5001));
[Link]("Contents of file:"+str1+"\n");
while(str1!=null)
{
[Link](dp);
str1=new String([Link](),0,[Link]());
[Link](str1);
}
[Link]();
}
}
[Link]:
import [Link].*;
import [Link].*;
class udpServer
{
public static void main(String args[])throws IOException
{
byte b[]=new byte[1024];
DatagramSocket ds=new DatagramSocket(5001);
DatagramPacket dp=new DatagramPacket(b,1024);
[Link](dp);
String str=new String([Link](),0,[Link]());
[Link]("Received file "+str);
FileReader fr=new FileReader(str);
BufferedReader br1=new BufferedReader(fr);
while((str=[Link]())!=null)
{
b=[Link]();
[Link](new DatagramPacket(b,[Link],[Link](),5000));
}
[Link]();
}
}
[Link]:
import [Link].*;
import [Link].*;
/**
* This program connects to a Web server and downloads the specified URL
* from it. It uses the HTTP protocol directly.
**/
public class HttpClient {
public static void main(String[] args) {
try {
// Check the arguments
if (([Link] != 1) && ([Link] != 2))
throw new IllegalArgumentException("Wrong number of arguments");
// Get an output stream to write the URL contents to
OutputStream to_file;
if ([Link] == 2) to_file = new FileOutputStream(args[1]);
else to_file = [Link];
// Now use the URL class to parse the user-specified URL into
// its various parts: protocol, host, port, filename. Check the protocol
URL url = new URL(args[0]);
String protocol = [Link]();
if ()
throw new IllegalArgumentException("URL must use 'http:' protocol");
String host = [Link]();
int port = [Link]();
if (port == -1) port = 80; // if no port, use the default HTTP port
String filename = [Link]();
// Open a network socket connection to the specified host and port
Socket socket = new Socket(host, port);
// Get input and output streams for the socket
InputStream from_server = [Link]();
PrintWriter to_server =
new PrintWriter(new OutputStreamWriter([Link]()));
// Send the HTTP GET command to the Web server, specifying the file.
// This uses an old and very simple version of the HTTP protocol
to_server.println("GET " + filename);
to_server.flush(); // Send it right now!
// Now read the server's response, and write it to the file
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = from_server.read(buffer)) != -1)
to_file.write(buffer, 0, bytes_read);
// When the server closes the connection, we close our stuff
[Link]();
to_file.close();
}
catch (Exception e) { // Report any errors that arise
[Link](e);
[Link]("Usage: java HttpClient <URL> [<filename>]");
}
}
}
[Link] FILE TRANSFER
import [Link]; import [Link]; public
interface FileInterface extends Remote { public byte[]
downloadFile(String fileName) throws RemoteException; }
import [Link].*;
import [Link].*;
import [Link];
public class FileImpl extends UnicastRemoteObject implements FileInterface {
private String name;
public FileImpl(String s) throws RemoteException {
super();
name = s;
}
public byte[] downloadFile(String fileName){
try {
File file = new File(fileName);
byte buffer[] = new byte[(int)[Link]()];
BufferedInputStream input = new
BufferedInputStream(new FileInputStream(fileName));
[Link](buffer,0,[Link]);
[Link]();
return(buffer);
} catch(Exception e){
[Link]("FileImpl: "+[Link]());
[Link]();
return(null);
}
} }
import [Link].*;
import [Link].*;
public class FileServer {
public static void main(String argv[]) {
try {
FileInterface fi = new FileImpl("FileServer");
[Link]("//[Link]/FileServer", fi);
} catch(Exception e) {
[Link]("FileServer: "+[Link]());
[Link]();
}
} }
import [Link].*;
import [Link].*;
public class FileClient{
public static void main(String argv[]) {
if([Link] != 2) {
[Link]("Usage: java FileClient fileName machineName");
[Link](0);
}try {
String name = "//" + argv[1] + "/FileServer";
FileInterface fi = (FileInterface) [Link](name);
byte[] filedata = [Link](argv[0]);
File file = new File(argv[0]);
BufferedOutputStream output = new BufferedOutputStream(new
FileOutputStream([Link]()));
[Link](filedata,0,[Link]);
[Link]();
[Link]();
} catch(Exception e) {
[Link]("FileServer exception: "+ [Link]());
[Link]();
}
} }
[Link] ARRAY SORT
import [Link].*;
import [Link].*;
public class strs{
public static void main(String args[])throws Exception
{
strimp st=new strimp();
[Link]("strs",st);
}
}
import [Link].*;
public interface strint extends Remote
{
int[] strev(int[] s)throws Exception;
}
import [Link].*;
import [Link].*;
import [Link].*;
public class strimp extends UnicastRemoteObject implements strint
{
public strimp()throws Exception{}
public int[] strev(int arr[]){
for(int i=0;i<[Link]-1;i++)
{ for(int j=i+1;j<[Link];j++){
if(arr[i]>arr[j])
{
int a=arr[i];
arr[i]=arr[j];
arr[j]=a;
}
}
}
return arr;
}
}
import [Link].*;
import [Link].*;
public class strc{
public static void main(String args[])throws Exception
{
int arr[]=new int[5];
BufferedReader r=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter any five numbers: ");
for(int i=0;i<5;i++)
arr[i]=[Link]([Link]());
String url="rmi://"+args[0]+"/strs";
strint st=(strint)[Link](url);
arr=[Link](arr);
for(int i=0;i<5;i++)
[Link](arr[i]);
}
}