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