logo
  • Home
  • About
  • Contact
  • My Forum
  • Privacy Policy
  • General
  • Technical
"Everything I did in my life that was worthwhile I caught hell for." -Earl Warren
delete
bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Client/Server Program

Posted by admin in General on 10 5th, 2009 | no responses

//for Server side

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

class FileManager
{
public static String getFileContent( String filename ) throws IOException
{
String content=”";

try
{
BufferedReader reader = new BufferedReader( new FileReader( filename ) );
String data;
while( ( data = reader.readLine() ) != null )
content = content + data + “\n”;
reader.close();
} catch( FileNotFoundException e)
{
System.out.println(”Request file does not exists”);
content = “-1″;
}
return content;
}
}

class Service implements Runnable
{
Thread t;
Socket client=null;

Service( Socket client )
{
this.client = client;
t = new Thread(this);
t.start();
}

public void run( )
{
try
{
DataInputStream din = new DataInputStream( client.getInputStream() );

DataOutputStream dout = new DataOutputStream( client.getOutputStream() );

String filename = din.readUTF();
System.out.println(”Requested file = ” + filename);

String data = FileManager.getFileContent( filename );

dout.writeUTF( data );
din.close();
dout.close();
}
catch( Exception e) {
System.out.println(e);
}
}
}

class ftpserver
{
public static void main( String v[] )
{
try
{
ServerSocket s = new ServerSocket( 2121 );
System.out.println(”Server started waiting for client request”);
System.out.println(”Ip address = ” + InetAddress.getLocalHost());
System.out.println(”Port address ( 2121 )”);

while( true )
{
new Service( s.accept() );
}
}
catch( Exception e) {
System.out.println(e);
}
}
}

//Code for Client Side

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

class ftpclient
{
public static void main( String v[] )
{
try{
Socket client = new Socket( “127.0.0.1″ , 2121 );

DataInputStream din = new DataInputStream( client.getInputStream() );

DataOutputStream dout = new DataOutputStream( client.getOutputStream() );

dout.writeUTF( v[0] ); // write filename to server

String content = din.readUTF(); // read content from the server

if( content.equals( “-1″))
{
System.out.println(”No such file exists at server”);
}
else
{
System.out.println( content );
}

}
catch( Exception e )
{
System.out.println(e);
}
}
}

//Executing Process

>javac ftpserver.java

>javac ftpclient.java

>java ftpserver

Open new Cmd prompt
——————–
>java ftpclient .java

Leave a Reply

Click here to cancel reply.

  • Sponsors
  • Recent Posts
  • Recent Comments
  • About
  • some concepts related with operating system
  • Friend
  • pani puri in rainy days
  • Client/Server Program
  • RMI program
  • Hello world!
  • Mr chandan Hi, this is a test comment....
About us
  • Blog Roll
  • archives

Blogroll

  • Drink Recipes
  • Food Recipes
  • Free SMS India
  • Indian Blogs
  • Jokes Blog
  • Kannada Portal
  • Kannada Songs
  • October 2009

categories

  • General
  • Technical

Search

  • Sponsors
  • Admin
advertisement advertisement
  • Log in
© Shwetha's Blog 2009 | chandan | kannada | directory | Indian Blogs | Powered by Wordpress | Elegant Themes