This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

s.accept ( ) method, gives me an error ?

 
Ayman Jaffar
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I've a win98 OS and I don't recal installing any S/W for server.
I've got this program from a book and I want to know why it's not working.
I'm getting this message exactly:
C:\Delete>javac SimpleServer.java
SimpleServer.java:20: variable s might not have been initialized
Socket s1 = s.accept();
^
1 error

and here is my program:
public class SimpleServer {
public static void main(String args[]) {
ServerSocket s;
// Register service to port 5432 or anyother number
try {
s = new ServerSocket(5432);
} catch (IOException e) { }

while (true) {
try {
// Wait here and listen for a connection
Socket s1 = s.accept();
// Get output stream associated with the socket
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(s1out);
// Send your string!
dos.writeUTF("Hello Net World!");
// Close the connection, but not the server socket
dos.close();
s1.close();
} catch (IOException e) { }
} //end of while
}//end of main
}//end of class
Thanks alot
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which Version of the JDK are you using? The problem appears to be a condition called something like self referential error. It's a documented bug with certain versions. Basically the compiler sees that s may not get itialized in the constructor because it exists inside of braces and it complains. set s to null when you declare it
ServerSocket s = null;
or in the catch statement of the constructor prevents this.
One note, never have an empty catch block, this will mask errors and can be a horror to debug. a simple e.printStackTrace() will save a lot of hair!

Hope This Helps
 
Ayman Jaffar
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Carl Trusiak my program "SimpleServer.java" is now working becuase I set "ServerSocket s = null;"....exactly as you said !!
I've jdk 1.3 and I guess it is a bug.
I've just finished runnig both "SimpleClient.java" and "SimpleServer.java" and they're both workin' fine.
Thanks again.

[This message has been edited by Ayman Jaffar (edited January 09, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic