#java socket : message not getting transferred

1 messages · Page 1 of 1 (latest)

thorny blade
#

Hi All,

neon sandalBOT
#

<@&987246399047479336> please have a look, thanks.

neon sandalBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

thorny blade
neon sandalBOT
shy pendant
thorny blade
#

i open 2 remote servers and done this

#

but message is not getting copied

#

this is how it should be ideally

#

this is what i'm getting

sweet egret
#

Try flushing your output right after writing it

shy pendant
#

ServerSocket cannot be non-blocking, it always blocks

#

In your example, you have both apps bind to port 28220. But only 1 thing can be bound to a port on a single network card at a time

#

So it seems like there's a few problems with your code, unless you're testing on completely different machines

thorny blade
#

both of them are different remote servers

#

so it is fine

shy pendant
#

Oh alright. So do you know which line it's halting at?

thorny blade
#

so , i tried debugging

#

after forming the connections

#

after creating the input , outstreams, if i print anything , it is not showing on console

#
  try {
                System.out.println( "TCP connection established..." );
                ObjectInputStream input = new ObjectInputStream(client.getInputStream());
                ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream());
                System.out.println("input and output stream formed ");
                output.writeObject( message + " from " +
                                    InetAddress.getLocalHost( ).getHostName( ) );
                System.out.println( ( String )input.readObject( ) );
                System.out.println("message delivered  ");
            } catch ( Exception e ) {
                error( e );
            }
neon sandalBOT
# thorny blade ``` try { System.out.println( "TCP connection established..." ...

Detected code, here are some useful tools:

Formatted code
try {
  System.out.println("TCP connection established...");
  ObjectInputStream input = new ObjectInputStream(client.getInputStream());
  ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream());
  System.out.println("input and output stream formed ");
  output.writeObject(message + " from " + InetAddress.getLocalHost().getHostName());
  System.out.println((String) input.readObject());
  System.out.println("message delivered  ");
} catch (Exception e) {
  error(e);
}
thorny blade
#

the lines , input and output stream formed, are not running

#

neither does the message delivered one

shy pendant
#

But there are other print statements that should have printed before "TCP connection established", right?

#

Such as "connection accepted"

thorny blade
#

yes, you are right

shy pendant
#

So why is your output going right to "TCP connection established"?

thorny blade
neon sandalBOT
# thorny blade i actually

I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users 👍

shy pendant
#

Oh alright

thorny blade
shy pendant
#

So, Object streams use header data to establish a connection

#

ObjectInputStream is waiting for the header data

#

You should create the output stream first, which sends the header data, then create the input stream

#

Start by fixing that

thorny blade
#

i did that

#

still no change

#
                System.out.println( "TCP connection established..." );
                ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream());
                
              
                output.writeObject( message + " from " +
                                    InetAddress.getLocalHost( ).getHostName( ) );
                ObjectInputStream input = new ObjectInputStream(client.getInputStream()); 
                System.out.println( ( String )input.readObject( ) );
                System.out.println("message delivered  ");
            } catch ( Exception e ) {
                error( e );
            }
shy pendant
#

It's not going to fix your entire app, but it should get you to the next print statement

#

Seems like the connection is being established, but it was failing at the streams. To fix the stream part, you need to create the output before the input

#

Don't even send data before both output & input have been established

#

It's hard to help when you keep changing the code

thorny blade
#

apologies for that

neon sandalBOT
# thorny blade

I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users 👍

thorny blade
#

this is the code as of now

shy pendant
#

Alright, and what are the results as of now?

thorny blade
#

i get tcp connection only

#

i'm just changing this function in try catch

#
                System.out.println( "TCP connection established..." );
                ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream());
                System.out.println("output");
                ObjectInputStream input = new ObjectInputStream(client.getInputStream()); 
                System.out.println("input ");
                output.writeObject( message + " from " +
                                    InetAddress.getLocalHost( ).getHostName( ) );
               
                System.out.println( ( String )input.readObject( ) );
                System.out.println("message delivered  ");
            } catch ( Exception e ) {
                error( e );
            }```
neon sandalBOT
# thorny blade ``` try { System.out.println( "TCP connection established..." )...

Detected code, here are some useful tools:

Formatted code
try {
  System.out.println("TCP connection established...");
  ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream());
  System.out.println("output");
  ObjectInputStream input = new ObjectInputStream(client.getInputStream());
  System.out.println("input ");
  output.writeObject(message + " from " + InetAddress.getLocalHost().getHostName());
  System.out.println((String) input.readObject());
  System.out.println("message delivered  ");
} catch (Exception e) {
  error(e);
}
thorny blade
#

it stops at ouput

#

i am not sure why input stream is not getting formed

shy pendant
#

Where did "output" come from? Is that the message you're sending?

#

Cause I don't see a println("output") in your code

shy pendant
#

Are you sure both apps are using the most recent code?

thorny blade
#

yes

thorny blade
shy pendant
#

Well, the endpoints established a connection just fine, or else the output stream would not have created properly

#

And you create the input stream right after the output stream

#

So if the code you're showing really is the most updated code, I don't see an issue

shy pendant
thorny blade
shy pendant
#

The full code

thorny blade
#

this is what my batchmate have done, exact same code , but she is getting the output

shy pendant
#

Well wait, now you remove "output" and "input"

#

You keep changing the code....

thorny blade
neon sandalBOT
thorny blade
thorny blade
shy pendant
#

In your code, you accepted a connection using accept(), but then you do nothing with it

#

That's a bit suspicious

#

Does your mate do that too?

thorny blade
#

yes, once the server accepts the connection

#

when we run this code on the 2 machines, 1 machine becomes the server and the other becomes client

#

we are initializing both of them in the constructor

#

i came across this now

shy pendant
#

What I'm saying is, server.accept() returns a Socket. But you ignore the accepted socket in your code

#

You don't assign it to a variable

#

Notice in the SO question how they do java Socket socket = serverSocket.accept();

#

But you just do java serverSocket.accept();

thorny blade
#

not able to figure it out

#

i'll probably update you tomorrow if anything works

shy pendant
#

But then App2 establishes a server, which App1 connects to

#

So you are basically establishing 2 connections between the apps, instead of 1 connection

#

I believe that's why you're getting confused

thorny blade
#

i have a single application this which i'm running from 2 different machines ( passed in arguments)
when i run both of them , one takes the role of client and the other the role of server,

#

output stream is formed first

#

the code blocks at input stream , i'm trying to figure out why

#

i've tried add output.flush() in the end

#

but no success so far

shy pendant
#

It's the same app, I'm saying 2 apps because they are different instances of the same app

#

So you have the app which runs on machine 1 (App1) and the app which runs on machine 2 (App2)

#

And you should have 1 connection between them, right?

#

But you are actually establishing 2 connections between them

#

The first when App1 connects to App2's ServerSocket

The second when App2 connects to App1's ServerSocket

sweet egret
#

Have you tried running the app twice, on the same machine, with localhost and a different port per applicatie

thorny blade
#

i got confused with the way variable was defined,

#

thanks for your help really appreciate it

shy pendant
#

Yup

#

No problem

neon sandalBOT
#

Closed the thread.