Results 1 to 15 of 15
- 09-27-2011, 03:05 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
New to Java and don't understand the underlying mechanisms of the following...
Hi,
I've been programming in .NET for a few years and recently started learning Java. As an exercise I want to remake a server-client application I made in C# a while ago, but I encounter problems that I don't understand. First some code:
When I call startListening, it works... the thread starts, but when stopListening is called, the program goes in the catch.Java Code:package sandsserver; import java.net.*; public class ConnectionsHandler { void startListening() { ServerSocketThread serverSocketThread = new ServerSocketThread(serverSocket); serverSocketThread.start(); } void stopListening() { try { serverSocket.close(); } catch(Exception ex) { System.out.println("ex: kan serverSocket niet sluiten"); } } private ServerSocket serverSocket; } //------------------------------------------------------------- package sandsserver; import java.net.*; public class ServerSocketThread extends Thread { private static final int PORT = 13713; ServerSocketThread(ServerSocket socket) { serverSocket = socket; } @Override public void run() { if(serverSocket != null) { serverSocket = null; } try { serverSocket = new ServerSocket(PORT); } catch(Exception ex) { System.out.println("ex: kan serverSocket niet creeeren"); } try { while(true) { System.out.println("in lus"); Socket incomingClient = serverSocket.accept(); } } catch(Exception ex) { System.out.println("ex: probleem in de lus"); } System.out.println("uit lus"); } private ServerSocket serverSocket; }
I tried some things and when I do the following it works:
So... can anyone explain me why this works now? I do the same thing (I think). The first one would be my favorite one because I don't want to keep a reference to the thread, I just need the reference to the socket.Java Code:ConnectionsHandler.java ... void startListening() { serverSocketThread = new ServerSocketThread(); ... } void stopListening() { try { serverSocketThread.getServerSocket().close(); } ... } private ServerSocketThread serverSocketThread; // I have a reference to the thread, where I had a reference to the socket ServerSocketThread.java ... //ServerSocketThread(ServerSocket socket) //{ //serverSocket = socket; //} // No constructor anymore, since the socket gets created in this class ServerSocket getServerSocket() { return serverSocket; } // I added this function to get access to the socket
Thank you in advance for an answer,
P.
- 09-27-2011, 03:45 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: New to Java and don't understand the underlying mechanisms of the following...
It probably would have helped you if you had done:
That would have told you exactly what exception had been thrown, thus possibly answering your question as to why it threw an exception.Java Code:void stopListening() { try { serverSocket.close(); } catch(Exception ex) { // System.out.println("ex: kan serverSocket niet sluiten"); ex.printStackTrace(); } }Last edited by Tolls; 09-27-2011 at 03:47 PM. Reason: Removed mark up in code.
- 09-27-2011, 05:00 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
Re: New to Java and don't understand the underlying mechanisms of the following...
The stacktrace let me know the serversocket has a nullreference. Very strange, because the socket does exist. I guess I can't close the socket from another thread than the one I created it. If anyone knows more about this, or had tips for me about this topic, please shoot. :-)
- 09-27-2011, 05:07 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: New to Java and don't understand the underlying mechanisms of the following...
No it doesn't exist.
You pass a null socket into the ServerSocketThread class.
The serverSocket attribute of ConnectionsHandler is never anything other than null.
This means that the above constructor simply sets the serverSocket attribute in the ServerSocketThread object to null.Java Code:ServerSocketThread(ServerSocket socket) { serverSocket = socket; }
That attribute is later instantiated, but the one in ConnectionsHandler never is.
Indeed, even if that one was instantiated and then passed into the ServerSocketThread constructor, you would then (on run()) simply nullify it and create a new one:
Java Code:if(serverSocket != null) { serverSocket = null; } ... serverSocket = new ServerSocket(PORT);
- 09-27-2011, 05:18 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
Re: New to Java and don't understand the underlying mechanisms of the following...
I thought that both serverSocket-variables referred to the same object; if I instantiate serverSocket in ServerSocketThread, the variable serverSocket in ConnectionsHandler wouldn't be null anymore... Gonna experiment a bit with this tomorrow.
- 09-27-2011, 05:32 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: New to Java and don't understand the underlying mechanisms of the following...
Nope.
Java is pass by value.
The reference (and that is all variables are, a reference to an object in memory) passed to method2 is a value, not a pointer to a reference, so the instantiation inside method2 has no effect whatsoever on the value of "so" in method1.Java Code:void method1() { SomeObject so = new SomeObject(); method2(so); System.out.println(so); } void method2(SomeObject so) { so = new SomeObject(); }
The same applies to your serverSocket variables (references).
- 09-28-2011, 08:25 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
Re: New to Java and don't understand the underlying mechanisms of the following...
I was so used for object to be passed by reference in C#, that I didn't think it would be different in Java. Thank you for making this clear!
- 09-28-2011, 09:26 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: New to Java and don't understand the underlying mechanisms of the following...
Would that work in C#?
I know you can pass by ref, but will that continue to a later assignment which occurs as part of a completely different method call?
- 09-29-2011, 08:06 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
Re: New to Java and don't understand the underlying mechanisms of the following...
As far as I know, yes it would.
- 09-29-2011, 09:33 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: New to Java and don't understand the underlying mechanisms of the following...
(C# syntax is not guaranteed to be correct)
If I call initialMethod() with a parameter, then at some later point call someLaterMethod(), then I really wouldn't expect C# to remember that at some point in the past someLocalPar was assigned from a ref parameter and thus be able to change that parameters reference to something else.Java Code:void initialMethod(ref SomeOjbect par) { someLocalPar = par; } void someLaterMethod() { someLocalPar = new SomeObject(); }
I think Jos knows C# so he might be able to shed light on it, assuming he ever looks in here.
Not that that's important to your problem of course.
- 09-29-2011, 03:48 PM #11
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
Re: New to Java and don't understand the underlying mechanisms of the following...
I think someLocalPar keeps a reference to par until someLaterMethod() is raised.
- 09-29-2011, 04:02 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: New to Java and don't understand the underlying mechanisms of the following...
I really can't see how it does that (or why it would bother).
Smacks of "magic"...and magic in code is a Bad Thing.
- 09-29-2011, 06:52 PM #13
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
Re: New to Java and don't understand the underlying mechanisms of the following...
Not sure if it is the following that you are talking about, but I quickly made this example in C# to test it, the comments is what is shown in the console. :
C# code:
So, someLocalPar doesn't forget its reference between different methods (you don't even need to add the ref keyword).Java Code:class Program { private theObject someLocalPar; static void Main() { new Program(); } Program() { theObject originalPar = new theObject("original"); Console.WriteLine(showPar(someLocalPar)); // null Console.WriteLine(showPar(originalPar)); // original initalMethod(originalPar); Console.WriteLine(showPar(someLocalPar)); // original Console.WriteLine(showPar(originalPar)); // original someLaterMethod(); Console.WriteLine(showPar(someLocalPar)); // later Console.WriteLine(showPar(originalPar)); // original } void initalMethod(theObject par) { someLocalPar = par; } void someLaterMethod() { someLocalPar = new theObject("later"); } string showPar(theObject par) { if (par != null) { return par.aProperty; } else { return "null"; } } class theObject { public string aProperty { get; set; } public theObject(string ap) { aProperty = ap; } } }
- 09-30-2011, 09:54 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: New to Java and don't understand the underlying mechanisms of the following...
But that's not what you were doing in the Java.
You were expecting originalPar to change...which your code shows i doesn't.
- 09-30-2011, 10:36 AM #15
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
SQLException caught: Communications link failure due to underlying exception: **
By sharanya in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-25-2011, 09:46 AM -
ECLIPSE + MYSQL Communications link failure due to underlying exception
By transistor47 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 07-16-2009, 11:21 PM -
Renaming underlying directory in Java project
By Rodrigo Braz in forum EclipseReplies: 3Last Post: 03-08-2009, 07:25 AM -
For Newbies: Do you understand the Java API specification?
By CJSLMAN in forum New To JavaReplies: 8Last Post: 02-17-2009, 12:57 PM -
How do I identify that the underlying OS supports a certain language?
By johnt in forum AWT / SwingReplies: 1Last Post: 05-19-2007, 11:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks