Example socket app doesn't work

前端 未结 1 838
粉色の甜心
粉色の甜心 2021-01-24 13:09

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

相关标签:
1条回答
  • 2021-01-24 13:30

    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.

    0 讨论(0)
提交回复
热议问题