RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 8992 | Replies: 4
Reply
Join Date: Jul 2005
Posts: 2
Reputation: actionwillspeak is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
actionwillspeak actionwillspeak is offline Offline
Newbie Poster

UDP File Transfer Question

  #1  
Jul 27th, 2005
Hello, I have a quesiton regarding my code. I thought it would be cool to try to implement the reliable UDP file transfer that everone does as a college student. I'm new to java, and im having some problems. Whenever i try to connect with the client, it refuses the port. Can anyone help me out with this or any of my missing code im working on?

Client:
import java.net.*;
import java.io.*;

public class FTPClient
{
private static String server = "localhost";
private static int servPort = 4525;

public static void main( String[] args )
{
sendInitialRequest();
receiveFileListing();
}

// send initial request to server for file listing
public static void sendInitialRequest()
{
//contact server on port 4525
//create DatagramPacket request for files

String initRequest = "send file listing";

try {
byte[] data = initRequest.getBytes( );
InetAddress addr = InetAddress.getByName( server );
DatagramPacket initPacket =
new DatagramPacket(data, data.length, addr, servPort );
DatagramSocket ds = new DatagramSocket( );
ds.send( initPacket );
ds.close( );
} catch ( IOException e ) {
System.out.println( e ); // Error creating socket
}

}

// receive file listing from server
public static void receiveFileListing()
{

}

// show user files to select from

// send selected list of files to server

// receive files from server
}

Server:
import java.net.*;
import java.io.*;

public class FTPServer
{

private static final int PORT = 4525;
private static DatagramSocket servSocket;
private static DatagramPacket inPacket;
private InetAddress[] addresses;
private static byte[] buffer;

public static void main( String[] args )
{
Listfiles files = new Listfiles();
// files needs to be package and sent to client

// setup the servers listening port
listening();

// process incoming request
do
{
// buffer is initialized in constructor
inPacket = new DatagramPacket( buffer, buffer.length );
try
{
// receive the request
servSocket.receive(inPacket);
}
catch( IOException e )
{
System.out.println( "Server IOException " + e );
}
processRequests();

} while ( true );

}

public FTPServer()
{
buffer = new byte[4096];

}

// setup "listening" port
public static void listening()
{
try
{
servSocket = new DatagramSocket(PORT);
}
catch(SocketException e)
{
System.out.println("Unable to attach to port!");
System.exit(1);
}
}

// process requests
// spawn new thread, create new port for communication channel
// and send file listing to client
// client information is in inPacket and buffer
public static void processRequests()
{
// need to keep track of clients
NewClient nc = new NewClient();

}

}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: UDP File Transfer Question

  #2  
Jul 27th, 2005
Hi everyone,

From what i see you seem to be doing everything right. Then again as you know that UDP packets are unreliable and sometimes fail to work and there is no garantee in the arrival of the packets. If i were you i would use raw sockets to connect to the server(make sure its multi-threaded for multi-client use) and on both the client and server use streams to transfer the data. Remember that one you disconnect a socket it cannot be reconnected thus you have to create a new socket.

But if you still want to use UDP you can check the below three threads on sending and receiving datagrams respectively and entire java world tip expanation on UDP packets

http://javaalmanac.com/egs/java.net/SendDatagram.html

http://javaalmanac.com/egs/java.net/...ram.html?l=rel

http://www.javaworld.com/javaworld/j...javatip40.html

I hoped this helped you

Yours Sincerely

Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
Reply With Quote  
Join Date: Jul 2005
Posts: 2
Reputation: actionwillspeak is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
actionwillspeak actionwillspeak is offline Offline
Newbie Poster

Re: UDP File Transfer Question

  #3  
Jul 27th, 2005
Thank you so much for your help. I'm just assuming no loss for the time being, and just working my way through it step by step. Thank you very much, and if anyone has any other good ideas keep em coming!

Originally Posted by freesoft_2000
Hi everyone,

From what i see you seem to be doing everything right. Then again as you know that UDP packets are unreliable and sometimes fail to work and there is no garantee in the arrival of the packets. If i were you i would use raw sockets to connect to the server(make sure its multi-threaded for multi-client use) and on both the client and server use streams to transfer the data. Remember that one you disconnect a socket it cannot be reconnected thus you have to create a new socket.

But if you still want to use UDP you can check the below three threads on sending and receiving datagrams respectively and entire java world tip expanation on UDP packets

http://javaalmanac.com/egs/java.net/SendDatagram.html

http://javaalmanac.com/egs/java.net/...ram.html?l=rel

http://www.javaworld.com/javaworld/j...javatip40.html

I hoped this helped you

Yours Sincerely

Richard West
Reply With Quote  
Join Date: Oct 2004
Location: On Earth, I think...
Posts: 246
Reputation: mmiikkee12 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

Re: UDP File Transfer Question

  #4  
Jul 30th, 2005
Well... if it's a FTP app...
1. I think FTP uses port 21.
2. You should probably start off with TCP/IP. It's VERY hard to rewrite an app to use something different after it already uses one thing.
Reply With Quote  
Join Date: Jul 2005
Posts: 56
Reputation: Sauce is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Sauce Sauce is offline Offline
Junior Poster in Training

Re: UDP File Transfer Question

  #5  
Jul 30th, 2005
For file transfer its better to use TCP instead of UDP because you can use a connection oriented paradigm (unless you are going to transfer only small files). Connection oriented has the overhead of creating the connection so for small files it will be slower then udp, but for larger files it will transfer faster, packets will arrive in order, and it will confirm that they all were received.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:09 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC