Valhalla Legends Archive

Programming => General Programming => Java Programming => Topic started by: MoNksBaNe_Agahnim on September 26, 2004, 10:20 PM

Title: TCP/IP
Post by: MoNksBaNe_Agahnim on September 26, 2004, 10:20 PM
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
Title: Re: TCP/IP
Post by: TangoFour on September 27, 2004, 08:35 AM
A simple illustration:


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());
             
       }
}



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
Title: Re: TCP/IP
Post by: iago on September 27, 2004, 12:56 PM
Yeah, it's really as simple as that code there.

If you want to see a real example, have a look at JavaOp (http://javaop.clan-e1.net/content.pl?download)'s code.  But I took most of my connection code from the above tutorial.
Title: Re: TCP/IP
Post by: CrAzY on October 18, 2004, 07:14 PM
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.
Title: Re: TCP/IP
Post by: TangoFour on October 19, 2004, 04:25 PM
There are multiple options:

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



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()));



Or you can encapsulate the inputstream in another stream type:



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());



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
Title: Re: TCP/IP
Post by: Banana fanna fo fanna on October 19, 2004, 09:18 PM
I don't know about 1.5, but I strongly advise against using java.nio.
Title: Re: TCP/IP
Post by: TangoFour on October 20, 2004, 03:52 AM
Any particular reason why? It's working just fine with my bot, and I'm using 1.4.2
Title: Re: TCP/IP
Post by: iago on October 21, 2004, 12:28 PM
I would advise against using a Reader/Writer for a binary connection.  They try to interpret the stream and that just breaks stuff.

Title: Re: TCP/IP
Post by: TangoFour on October 21, 2004, 01:32 PM
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
Title: Re: TCP/IP
Post by: Banana fanna fo fanna on October 21, 2004, 02:15 PM
Quote from: TangoFour on October 20, 2004, 03:52 AM
Any particular reason why? It's working just fine with my bot, and I'm using 1.4.2

It's really buggy when going cross platform, and even in some of the nonblocking stuff, too.
Title: Re: TCP/IP
Post by: iago on October 21, 2004, 04:17 PM
Quote from: TangoFour on October 21, 2004, 01:32 PM
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

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.
Title: Re: TCP/IP
Post by: TangoFour on October 22, 2004, 04:05 AM
Quote from: iago on October 21, 2004, 04:17 PM

He also didn't ask for a non-binary connection. 

True
Title: Re: TCP/IP
Post by: The-FooL on October 24, 2004, 05:18 PM
Quote from: iago on October 21, 2004, 12:28 PM
I would advise against using a Reader/Writer for a binary connection.  They try to interpret the stream and that just breaks stuff.


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.
Title: Re: TCP/IP
Post by: iago on October 25, 2004, 12:05 PM
Quote from: The-FooL on October 24, 2004, 05:18 PM
Quote from: iago on October 21, 2004, 12:28 PM
I would advise against using a Reader/Writer for a binary connection.  They try to interpret the stream and that just breaks stuff.


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.

Me too :(