Results 1 to 3 of 3
- 04-24-2009, 11:48 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
Server.java Errors trying to build a chat
I'm new to this stuff. Im trying to build a chat room. a have a file called server.java. I keep coming across this error when running javac Server.java...
Note: Server.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
I don't think I have the path set right. Im having problems with all the other chat files. Mostly error....
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Server.main(Server.java:92)
Again, I really don't know what's wrong. I intall the vrtual machine under Program File-> java. in the java folder I have jdk1.6.0_13 folder, jre1.6.o folder, and jre6 folder. How to I SET PATH and what do I set it with? Is Server.java, Client.java, ClientApplet.java, ServerThread.java suppose to be inside the java folder?
Here's the code for Server.java. If anyone likes the rest contact me. I'll send them to you.
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
// The ServerSocket we'll use for accepting new connections
private ServerSocket ss;
// A mapping from sockets to DataOutputStreams. This will
// help us avoid having to create a DataOutputStream each time
// we want to write to a stream.
private Hashtable outputStreams = new Hashtable();
// Constructor and while-accept loop all in one.
public Server( int port ) throws IOException {
// All we have to do is listen
listen( port );
}
private void listen( int port ) throws IOException {
// Create the ServerSocket
ss = new ServerSocket( port );
// Tell the world we're ready to go
System.out.println( "Listening on "+ss );
// Keep accepting connections forever
while (true) {
// Grab the next incoming connection
Socket s = ss.accept();
// Tell the world we've got it
System.out.println( "Connection from "+s );
// Create a DataOutputStream for writing data to the
// other side
DataOutputStream dout = new DataOutputStream( s.getOutputStream() );
// Save this stream so we don't need to make it again
outputStreams.put( s, dout );
// Create a new thread for this connection, and then forget
// about it
new ServerThread( this, s );
}
}
// Get an enumeration of all the OutputStreams, one for each client
// connected to us
Enumeration getOutputStreams() {
return outputStreams.elements();
}
// Send a message to all clients (utility routine)
void sendToAll( String message ) {
// We synchronize on this because another thread might be
// calling removeConnection() and this would screw us up
// as we tried to walk through the list
synchronized( outputStreams ) {
// For each client ...
for (Enumeration e = getOutputStreams(); e.hasMoreElements(); ) {
// ... get the output stream ...
DataOutputStream dout = (DataOutputStream)e.nextElement();
// ... and send the message
try {
dout.writeUTF( message );
} catch( IOException ie ) { System.out.println( ie ); }
}
}
}
// Remove a socket, and it's corresponding output stream, from our
// list. This is usually called by a connection thread that has
// discovered that the connectin to the client is dead.
void removeConnection( Socket s ) {
// Synchronize so we don't mess up sendToAll() while it walks
// down the list of all output streamsa
synchronized( outputStreams ) {
// Tell the world
System.out.println( "Removing connection to "+s );
// Remove it from our hashtable/list
outputStreams.remove( s );
// Make sure it's closed
try {
s.close();
} catch( IOException ie ) {
System.out.println( "Error closing "+s );
ie.printStackTrace();
}
}
}
// Main routine
// Usage: java Server <port>
public static void main( String args[] ) throws Exception {
// Get the port # from the command line
int port = Integer.parseInt( args[0] );
// Create a Server object, which will automatically begin
// accepting connections.
new Server( port );
}
}
- 04-24-2009, 12:57 PM #2
Hi,
My first question here is "Have you passed any argument to this class?"
without passing argument if u try to use,then only u will get "ArrayindexOutOfBoundsException".
-Regards
Ramya
- 04-25-2009, 01:49 AM #3
Read the error messages. You are indexing beyond the end of an array in line 92 (because you haven't passed any arguments, or checked for this case).
Do as the message says and compile with -Xlint:unchecked to find out the other bad stuff you're doing.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
Similar Threads
-
Multithread Chat server/client
By gwaldarick in forum Advanced JavaReplies: 3Last Post: 09-19-2009, 12:22 AM -
[SOLVED] UDP chat client server
By Koren3 in forum NetworkingReplies: 2Last Post: 04-25-2009, 01:51 AM -
Multi-user chat server and client
By 435.mahesh in forum Java SoftwareReplies: 6Last Post: 04-25-2009, 12:45 AM -
P2P like Chat with central connection server, will work?
By MasterD in forum NetworkingReplies: 1Last Post: 03-23-2009, 03:08 PM -
simple chat server
By sari in forum New To JavaReplies: 0Last Post: 02-06-2009, 02:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks