UDP Server and Client Thursday, Jan 7 2010 

Program for UDP Server and Client

Server Program:

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

public class Server
{

public static void main(String str1[]) throws Exception
{
DatagramSocket ds;
byte[] buf=new byte[1024];
ds=new DatagramSocket(200);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the String”);
InetAddress ia=InetAddress.getLocalHost();
(more…)

FTP Server and Client Thursday, Jan 7 2010 

FTP Program

Server Program:

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

public class Server
{
public static void main(String asd[]) throws Exception
{
ServerSocket ss=new ServerSocket(1024);
System.out.println(“ServerSocket Generated”);
Socket s=ss.accept();
System.out.println(“ServerSocket Accepted”);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintStream p=new PrintStream(s.getOutputStream());
String fname,str;
(more…)

TCP CLIENT/SERVER FOR ARITHMETIC OPERATION Sunday, May 4 2008 

TCP CLIENT

//arithtcpclient.java

import java.io.*;

import java.net.*;

public class arithtcpclient

{

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

{

System.out.println();

System.out.println(“ARITHMETIC CLIENT”);

System.out.println(“*****************”);

System.out.println(“Enter the host name to connect”);

String str;

DataInputStream inp=new DataInputStream(System.in);

str=inp.readLine();

Socket clientsoc = new Socket(str, 9);

System.out.println(“Enter the inputs”);

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

String userinput;

try

{

while (true)

{

do

{

userinput = stdin.readLine();

out.println(userinput);

}while(!userinput.equals(“.”));

System.out.println(“Sever Says : ” + in.readLine());

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

TCP SERVER

//arithtcpserver

import java.io.*;

import java.net.*;

public class arithtcpserver

{

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

{

System.out.println();

System.out.println(“ARITHMETIC SERVER”);

System.out.println(“*****************”);

System.out.println(“Server is ready to accept inputs…”);

ServerSocket serversoc=new ServerSocket(9);

Socket clientsoc = serversoc.accept();

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

String inputline;

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

try

{

while (true)

{

String s,op=””,st;

int i=0,c=0;

int[] a=new int[2];

while(true)

{

s=in.readLine();

if(s.equals(“+”) || s.equals(“-“) || s.equals(“*”) || s.equals(“/”))

op=s;

else if(s.equals(“.”))

break;

else

{

a[i]=Integer.parseInt(s);

i++;

}

}

if(op.equals(“+”))

c=a[0]+a[1];

else if(op.equals(“-“))

c=a[0]-a[1];

else if(op.equals(“*”))

c=a[0]*a[1];

else if(op.equals(“/”))

c=a[0]/a[1];

s=Integer.toString(c);

out.println(s);

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

Sample Output:

SERVER SIDE

*************

C:\jdk1.5\bin>javac arithtcpserver.java

C:\jdk1.5\bin>java arithtcpserver

ARITHMETIC SERVER

*********************

Server is ready to accept inputs…

CLIENT SIDE

************

C:\JDK1.5\bin>javac arithtcpclient.java

C:\JDK1.5\bin>java arithtcpclient

ARITHMETIC CLIENT

********************

Enter the host name to connect

p4-221

Enter the inputs

8

+

3

Sever Says : 11

9

11

Sever Says : -2

7

*

9

Sever Says : 63

12

/

3

Sever Says : 4

CLIENT/SERVER COMMUNICATION USING TCP SOCKETS Sunday, May 4 2008 

TCP CLIENT

//tcpclient.java

import java.io.*;

import java.net.*;

public class tcpclient

{

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

{

System.out.println(“TCP CLIENT”);

System.out.println(“Enter the host name to connect”);

DataInputStream inp=new DataInputStream(System.in);

String str=inp.readLine();

Socket clientsoc = new Socket(str, 9);

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

String userinput;

try

{

while (true)

{

System.out.println(“Sever Says : ” + in.readLine());

userinput = stdin.readLine();

out.println(userinput);

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

TCP SERVER

//tcpserver.java

import java.io.*;

import java.net.*;

public class tcpserver

{

public static void main(String a[]) throws Exception

{

System.out.println(“TCP SERVER”);

System.out.println(“Server is ready to connect…”);

ServerSocket serversoc=new ServerSocket(9);

Socket clientsoc = serversoc.accept();

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

String inputline;

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

try

{

while (true)

{

inputline = stdin.readLine();

out.println(inputline);

System.out.println(“Client Says : “+in.readLine());

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

Sample Output:

CLIENT-SERVER CHATTING USING TCP

*************************************

TCP SERVER

************

Server is ready to connect…

hello

Client Says : hello

How are you

Client Says : I’m doing programs

What programs

Client Says : networking

ok.Go ahead

Client Says : ok.Bye

Bye

TCP CLIENT

***********

Enter the host name to connect

p4-221

Sever Says : hello

hello

Sever Says : how are you

I’m doing programs

Sever Says : What programs

networking

Sever Says : ok.Go ahead

ok.Bye

Sever Says : Bye

Addition Server Thursday, May 1 2008 

//AddServer

import java.rmi.*;

public interface AddServer extends Remote

{

double add(double d1,double d2) throws RemoteException;

}

//Add Server Imp

import java.rmi.*;

import java.rmi.server.*;

public class AddServerImp extends UnicastRemoteObject implements AddServer

{

public AddServerImp() throws RemoteException

{

}

public double add(double d1,double d2) throws RemoteException

{

return d1+d2;

}

}

//Adds

import java.net.*;

import java.rmi.*;

public class Adds

{

public static void main(String args[])

{

try

{

AddServerImp addServerImp= new AddServerImp();

Naming.rebind(“Adds”,addServerImp);

}

catch(Exception e)

{

System.out.println(“Exception:”+e);

}

}

}

//AddClient

import java.rmi.*;

public class AddClient

{

public static void main(String args[])

{

try

{

String addServerURL=”rmi://localhost”+”/Adds”;

AddServer addserver=(AddServer)Naming.lookup(addServerURL);

System.out.println(“First number is:”+args[0]);

double d1=Double.valueOf(args[0]).doubleValue();

System.out.println(“Second number is:”+args[1]);

double d2=Double.valueOf(args[1]).doubleValue();

System.out.println(“The sum is:”+addserver.add(d1,d2));

}

catch(Exception e)

{

System.out.println(“Exception:”+e);

}

}

}