PDA

Click to See Complete Forum and Search --> : Socket Class.


Nove
Jun 18th, 2005, 09:36 PM
How do sockets work in java? I have only previously been exposed to the VB winsock control, which receives data automatically as an event. From a tutorial i've seen, it looks like all incoming data has to be manually read using a bufferedreader, perhaps using a continuous loop, is that how it goes?

CornedBee
Jun 19th, 2005, 03:07 AM
That's one way. The other way is using the New I/O channels to poll which sockets have data available.

Dillinger4
Jun 19th, 2005, 01:44 PM
The URL class can be used also. All you do is invoke openStream() on an instance of a URL to get an InputStream which can then be used to read the contents of the URL. You can also invoke openConnection() on A URL instance to get an URLConnection so you can read/write.

For lower level communication i would use Sockets though. Say if you were writing a client-sever appliction and you wanted to implement some sort of custom protocol.

Nove
Jun 19th, 2005, 04:52 PM
Yes, I'm writing a client-server application. The server is actually in VB, I just need to get a working client apllication going. I'm only sending and receiving simple text strings, so which method would work best, and can you give me a code example?

System_Error
Jun 19th, 2005, 07:42 PM
I can't give you a code example, but I would go with polling the sockets as CB suggested. I tried making a simple chat program and looping continously readinng from the socket even if it was null was very slow and usually locked up the application.

Dillinger4
Jun 19th, 2005, 09:23 PM
Nove why not just write the client in VB? You already have the server side code written. I wouldn't add to the complexity by writing a cross language program until i knew exactly both sides.

Nove
Jun 19th, 2005, 11:55 PM
Well, I have written the client in VB, but it'll be much easier on the user if they can just log on through a web browser. This also gives me a good chance to get some Java learning in.

ccoder
Jun 21st, 2005, 08:55 AM
Here is a small applet that I wrote for one of my web pages for our Help Desk. It may be capable of doing a bit more than you intend, but it should get you started.

It 'talks' to a C server, but I have done the same thing with VB. It isn't necessary, but I put the variables to be sent/redeived in classes that match the C struct or VB UDT. It's helpful when you dealing with very large complicated structures.

You will have to convert any Longs and Integers in your VB app to/from network-byte-order (big-endian). The read/write methods in the Java classes automatically do the conversions.