last day (15 days later) » 

3:33 PM
0
Q: Outputting Every Other Time

ZackI have made an application that opens up a socket via a thread and updates based on what is typed. Here is the code: Server.java: public class Server { public static void main(String[] args) throws IOException { int portNumber = 2392; boolean listening = true; Sy...

 
The fact that you are getting the data from the server correctly implies this is a GUI related issue.
 
@OlaviMustanoja That's what I thought as well but I've been debugging for hours and have yet to come up with a reason as to why it is acting strange when setting the text value in the TextView.
 
when you call the setText, can you also output the fromServer output from the called method? Particularly, can you output the correct text to stdout from the same place where the actual gui change should take place?
 
@OlaviMustanoja do you mean adding the System.out.println() to the runOnUIThread?
 
Ah, yes, that's what I meant. I was running on low caffeine earlier :)
 
3:33 PM
@OlaviMustanoja I KO'd so I wasn't able to respond until this morning lol! Alright I did as requested. As it turns out, it is only outputting every other time as suggested from the output on the screen. I also noticed that it seems to get worst the more times that I close and reopen. (Outputs every 3 times). I'm not sure why but it's now acting the same outside the UIthread. I will edit and post both the server and client code for you to look at.
@OlaviMustanoja it seems to be also incrementing by how many ServerThreads I create. For instance, if I close and reopen the app 3 times. It will most likely output to text every 3 times.
 
You have a peculiar way of starting a new serversocket. I can't determine whether or not it is actually the cause of your issues. I'll test your code as I get home in 15 minutes. Also in ServerThread's while loop you should break if outputLine is null. Try it.
 
Hey Olavi!
 
Thanks for helping me with this btw
 
It's alright. I enjoy the challenge :)
 
3:38 PM
I didn't think I would have this much trouble! lol Did you change anything that was within the Server/Multiserver class?
SeverThread*
 
yes, I of course didn't have any gui classes so I had to modify any code related to that, and anyway any code I had to modify that referenced to something that you didn't provide.
Which leads me to the full source code. Do you have it and can I see it?
 
Yeah I can upload the files if need be.
 
Do that please. We'll save a week with that :D
I would also need the server source, if it's an external application
 
The server is set up on my comp. Its not even a server just opening a socket on my ip address on the network.
using my IPv4 address
Doesn't seem I can upload via stack overflow
have an email I could send this to you?
 
sure. tilastokeskus@gmail.com
 
3:47 PM
Sent
One sec ill be back! let me know if you need anything else!
 
alright
I didn't get the mail
Did you zip it?
 
Yes I zipped both files
The server proj I just zipped the files. The app proj I compressed the whole project.
 
try again. if it doesn't work, try sending it to my other email: olavi.mustanoja@cs.helsinki.fi
 
Okay one sec
Failed to send because of security through Google. I'll try your other email
Seny
Sent
 
Didn't get them >_< do you have dropbox?
 
4:02 PM
Yes I do lol
Lemme send you the link
if you couldn't tell I'm new to coding on android. but I'm working on it lol
 
well it's all java
 
lol of course.
Im pretty good with ObjC but still getting into the whole Java thing.
Oh and ignore all the output to the System. I was just trying to do some outputting to see where things were being calle .
 
as you should :)
 
I just still can't decide whether its the server side or the client side or a little of both. It seems to work fine ALWAYS the first time. But something to do with onStop() to where when I restart the program it will only output every other time.
 
in onStop() you are calling in.stop()
not socket.close()
 
4:16 PM
Do I need to close the socket?
Because I tried that I think originally and this changed nothing for me. But I can try again.
 
preferably when the server stops, the server stops ;)
 
is the Buffer Reader supposed to be closed as well?
 
socket.close() closes the streams automatically
 
ohhhh. So all I should do is socket.close()
 
4:18 PM
Okay let me make that change and let you know
Running. one sec
 
I actually fixed an error an hour ago in my own application where the server went haywire when I tried closing the streams manually. Everything went fine with simply calling socket.close(), and even the javadocs, I later read, said that the streams will be closed automatically
 
Interesting.. ugh its still not working
 
anything changed?
 
Nothing. I am getting a W/System.err
java.net.SocketException: Socket closed
10-24 12:23:08.854 8314-8342/com.ztan222.vitals W/System.err﹕ at libcore.io.Posix.recvfromBytes(Native Method)
10-24 12:23:08.854 8314-8342/com.ztan222.vitals W/System.err﹕ at libcore.io.Posix.recvfrom(Posix.java:141)
10-24 12:23:08.854 8314-8342/com.ztan222.vitals W/System.err﹕ at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
10-24 12:23:08.854 8314-8342/com.ztan222.vitals W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
I don't know if thats normal or nt
 
it's normal. socket.close() always throws an IOException. in your onStop() catch clause you can change e.printStackTrace(); to System.err.println(e.getMessage()); to get a less scary output :) e.getMessage() will output "Connection reset" when the socket closes.
 
4:28 PM
Oh very cool. Something else I noticed is that when the screen dims and disappears without interaction, onStop is called thus making the socket close before exiting the application
Am I supposed to be maybe remembering the socket onStop()?
 
I'm not an android programmer, so I don't know how to handle screen off calls. You can probably google something like "android prevent application from closing on screen off"
 
Yeah, what if I added the socket.close(); to the end of the try-catch?
 
btw in ServerThread.java you close a stream after closing the socket. remove the stream close from there too
what do you mean end?
 
finally {}
and okay one sec!
 
also in the onClose call you check if in != null. you should see if socket != null
 
4:34 PM
Yes I did my code goes:
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
 
it won't compile if you put it in the finally block :) socket.close() throws an exception.
ok good
 
ugh I can't still figure this thing out. Glad you know networking though lol
 
only the basics
 
Hmmm Let me try debugging this thing.
 
CommunivationThread:
if (fromServer.equals("Disconnect."))
{
socket.close();
}
 
4:40 PM
break the while loop as well probably right?
 
socket.close() throws an exception, remember
it'll throw you out of the loop
 
oh crap right.
 
since we do it like that now, we won't need to close it in the onClose method
so remove the whole block from there
 
So wait, I am closing the socket on the server side. Not the server socket but the regular socket. Do I still need to close the socket on the client side?
Oh wait.. Do you think that that could be the issue? I am removing the socket from the client side by dismissing the application but am not closing the socket in the server side because I didn't type "Disconnect"
 
CommunicationThread is the client's connection, correct? If we receive a Disconnect message from the server, the server will close the socket, and then the client will close the socket (or which ever does it first)
 
4:45 PM
Oh so if the client closes the socket it sends a message to tell the server to disconnect the socket as well?
 
oh you didn't? lol
 
No I wasn't typing "Disconnect" just closing the application.
 
well the server should close the socket if the application closes
 
Okay.. So Then I need to send the str "Disconnect" to fromServer Then after that the server will close the socket connection (Not the server socket) and the client will also close the socket. Is that correct?
 
when server sends the Disconnect message, the client will disconnect. Then the server will break out of the loop and close its socket
both sockets will be thus closed
 
4:49 PM
Oh okay.
One sec
I think I might have a little bit of confusion going on. But I have to get to class. Ill continue to work on it and let you know!
It turns out that when I disconnect. The client recreates the serverSocket
 
Ah, too bad. See you later then!
 
If it's okay. I might send you an email to continue working on this.
Thank you so much though for your help and all the information you've provided thus far
 
Of course. One thing quick
change socket = new Socket(serverAddr, SERVERPORT);
to Socket socket = new Socket(serverAddr, SERVERPORT);
and remove the class-scope variable
 
Which file? Sorry I'm walking lol
 
MainActivity.java
 
4:56 PM
Okay. I'll make the change when I get to class!
 
Alright I'm in class lol
 
Is there a reason why you start the server in a new thread?
are you trying to get multiple connections at the same time?
I mean are you trying to get many people to be able to connect to your server?
 
Yes here is the main goal: Have multiple google glass connect to my computer and then have the computer output messages to all google glasses
 
I see. So it's broken that way. Ok we'll do this: get one person to connect to the server without issues :D
 
5:04 PM
Okay deal! lol
 
then we'll do the one that supports multiple connections
I'll link you a paste with my changes in a minute
 
Okay. Thank you so much for your help Olavi
 
MainActivity.java: codepaste.net/39wmze
 
If there is even a way. I was trying to make it to where it would close the socket when the app closes and then use a new socket with the same port number to connect to the server and continue to receive messages without restarting the server program. It wasn't until Disconnect is typed on the server that ALL sockets were closed
 
Server.java: codepaste.net/9gsv7w
there's always a way
there wont be a ServerThread.java this time around :D go compile & run
 
5:16 PM
Okay. I still have the fromString error originally. What should I do again?
 
It might not compile, though. I'm going with notepad here at the moment :D
 
Wow thats impressive.
 
compiler errors are never impressive... well e.getStackTrace() is pretty damn impressive
hum
sec
 
okay
 
final textToBeSet = fromServer;
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// This code will always run on the UI thread, therefore is safe to modify UI elements.
status.setText(textToBeSet);
}
});
brb I need a smoke. ~5min
 
5:22 PM
Okay cool man thanks. Ill run and compile and see what happens!
 
any luck?
 
Have one warning "Server Socket is never closed" and I had to add reader as a local. Other than that I'm about to run!
on Server.java
I can put it in the resources section of the try block
try (ServerSocket serverSocket = new ServerSocket(portNumber);) {

while (listening) {
Socket clientSocket = serverSocket.accept();
......
}
 
yeah you can do that
 
okay cool. Im gonna run it!
I also did
public static void main(String[] args) throws IOException {

boolean listening = true;
Scanner reader = new Scanner(System.in);

System.out.println("Server: Running...");

try (ServerSocket serverSocket = new ServerSocket(portNumber);) {
added the reader as a local
just as an fyi
 
oh yeah, good. you should also probably remove the throws statement form the main method
 
5:38 PM
okay. Will do. I didn't do disconnect. I dismissed the application on the client. It still gets information from the server but does not update the textview at all.
 
What do you mean you didn't do disconnect and what do you mean you dismissed the application on the client?
 
When I ran disconnect, it disconnected the client from the socket! But, when I dismiss or "close" the application and reopen it on my glass, the client will rerun the application making a new socket on the client.
 
Isn't that how it's supposed to go? I mean you close the application, the application closes all connections. You start the application, it creates new sockets and server
 
Right! But for some reason when I do that it doesn't update the text :/
 
let's worry about that later
can you get the correct output from the server?
do you still have that only every second output -issue?
 
5:47 PM
And yes! I can. It receives output, then when i type disconnect. It displays Disconnect then doesn't receive anymore output
 
yes. when you type disconnect, the client and the server disconnects, and the application should close
isn't that what happens?
 
The application still runs on the server side
 
get this code (Server.java): codepaste.net/ssgq5t
 
I can just set listening = false;
 
what does that code output?
 
5:55 PM
One sec
let me try
Server: Running...
Client connected!
asd
fds
Disconnect
1
Disconnect
1
 
what???
what the fuck
 
That's what I'm saying lol
 
clientSocket.close(); doesn't throw IOException?
 
Not on the comp
 
take ServerSocket serverSocket = new ServerSocket(portNumber); out of the try resource, and add a finally block that closes the serverSocket
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(portNumber);
. . .
} finally {
if (serverSocker != null) {
}
oops

ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(portNumber);
. . .
} finally {
if (serverSocker != null) {
serverSocket.close();
}
}
 
6:03 PM
have to do try catch though for server.close right?
 
Disconnect still throwing a 1
nothing else
I can always just add a break and set listening = false though
It works now after adding a break and setting listening = false
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

while (true) {
String outputLine = reader.nextLine();
out.println(outputLine);

if (outputLine.equals("Disconnect")) {
System.out.println(1);
clientSocket.close();

break;
}

listening = false;
 
alright... well. you can remove the while (listening) completely, then
 
hmm. I guess my next hurdle is to make the clientSocket close based on the client and not the server.
 
your server runs independently from the client, yes?
 
6:16 PM
Yes. the server should be running all the time and close the socket based on if the user leaves the application.
on the app
 
we'll make it do that in a while
sec
 
Okay. Unfortunately I do have to go :/ Is there anyway we can keep in touch via email??
 
sure. we can set up the chat again later :)
mail me preferably to tilastokeskus@gmail.com
I'm sleeping mostly from 1:00 am to 12:00 am (GMT +2)
 
Okay. Sounds good! Thanks for all of your help!!!
 
sure. we'll get your app working :)
 

last day (15 days later) »