#java socket : message not getting transferred
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
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.
i am having issue in setting up the clietn
I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users 👍
What's your question?
i'm supposed to send messages via socket
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
Try flushing your output right after writing it
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
Oh alright. So do you know which line it's halting at?
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 );
}
Detected code, here are some useful tools:
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);
}
the lines , input and output stream formed, are not running
neither does the message delivered one
But there are other print statements that should have printed before "TCP connection established", right?
Such as "connection accepted"
yes, you are right
So why is your output going right to "TCP connection established"?
i actually
I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users 👍
Oh alright
i was trying to debug earlier, since no success, i started on a fresh file
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
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 );
}
i did that , it didn't work
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
I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users 👍
this is the code as of now
Alright, and what are the results as of now?
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 );
}```
Detected code, here are some useful tools:
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);
}
Where did "output" come from? Is that the message you're sending?
Cause I don't see a println("output") in your code
Oh, here
Are you sure both apps are using the most recent code?
yes
tthe message i'm sending is the 3rd argument on both the machines
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
Could you post the full code one more time? Seems like you made a change between the last file upload and thid
The full code
this is what my batchmate have done, exact same code , but she is getting the output
here's my full code
I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users 👍
this is not mine, this is of my batchmate, i asked for reference because our code exactly same but my input stream is not getting created
this is mine
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?
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
That's what I said here
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();
It sounds like you have 2 apps, let's call em App1 and App2. They're supposed to communicate.
App1 creates a server. App2 connects to the server. That is 1 established connection
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
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
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
Have you tried running the app twice, on the same machine, with localhost and a different port per applicatie
it should have been client = server.accept() ;
i got confused with the way variable was defined,
thanks for your help really appreciate it
Closed the thread.