27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Networking<br />

http://developer.java.sun.com/developer/technicalArticles/InnerWorkings/Burrowing/index.html<br />

How can I let dial a phone number with a modem in a <strong>Java</strong> app.? Is there a way without a<br />

System.exec() call and without any M$ classes?<br />

Answer: You could use javax.comm to do it manually via the serial port and the good old AT<br />

command set on the modem. Alternatively, you might look at JTAPI, but that might have its<br />

own problems and a lot of overkill.<br />

Does it possible to have two thread running at the same time which reads from the same<br />

socket. If a message is received, does both threads then receive it?<br />

Answer: Two threads can read data from the same socket input stream, but they WON'T each<br />

get copies of the same data. They'll each get separate parts of the message.<br />

Q: how can I get an IP Adress in the class InetAdress? The constructor is private, so I can’t<br />

use it. I want to call the method getName () to get the domain name out of an IP Adress.<br />

Answer: It is not necessary to construct something :-)<br />

Just do it like this:<br />

for example: String hostname = InetAddress.getLocalHost().getHostName();<br />

I'm converting an old java client/server program which is based on raw byte stream heavily<br />

into new one which requires utilizing object streams. But if I open input/output object streams<br />

on both sides this blocks system and won't proceed...<br />

Hi,<br />

I'm converting an old java client/server program which is based on raw byte stream heavily<br />

into new one which requires utilizing object streams. But if I open input/output object streams<br />

on both side this blocks system and won't proceed.<br />

ObjectInputStream in = new ObjectInputStream(socket.getInputStream());<br />

ObjectOutputStream out = new<br />

ObjectOutputStream(socket.getOutputStream());<br />

Answer: Upon opening the ObjectInputStream, the constructor blocks to read a header from<br />

the stream (doing who-knows-what). Now, what happens is, both your client and server open<br />

the InputStream... and happily wait forever for the other side to send them the header they<br />

want. Deadlock guaranteed!<br />

The solution is simple: open the ObjectOutputStream first on at least one side, but better on<br />

both sides (usually, symmetry == good). Problem solved<br />

:)<br />

You are trying to keep two streams going at once independently, right.<br />

That means you need at least two threads at each end.<br />

Are there classes available to read and write to an RS 232 port or does this require using<br />

native code?<br />

Answer: See the <strong>Java</strong> Communications API.<br />

Q: Is there a System property that'll tell me the (TCP/IP) machine name? If not, how do<br />

you go about it?<br />

I need to display all the machine's TCP/IP addresses to the user. This requires a call to<br />

InetAddress.getAllByName(), which requires the machine name. You *can't* pass null or<br />

"localhost" to this method. Well, you can pass "localhost", but that only gets you the localhost<br />

file:///F|/350_t/350_tips/networking.htm (2 of 5) [2002-02-27 21:18:49]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!