Hi everyone...
i am currently doing a java system concerning file transfers between server and client... file transfer starts from the server.. So far, i have done one file transfer. However, i would like to program my code in such a way that the client actively still listens to the server after a single file transfer.. also i need the client to run the files it receives in an array form. Its my final year project and i was hoping to get some help for my code. Please help as i would really appreciate it...=) Thank you in advance...=)
Here is my client code:
import java.io.* ;
import java.net.* ;
import java.util.* ;
import java.lang.*;
//import javax.swing.* ;
/*
* Charity.java is the client-side implementation of the Prototype project, the purpose of which is to encourage
* further understanding of the processes, methods and steps involved in making a network-capable program. It also
* serves to clarify existing knowledge of the primitive datatype sizes and how Java deals with signed bytes in
* networked applications (such as the raw IP address byte array in InetAddress).
*
* The program will query the user as to which IP address the user wants to deliver UDP packets. Only pcs with the
* server-side implementation Goodwill.java running will reply to this packet. It sends a packet to a specified IP
* at port 4445, the content of which is empty (carries only 256 bytes of uninitialized data). It will then
* wait for a reply and print out its contents in a string.
*
* Run this program as "Run File". Both the server and the client programs have their own main method which will
* conflict if "Run Project" is selected.
*
*/
class Emissary
{
public static void main (String args []) throws IOException
{
byte iptargetpc[] = new byte[4] ;
iptargetpc[0] = -64 ;
iptargetpc[1] = -88 ;
iptargetpc[2] = 1 ;
iptargetpc[3] = 88 ;
InetAddress targetaddr = InetAddress.getByAddress (iptargetpc) ;
/// Client implementation
/// Get response on unicast port 4446
final int PORT_USED = 4446 ;
DatagramSocket socket = new DatagramSocket (4446) ;
DatagramPacket packet ;
byte buffer [] = new byte [256] ;
InetAddress server_ip;
String file_name = null;
while (true)
{
new Primaria().start() ; /// Get response on Multicast address 230.5.5.4 port 54100
packet = new DatagramPacket(buffer, buffer.length) ;
System.out.print ("\nNow waiting for unicast reply... \n") ;
socket.receive (packet) ; // This method blocks until it receives a packet
/// Display response
String received = new String (packet.getData(), 0, packet.getLength ()) ; /// String (byte[] bytes, int offset, int length)
server_ip = packet.getAddress();
System.out.print ("Server reply unicastly : " + received + "\n") ;
if (received.startsWith("--COPY--") == true)
{
System.out.println ("The server has sent a copy command. Going into copy mode and reply ACK \n") ;
String ack = new String("ACK") ;
byte ack_response[] = ack.getBytes () ;
file_name = received.substring (9);
packet.setData (ack_response) ;
packet.setAddress (packet.getAddress()) ;
packet.setPort (packet.getPort()) ;
socket.send (packet) ;
System.out.println ("Waiting for final confirmation before going in copy mode") ;
socket.setSoTimeout (5000) ; // Sets the timeout for the receive command so not to wait indef
try
{
socket.receive (packet) ;
if (new String(packet.getData()).startsWith ("ACK") == true)
{ /// Transmission mode
System.out.println ("Confirmation confirmed. Transmit Mode active") ;
System.out.println ("File name is '" + file_name) ;
}
else
throw new SocketTimeoutException() ;
}
catch (SocketTimeoutException se)
{
System.out.println ("The server did not reply to confirmation within 5 secs, terminating mode") ;
}
int filesize=6022386; // filesize temporary hardcoded
long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
Socket sock = new Socket(server_ip,13267);
System.out.println("Connecting...");
// receive file
byte [] mybytearray = new byte [filesize];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("Guitar1.flv");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0)
current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
sock.close();
}
socket.close() ;
String command = new String("cmd /c start " + file_name);
// Runtime run = Runtime.getRuntime();
// Process proc = run.exec("fyp2.ppsx");
// Runtime.getRuntime().exec("cmd /c start " + file_name);
Runtime.getRuntime().exec(command);
}
}
}
class Primaria extends Thread
{
private static final int BUFFER_SIZE = 256 ;
private static final int PORT_USED = 54100 ;
private static final byte TTL_USED = (byte) 1 ;
byte iptargetmulti[] = new byte[4] ;
MulticastSocket comms_socket = new MulticastSocket(PORT_USED) ;
InetAddress multicast_inetadd ;
public Primaria () throws IOException
{
this ("Multicast Listener") ;
System.out.println ("\n\nEntering Multicast Listener Thread ") ;
}
public Primaria (String name) throws IOException
{
super (name) ; /// super refers to the Thread class and gives it a pretty name ;
iptargetmulti[0] = (byte) 230 ;
iptargetmulti[1] = (byte) 5 ;
iptargetmulti[2] = (byte) 5 ;
iptargetmulti[3] = (byte) 4 ;
comms_socket.setTimeToLive (TTL_USED) ;
}
public void run ()
{
byte mail_buffer[] = new byte[BUFFER_SIZE] ;
byte buffer_to_show[] ;
String mail_output ;
DatagramPacket incoming_packet = new DatagramPacket (mail_buffer, mail_buffer.length) ;
try
{
multicast_inetadd = InetAddress.getByAddress (iptargetmulti) ;
comms_socket.joinGroup (multicast_inetadd) ;
System.out.println ("\nNow waiting for multicast reply...") ;
comms_socket.receive (incoming_packet) ;
}
catch (IOException m)
{
System.out.println ("IOException error mark 1: " + m) ;
}
System.out.println ("Incoming message was : " + new String (mail_buffer)) ;
try
{
comms_socket.leaveGroup (multicast_inetadd) ;
comms_socket.close () ;
}
catch (IOException l)
{
System.out.println ("An error occured while leaving the group mark 2" ) ;
}
}
}
Thank you again... Hoping for a reply as soon as possible...=)
PS: i have uploaded my codes