Sunday, 3 April 2011

SOCKET PROGRAMMING

Socket programming and Rmi programming is important part of Internet Technology, Its weight-age is about 16-24 marks with each question for 8 marks is fixed. One can easily score good marks in I.T by practicing Socket and Rmi Programs.
Note: All Rmi and Socket Programs are almost same only the logical part changes i.e 95% of all your programs is same coding that is a real advantage for students.

Here are the Few samples:
TCP Socket:
Client side

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

class EchoClient
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("127.0.0.1",1099);
BufferedReader kbin = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintStream out = new PrintStream(sock.getOutputStream());
while(true)
{
System.out.print("Enter a msg : ");
String msg = kbin.readLine();
out.println(msg);
if(msg.equals("bye"))
break;
System.out.println(in.readLine());
}
sock.close();
}
}
Server Side:

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

class EchoServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(1099);
while(true)
{
Socket sock = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintStream out = new PrintStream(sock.getOutputStream());
while(true)
{
String msg = in.readLine();
if(msg.equals("bye"))
break;
out.println("Server says : "+msg);
}
sock.close();
}
}
}

Rmi(Remote Method Invocation):Program for String Reverse

Create Interface
import java.rmi.*;

public interface strrev extends Remote
{
String rev(String ip) throws RemoteException;
}
Implementation
import java.rmi.*;
import java.rmi.server.*;

public class strrevimpl extends UnicastRemoteObject implements strrev 
{
public strrevimpl() throws RemoteException
{
super();
}
public String rev(String ip) throws RemoteException
{
StringBuilder sb = new StringBuilder(ip);
String s = (sb.reverse()).toString();
return(s);
}
}
Client Side


import java.rmi.*;

class  strclient
{
public static void main(String ar[]) throws Exception
{
strrev sr = (strrev)Naming.lookup("rmi://localhost/strreverse");
System.out.println("Reverse of hello is : "+sr.rev("hello"));
}
}
Server Side


import java.rmi.*;

class strserver
{
public static void main(String ar[]) throws Exception
{
strrevimpl s = new strrevimpl();
Naming.rebind("rmi://localhost/strreverse",s);
System.out.println("Object successfully created");
}
}

This were the sample programs one can contact me if any doubt or for rest of programs if required.

1 comment:

  1. I was looking for this information through Google then i found this blog, really great knowledge you have shared here.domain registration india

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...