Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

An array or (ArrayList) of Sockets  RSS feed

 
King-Chieh Wang
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone!
I am having some problem with socket programming... In a typical server code, I do something as below: (sorry for malsyntax)

What happens is when I tried to use the sockets, and send a message or read a message, it gives me NullPointerException. However, if I do .InetAddress().hostAddress on the socket I retrived from the array, it actually displays correctly.
SO, my problem is, is there anyway I can store an array, or an arraylist of sockets, and pass them as parameters?
thanks in advance,
Jack
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a nitpick on syntax. This code:

Doesn't "initalize" an array. It declares it. You still need to create the array by doing something like this:

If your real code is similar to what you posted, this is what is causing your NullPointerException.
I'm not sure if you realize it or not, but calling serverSocket.accept() will wait for a client to connect, so in effect your loop will be preventing the first client from being processed until the 10th client connects. There's some good information on this topic in the Java Tutorial, Custom Networking Trail
[ March 11, 2004: Message edited by: Joe Ess ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!