Nikitin Mikhail
Nikitin Mikhail

Reputation: 3019

Example socket app doesn't work

I'm studying java.net and trying to make a simple app. here is the code:

EDIT: My fault, sorry, but the main problem still exists. Here is corrected code:

public static void main(String args[]){
        try{

            Socket s = new Socket("localhost", 3128);
            args[0] = args[0]+"\n"+s.getInetAddress().getHostAddress()
                    +":"+s.getLocalPort();
            s.getOutputStream().write(args[0].getBytes());

            byte buf[] = new byte[64*1024];
            int r = s.getInputStream().read(buf);
            String data = new String(buf, 0, r);

            System.out.println(data);
        } catch(Exception e){
            System.out.println("init error: "+e);
        }
    }

But when I'm trying to start it it gives me the following error:

init error: java.lang.ArrayIndexOutOfBoundsException: 0

what is the possible reason of this?

UPD: here is the rest trace:

/usr/lib/jvm/jdk1.7.0_13/bin/java -Didea.launcher.port=7535 -Didea.launcher.bin.path=/home/mikhail/Загрузки/idea-IU-123.155/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/jdk1.7.0_13/jre/lib/jce.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/jfr.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/deploy.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/rt.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/javaws.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/jfxrt.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/resources.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/jsse.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/management-agent.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/plugin.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/charsets.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/ext/dnsns.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/ext/sunec.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/ext/zipfs.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/ext/localedata.jar:/usr/lib/jvm/jdk1.7.0_13/jre/lib/ext/sunjce_provider.jar:/home/mikhail/bzrrep/DLP/producer/target/classes:/home/mikhail/.m2/repository/commons-lang/commons-lang/2.3/commons-lang-2.3.jar:/home/mikhail/.m2/repository/org/apache/pdfbox/pdfbox/1.8.0/pdfbox-1.8.0.jar:/home/mikhail/.m2/repository/org/apache/pdfbox/fontbox/1.8.0/fontbox-1.8.0.jar:/home/mikhail/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar:/home/mikhail/.m2/repository/org/apache/pdfbox/jempbox/1.8.0/jempbox-1.8.0.jar:/home/mikhail/Загрузки/idea-IU-123.155/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain com.messagedna.dlp.main.Main

Upvotes: 1

Views: 191

Answers (1)

Andreas Fester
Andreas Fester

Reputation: 36630

what is the possible reason of this?

The reason for the ArrayIndexOutOfBoundsException is that you have not passed any arguments to your program. Hence, args[0] does not refer to a valid array index (btw: I would create a separate local variable instead of assigning to the args[] array. Technically it is possible, but I would not consider this "good style").

When you pass a parameter to your program, you will get a NullPointerException since you have not configured your Socket. You need, at least, connect it to some server, e.g. like this:

s.connect(new InetSocketAddress("myserver.example.com", 1234));

Then, when there is an appropriate server running at port 1234 at "myserver.example.com", you will be able to send and receive some data.

For more information about network programming in Java, you should go through the Sockets tutorial.

Upvotes: 2

Related Questions