Java Sockets

Associate
Joined
14 Oct 2008
Posts
416
I'm currently writing a really basic server/client transfer application in Java to learn about sockets (text files only) but I'm having a few problems.

I've got to the stage where a file will transfer from the client to the sever, but if I try and send another file after the first one, it won't work. No errors, just doesn't seem to do anything. I have to quit the application and start it again to send the next file.

The code's quite hefty so I can't easily paste it in. I was really just wondering if there's anything obvious I don't know about with regard to sockets which could be causing this? I thought I'd found the solution when I came across "flushing" via google but it didn't seem to do the trick unfortunately :(

Any ideas?
 
Can you not upload the code to a URL so we can take a peek?

Whats your code like for handling the file transfer? Sounds like its in your code handler for the file upload, what does it do at the end of the first transfer?
 
Check that you are succesfully closing the socket connections correctly.

EDIT : Or that you are closing them, and you don't mean to :)
 
Cheers for the responses guys.

Can you not upload the code to a URL so we can take a peek?

Whats your code like for handling the file transfer? Sounds like its in your code handler for the file upload, what does it do at the end of the first transfer?
I stripped everything down yesterday and I'm just separating it all into classes to try and make things more readable and get my head round it.

If I'm still having problems after that, I'll upload it and send a link. It's a bit pointless uploading it at the minute because there's bits not working which I'm not having problems with.

I'm pretty sure it's a case of me closing connections before I should be (as Shoseki said). It's an "ftp-like" protocol and I've been reading up on how ftp actually works. I'm going to try using a control connection and a data connection and hopefully that'll resolve the issue.

I'll let you know how it goes.
 
Last edited:
OK, I've finally got round to spending a bit more time on this and sorted the problem. Just separated everything into different classes and set up another socket, one for data and one for control and it's much easier to keep track of everything (for me anyway, although the code is still a bit messy).

The problem I've got now is, I'm not sure how the Java classes handle buffer management etc. I was speaking to a lecturer at uni and she said in C you'd need to handle all the buffer management yourself, but wasn't sure about how much Java did for you.

Does anyone know?

Here's my code to see how I've done it. I just used BufferedReader/Writer and InputStreamReader/Writer.

http://www.ben-thomas.org/stuff/FTP.zip
 
Last edited:
Not had a thorough check, but don't forget to flush the BufferedWriter before you close it

Code:
  public void receive(String file) {
        try {
            FileWriter fstream = new FileWriter(file);
            BufferedWriter toFile = new BufferedWriter(fstream);
            String line = null;

            while ((line = getIn().readLine()) != null) {
                toFile.write(line + "\r\n");
            }
            [B]toFile.flush();[/B]      
            toFile.close();
        } catch (IOException ioe) {
            System.out.println("Error receiving file.");
        }
    }
 
Back
Top Bottom