#Problem with Networking - Socket timeout - but it shouldn't timeout!

54 messages · Page 1 of 1 (latest)

fathom gazelle
#

So I've made an App which should allow my phone to send an Image to my computer. I've made another app before, which was a bit easier and everythiong worked fine. In the new App, the only difference is that the image is being cropped before it is sent to my pc, the networking code stayed the same and I'm really furious right now because I just cant figure out why it does not work... I always get a TimeOut Exception which I dont understand. I tried turning off the firewall and stuff like that but nothing works. (This is the code in Android Studio to send the Image)

Heres my Code for sending the Image:

new Thread() {
            public void run() {
                Socket socket = null;
                try {
                    socket = new Socket(host, 6245);
                    System.out.println("socket successfully opened!");


                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                    InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());

                    OutputStream outputStream = socket.getOutputStream();
                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, bytesRead);
                    }
                    outputStream.close();
                    inputStream.close();
                    socket.close();
                    System.out.println("Photo sended successfully.");
                } catch (IOException e) {
                    e.printStackTrace();
                    socket.close();
                }

            }
        }.start();

This code must be the problem, since the server-side code works for the other app, which uses the exact same code to send the file and I didn't change anything whatsoever. Thank you for helping!

pastel sequoiaBOT
#

This post has been reserved for your question.

Hey @fathom gazelle! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fathom gazelle
#

This is the code for the server: https://hastebin.com/share/ominaneyoy.java

For some reason, I can't paste it in this channel, it says "Your message could not be delivered..."

maiden cairn
fathom gazelle
#

Its a timeout exception.

maiden cairn
#

Where in the code is the exception thrown ?

fathom gazelle
#

This line ```Java
socket = new Socket(host, 6245);

#

So it could be that the server is not opened correctly. But I've tried disabling firewall and stuff.... Not working

maiden cairn
#

You did check that the ip of the server corresponds to the ip you gave the client ?

fathom gazelle
#

I use ```Java
InetAddress.getLocalHost().getHostAddress()

#

I dont really get it, but my old app worked with the server, but now I does not anymore, but I have not changed anything with the server, its so weird

maiden cairn
fathom gazelle
#

I dont think so. Cause as I said, it worked yesterday but today it doesnt work....

#

(with the old App)

maiden cairn
#

Ok then hardcode it for now and check if that works. Then we can figure out why the code above doesnt work for your application.

fathom gazelle
#

Wait...

#

Could this be a step forward? This is the IP 169.254..... I always used to enter in my App

#

To connect

#

But it says Virtual-Box Ethernet-Adapter can this be a problem?

maiden cairn
#

Are you working out of a vm ?

fathom gazelle
#

No.

#

And also what might help you - When I run android Studio and I run the App inside android studio on an emulator and not on my phone, then it works fine

#

I just have no idea

#

It has never wondered me that I never got any 192.168.0.xyz adress on my PC when launching my server because it always worked when I entered this adress

maiden cairn
#

But the same configuration worked for the previous version of the app ? Even on your phone ?

fathom gazelle
#

Yep correct

#

But now it does not anymore

maiden cairn
fathom gazelle
#

On my PC I get this virtual box adress. When I launch this on my laptop, I get a 192.168.x.y adress

#

I dont know what I would get on my phone since I only enter this adress in my phone to connect to the pc

maiden cairn
fathom gazelle
#

Yes thats correct

maiden cairn
#

Does ipconfig on the pc show any other adapters ?

fathom gazelle
#

It shows my local IP4 adress (192.168.0.xyz) but when I enter that, it also doesn't work

#

thats it with the ethernet-dapter list

maiden cairn
#

So even if you use the router assigned ip4 adress on your phone it results in an error ?

fathom gazelle
#

Yeah

#

it times out

maiden cairn
fathom gazelle
#

I use this in my server-socket script to display the Ip adress I then input in my phone

fathom gazelle
#

Wait

#

It just worked

#

WTH

#

Im so confused

#

I changed literally nothing

#

bruh - What??

winter warren
#

maybe router glitch 🤷‍♂️

fathom gazelle
#

possible

#

Alright, then I think this is closed for now. Thank you very much @maiden cairn, Ich danke dir mit ganzem Herzen!

maiden cairn
#

More likely some confusion about the host. Binding the serversocket might help

fathom gazelle
#

I'll try that! Thank you very much!

pastel sequoiaBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.