Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket: could not connect to server or specific port
    text
    copied!<p>I am trying to connect server by port and Host Name.<br> I find one program but when i am trying to run it will show following Exception </p> <pre><code>java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) at java.net.Socket.connect(Socket.java:579) at java.net.Socket.connect(Socket.java:528) at java.net.Socket.&lt;init&gt;(Socket.java:425) at java.net.Socket.&lt;init&gt;(Socket.java:208) at sample.Echoclient2.main(Echoclient2.java:31) Couldn't get I/O for the connection to: 127.0.0.1 Java Result: 1 </code></pre> <p>Here is my code which i use. </p> <pre><code>public class Echoclient2 { public static void main(String[] args) throws IOException { String serverHostname = new String ("127.0.0.1"); if (args.length &gt; 0) serverHostname = args[0]; System.out.println ("Attemping to connect to host " + serverHostname + " on port 10008."); Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { echoSocket = new Socket(serverHostname, 10008); System.out.println("server name"+Inet4Address.getByName(serverHostname)); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream())); System.out.println("Connection accepted " + echoSocket.getInetAddress() + ":" + echoSocket.getPort()); } catch (UnknownHostException e) { e.printStackTrace(); System.err.println("Don't know about host: " + serverHostname); System.exit(1); } catch (IOException e) { e.printStackTrace(); System.err.println("Couldn't get I/O for " + "the connection to: " + serverHostname); System.exit(1); } BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; System.out.println ("Type Message (\"Bye.\" to quit)"); while ((userInput = stdIn.readLine()) != null) { out.println(userInput); // end loop if (userInput.equals("Bye.")) break; System.out.println("echo: " + in.readLine()); } out.close(); in.close(); stdIn.close(); echoSocket.close(); } </code></pre> <p>} </p> <p><strong>What i need:</strong><br> 1. I need to send some message to server and want response from server. Suppose i send "I am user of stackoverflow" then server should give any response like return same String or convert in uppercase or something else. </p> <p><strong>Some Questions:</strong><br> 1. I write client Java File but whether i need to write server java file.<br> 2. Can we send request by using ip and port.<br> 3. Can we use host name.<br> 4. Any echo server name? i need to send message to this server want to know response.<br> 5. I try both server,java and client.java then i got result? is this solution for me. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload