• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error connect client via socket

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

I have a program that receives a data package via socket from a client.

The program detects the IP and client port to send to client a mensage confirmation.

But, when the program send the message back into its socket, it receives the following error message:

    Exception in thread "main" java.net.ConnectException: Connection timed out: connect
        at java.net.DualStackPlainSocketImpl.connect0 (Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect (DualStackPlainSocketImpl.java:79)
        at java.net.AbstractPlainSocketImpl.doConnect (AbstractPlainSocketImpl.java:339)



The code is as follows:



When try the command     occurs the error message.

NOTE: The server has no enabled Firewall.

What could be going wrong?

Thank you,

Fernando
 
Marshal
Posts: 4499
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two issues. First, to get the port for the client you should be calling getPort() - see Error recognizing IP and Port from Client ... . Second, to reply to the client, write to the output stream for the same socket you read from - s.getOutputStream() - don't try and open a new socket connection back to the client (unlikely the client is listening for connections anyway).
 
Wanna see my flashlight? How about this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic