• Welcome to Valhalla Legends Archive.
 

TCP/IP

Started by MoNksBaNe_Agahnim, September 26, 2004, 10:20 PM

Previous topic - Next topic

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

TangoFour

#1
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

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.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


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.
CrAzY

TangoFour

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

Banana fanna fo fanna

I don't know about 1.5, but I strongly advise against using java.nio.

TangoFour

Any particular reason why? It's working just fine with my bot, and I'm using 1.4.2

iago

I would advise against using a Reader/Writer for a binary connection.  They try to interpret the stream and that just breaks stuff.

This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


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

Banana fanna fo fanna

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.

iago

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.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


TangoFour

Quote from: iago on October 21, 2004, 04:17 PM

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

True

The-FooL

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.

iago

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 :(
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*