Valhalla Legends Forums Archive | Java Programming | TCP/IP

AuthorMessageTime
MoNksBaNe_Agahnim
Hey,

been gone for awhile due to school and work and in the absence I switched from C++ and Java, currently working under netbeans. Was wondering if anyone knows tutorials on doing stream socket connections using Java, googled but couldn't seem to find a half-way decent one. Thanks for any help
September 27, 2004, 3:20 AM
TangoFour
A simple illustration:

[code]
import java.net.*;
import java.io.*;

public class Demo
{
      public Demo(String server, int port)
      {
              Socket s = new Socket(server, port);
              DataInputStream data_in = new DataInputStream(s.getInputStream());
              DataOutputStream data_out = new DataOutputStream(s.getOutputStream());
             
      }
}

[/code]

Perhaps this helps, since you know C++ it shouldn't be a problem, but it might be a bit too basic

http://java.sun.com/docs/books/tutorial/networking/sockets/

I should also note that my above code does not catch Exceptions
September 27, 2004, 1:35 PM
iago
Yeah, it's really as simple as that code there.

If you want to see a real example, have a look at JavaOp's code.  But I took most of my connection code from the above tutorial.
September 27, 2004, 5:56 PM
CrAzY
You use the Socket class and create a new socket.  java.net* is the import i believe?  Also make 2 more varibles for your data in/out.  There are special Classes for these varibles but can't think of them off the top of my head.
October 19, 2004, 12:14 AM
TangoFour
There are multiple options:

You could use a Reader and Writer to read from the socket:

[code]

import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

...

Socket s = new Socket("url", port);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

[/code]

Or you can encapsulate the inputstream in another stream type:

[code]

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

...

Socket s = new Socket("useast.battle.net", 6112);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());

[/code]

Whichever suits you more - another approach is using the java.nio package, and use Buffers and Channels (which my bot does) - but this is a little bit more complicated
October 19, 2004, 9:25 PM
St0rm.iD
I don't know about 1.5, but I strongly advise against using java.nio.
October 20, 2004, 2:18 AM
TangoFour
Any particular reason why? It's working just fine with my bot, and I'm using 1.4.2
October 20, 2004, 8:52 AM
iago
I would advise against using a Reader/Writer for a binary connection.  They try to interpret the stream and that just breaks stuff.

October 21, 2004, 5:28 PM
TangoFour
The original poster didn't ask for a Binary Connection in specific.

In that case I'd either choose a DataInputStream and DataOutputStream, or my current setup with a SocketChannel and ByteBuffer
October 21, 2004, 6:32 PM
St0rm.iD
[quote author=TangoFour link=topic=8898.msg85325#msg85325 date=1098262364]
Any particular reason why? It's working just fine with my bot, and I'm using 1.4.2
[/quote]

It's really buggy when going cross platform, and even in some of the nonblocking stuff, too.
October 21, 2004, 7:15 PM
iago
[quote author=TangoFour link=topic=8898.msg85524#msg85524 date=1098383546]
The original poster didn't ask for a Binary Connection in specific.

In that case I'd either choose a DataInputStream and DataOutputStream, or my current setup with a SocketChannel and ByteBuffer
[/quote]

He also didn't ask for a non-binary connection.  I spent a very long time trying to figure out why I couldn't download .jpg files over http on every computer, and it turned out it was because I was using a InputReader instead of a InputStream.  I'd recommend that everybody watches out for that, it's a pain to track down.

Yes, DataInputStream would also work well.
October 21, 2004, 9:17 PM
TangoFour
[quote author=iago link=topic=8898.msg85556#msg85556 date=1098393478]

He also didn't ask for a non-binary connection. 
[/quote]

True
October 22, 2004, 9:05 AM
The-FooL
[quote author=iago link=topic=8898.msg85518#msg85518 date=1098379723]
I would advise against using a Reader/Writer for a binary connection.  They try to interpret the stream and that just breaks stuff.

[/quote]

Ha.  I spent hours upon hours trying to figure out what was wrong with my code, and all along I had been using an InputStreamReader.
October 24, 2004, 10:18 PM
iago
[quote author=The-FooL link=topic=8898.msg85961#msg85961 date=1098656295]
[quote author=iago link=topic=8898.msg85518#msg85518 date=1098379723]
I would advise against using a Reader/Writer for a binary connection.  They try to interpret the stream and that just breaks stuff.

[/quote]

Ha. I spent hours upon hours trying to figure out what was wrong with my code, and all along I had been using an InputStreamReader.
[/quote]

Me too :(
October 25, 2004, 5:05 PM

Search