Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-24-2009, 12:48 PM
Member
 
Join Date: Apr 2009
Posts: 2
Rep Power: 0
agfre44_9873 is on a distinguished road
Default 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 );
}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-24-2009, 01:57 PM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 588
Rep Power: 1
RamyaSivakanth is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-25-2009, 02:49 AM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multithread Chat server/client gwaldarick Advanced Java 3 09-19-2009 01:22 AM
[SOLVED] UDP chat client server Koren3 Networking 2 04-25-2009 02:51 AM
Multi-user chat server and client 435.mahesh Java Announcements 6 04-25-2009 01:45 AM
P2P like Chat with central connection server, will work? MasterD Networking 1 03-23-2009 04:08 PM
simple chat server sari New To Java 0 02-06-2009 03:30 AM


All times are GMT +2. The time now is 02:54 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org