• Post Reply Bookmark Topic Watch Topic
  • New Topic

Client acting as server using multithreading for bittorrent  RSS feed

 
Anand D
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, My college project is a minature version of bittorrent where i need the client to act like a server ie.giving the file and taking the file at the same time from other clients..... Using Multithreading.....

Will it have some conflict........how can i implement it??
 
Karunakar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I am posting sample Client,Server programs.In this Client reads from a file and sends it to the Server.The Server program just prints that on the console.I have used sockets.threads was not necessary in this thing.
You can use Threads if you want to run more than one process at the Client/Server.

If you have any query u can contact me @->karan_nitk@rediffmail.com, Karunakar.h.gowda@verizon.com

Hope this helps you

How to run:
-----------
1.Compile Server.java
2.Compile Client.java
3.run Server
4.run Client (c:\jdk\bin\java Client file.txt)

Server program
--------------
/ Java program for the Server side

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


class Server
{
static Socket sk;
static ServerSocket ssock;
static BufferedReader input;
//static PrintWriter pw;


public static void main(String ar[])
{


try
{

ssock=new ServerSocket(1051,5);//create server object

sk=ssock.accept(); //Waiting for the client

//Now the client is connected to Server
System.out.println("Connect to client "+sk);

//To get data from client
input= new BufferedReader(new InputStreamReader(sk.getInputStream()));



//Get the data from the client

while(true)
{
if(input.ready())
{
String line=input.readLine();
System.out.println("Client >> "+line);
}

}


}
catch(Exception ex)
{
System.out.println("Exception :"+ex.getMessage());
System.exit(0);
}


} //End of main


} //End of Server class



Client Program
----/ Java program for the client side

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


class Client
{
static Socket sk;
static PrintWriter pw;


public static void main(String args[])
{


try
{


InetAddress ia=InetAddress.getLocalHost();

//Create the socket object,this will esatblish connection between the client and server

sk=new Socket(ia,1051);

System.out.println("Connected to Server.... ");

//To send data to server
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sk.getOutputStream())),true);





String fname=args[0];
File iFile = new File(fname);
RandomAccessFile raf = new RandomAccessFile(iFile,"r");

String line;
while((line=raf.readLine())!=null)
{
//System.out.println(str);
pw.println(line);
}




}
catch(Exception ex)
{
System.out.println("Exception :"+ex.getMessage());
System.exit(0);
}


} //End of main


} //End of client class




-------------
Karunakar K.H.
karan_nitk@rediffmail.com
karunakar.h.gowda@verizon.com


karunakar.h.gowda@verizon.com
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!