Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

outputstream over the net  RSS feed

 
Contra Kirby
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I got some code, and it is stinking up the server it inhabits. The foul odor is ridiculous:
(some preemptive notes:
fname is a TextField
screen is a class I made and screen.getMap() returns Color [][]
)

This compiles just fine, but depending on what OS I use I get different runtime errors. Basically, I want to write some bytes to a file on a server from an applet. The file that I want to write will be given in the TextField. If someone can merely tell me how to write to a file, I can figure out what to put into the file just fine...thanks in advance?
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Contra Kirby:

This compiles just fine, but depending on what OS I use I get different runtime errors.

Letting us know what the errors are is an essential step in the debugging process. One thing you will want to do is invoke flush() on the stream between writing to it and closing it. If you don't it may well chop off the data it hasn't already sent.
 
Contra Kirby
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error I got was "Connection refused: connect"
[ February 19, 2004: Message edited by: Contra Kirby ]
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class of the exception is important as well, since different exception classes signal different things going wrong. I'm going to guess you got a java.net.ConnectionException. The documentation says:

Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port).

Sounds like the server you are trying to connect to doesn't offer FTP. Try connecting with an FTP client. If you can connect with an FTP app, you may need authorization to access your user directory. Try sending your username and password in the URL like this: "ftp://username :p assword@ftp.blah.com. . ."
[ February 19, 2004: Message edited by: Joe Ess ]
 
Contra Kirby
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well finally I have a stack trace:

The code leading up to line 77 in SpriteBuilder.saveSprite():
(note that where name and pass are I have my real name and pass for this server)

I tried that ftp://username: password@compy.host.edu/game/sprites but it didn't work. This server does have password protection and I don't even think it allows regular ftp, which is probably a problem. I tried http before but it doesn't let me create a file on the server. This is all very aggrivating and I thought I made it fairly clear what I want to do, but no one is giving me much to go on here. At any rate, once this works...

...hahahaha!
[ February 20, 2004: Message edited by: Contra Kirby ]
[ February 20, 2004: Message edited by: Contra Kirby ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First and foremost:

Never, ever, ever do this. Ever. Hiding errors is Wrong™. Empty catch blocks are EEEVIL™. If you catch an exception, log it somehow. Preferably with java.util.logging or Log4J or something similar, but a quick simple alternative is to just use printStacktrace(). Or don't catch the exception at all, but declare it to be thrown; eventually the system's uncaught exception handler will catch it and print a stack trace for you. (Of course, the main thread my exit as a result...)
Next, to reiterate something Joe said - are you sure you can ftp at all to this server? Forget Java for a minute - go to a command prompt. Can you type "ftp servername", enter a user name and password, and get a connection?
As for your question: I believe you need to connect() the URLConnection after it's been opened. (Yeah, the names are counterintuitive.) But the idea is that you can only set request parameters like setDoOutput() before you've connected, and then you can call getOutputStream() after the connect(). So the solution may well be as simple as inserting one line:
 
Contra Kirby
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I did catch the exceptions, I just didn't feel like showing you guys the code. It involved some stupid trickery that I needed to do to see what printStackTrace() prints to the console (IE sucks), besides that code was working fine, I was only showing the problematic code.
ftp command does not work, however sftp does...I am once again at a loss of what I should do. I tried connecting to the url sftp://name:_pass@host.edu/~/public_html/game/sprites/a but got a malformedURL exception. The api really doesnt go into depth as to what kind of urls it accepts...
[ February 27, 2004: Message edited by: Contra Kirby ]
 
Contra Kirby
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't mean to be annoying, but I was really hoping for a solution to my problem.
 
Caius Jard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then you should try doing as you are told..

C:\Documents and Settings\admin>telnet neverland.ncssm.edu 21
Connecting To neverland.ncssm.edu...Could not open connection to the host, on port 21: Connect failed
C:\Documents and Settings\admin>

the named server is not an FTP server
simple as that.
oh, and something else you indicated.. something about "console" and "IE sucks" - you wouldnt be trying to write an applet that FTPs to somewhere other than the server that it came from, would you? because that too.. is a big "no-possible" (without modifying the security restrictions
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He mentioned in his very first post, that this is an Applet.
sftp is listening on port 115 normally - don't know whether this helps, but without successfully transfering the data with an ftp-client, I wouldn't try with an applet.
Do you have permission to upload files to this server?
To publish applets?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!