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();

}

}

Recommended Answers

All 4 Replies

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/ReceiveDatagram.html?l=rel

http://www.javaworld.com/javaworld/javatips/jw-javatip40.html

I hoped this helped you

Yours Sincerely

Richard West

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!

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/ReceiveDatagram.html?l=rel

http://www.javaworld.com/javaworld/javatips/jw-javatip40.html

I hoped this helped you

Yours Sincerely

Richard West

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.