Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Using a socket array (Java)

Name: Anonymous 2007-06-23 13:07 ID:ghqoDeg/

I'm brand new to Java and have been working with it for, at the max, 3 hours. I've got a C background.
Anyway, I'm using it for a multi-threaded socket server and need to be able to access all the connected clients' sockets. I figured this would be achieved through creating a socket array and then assigning each socket object, as it's created, to whatever spot in the array is open. This part goes fine. However, when I try to access the methods of the socket, I get thrown a screenful of errors:
java.net.SocketException: socket closed
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(Unknown Source)
        at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at SocketThreader.run(SocketThreader.java:41)

I'm trying to use the objects with this:
for (int i = 0; i < MultiSocket.maxClients; i++)
{
    if (MultiSocket.clientsockets[i] != null && i != clientid)
    {
        PrintWriter tmpout = new PrintWriter(MultiSocket.clientsockets[i].getOutputStream(), true);
        tmpout.println("Remote msg: " + outputLine);
        tmpout.close();
    }
}

Any help on this would be appreciated, as I can't find any documentation on socket arrays.

Name: Anonymous 2007-06-23 13:10 ID:ghqoDeg/

Oops, here's the section that I assign the socket in.
for (int i = 0; i < MultiSocket.maxClients; i++)
{
    if (MultiSocket.clientsockets[i] == null)
    {
        MultiSocket.clientsockets[i] = socket;
        MultiSocket.clientparams[i][0] = "" + i;
        clientid = i;
        System.out.println("Set up clientsocket " + i);
        break;
    }
}

Name: Anonymous 2007-06-23 13:21 ID:ghqoDeg/

Nevermind. Figured it out.

Name: Anonymous 2007-06-23 13:27 ID:OXYSnvwD

I don't know java, but... java.net.SocketException: socket closed ?
I think that means that... the socket is closed.


.... did you open the sockets?

Name: Anonymous 2007-06-23 13:38 ID:ghqoDeg/

Yes, what was happening is I was trying to close the PrintWriter, but it closed the connection instead (tmpout.close()). Now it's working on all but the first client that connects but it's better than nothing.

Name: Anonymous 2007-06-23 13:45 ID:ghqoDeg/

And, fixed that by removing && i != clientid

Name: Anonymous 2007-06-23 14:28 ID:YDYfDmRc

If you are using a multi-threaded server why not let each thread handle just one socket.

Name: Anonymous 2007-06-23 14:40 ID:ghqoDeg/

Each thread is handling one socket. However, for my uses I need to be able to broadcast a message to all the sockets as well as store information for each socket that can be accessed by all of them.

Name: Anonymous 2007-06-23 18:33 ID:mRmDPP7h

I hope your daughter gets raped by negroes on cocaine.

Name: Anonymous 2007-06-23 21:43 ID:ghqoDeg/

Neat.
I've got another question.
How the hell can I check to see if a string is null?

Name: Anonymous 2007-06-23 23:17 ID:d2Qckfxb

public String isTheStringNull(String theString) {
if(theString == null) {
return "Yes, the string '"+theString+"' is null.";
}
else
{
return "No, the string is not null!";
}
}

Name: Anonymous 2007-06-24 3:35 ID:WOkO2AuW

Why not just check if the string is empty? Is it what you're really after, or do you really want to check against null?

String blah;
// ...
if (blah.equals("")) {
  System.out.println("blah is empty");
} else {
  System.out.println("blah is not empty");
}

Name: Anonymous 2007-06-24 3:47 ID:Heaven

>>10
memory is never empty.

Name: Anonymous 2007-06-24 17:52 ID:vTSRWAH9

>>12
.equals
Java is nasty and stupid.

Name: Anonymous 2007-06-24 19:52 ID:Heaven

JAVA IS AWESOME AND WIN

Name: Anonymous 2007-06-24 20:27 ID:Heaven

>>15
Lol no

Name: Anonymous 2009-01-14 14:38

YOU ARE NOW THINKING MANUALLY

Name: Anonymous 2009-03-06 8:36

The 2 html is   the active frame   or something because   of the lack   of those fucking   Camls and their   Black Egyptian aeronauts!

Name: Anonymous 2011-02-26 22:44

dicks

Don't change these.
Name: Email:
Entire Thread Thread List