Advertisement
Guest User

java.net.ConnectException: Connection refused: connect

a guest
Mar 19th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. Server's code :
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.widget.Toast;
  5.  
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.net.ServerSocket;
  11. import java.net.Socket;
  12. import java.nio.charset.Charset;
  13. import java.nio.charset.StandardCharsets;
  14.  
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);}
  22.  
  23.  
  24. public class Server{
  25.  
  26. public void main(String args[]) {
  27. ServerSocket serverSocket = null;
  28. try {
  29. serverSocket = new ServerSocket(5000);} catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32.  
  33. while (true) {
  34. // Wait for a client connection.
  35. Socket clientSocket = null;
  36. try {
  37. clientSocket = serverSocket.accept();
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41.  
  42. // Create and start a thread to handle the new client
  43.  
  44. try {
  45. // Get the socket's InputStream, to read bytes
  46. // from the socket
  47. InputStream in = clientSocket.getInputStream();
  48. // wrap the InputStream in a reader so you can
  49. // read a String instead of bytes
  50. BufferedReader reader = new BufferedReader(
  51. new InputStreamReader(in, Charset.forName("UTF-8")));
  52. // Read from the socket and print line by line
  53. String line;
  54. while ((line = reader.readLine()) != null) {
  55. Toast.makeText(getApplicationContext(),line,Toast.LENGTH_LONG).show();
  56. }
  57. }
  58. catch (IOException e) {
  59. e.printStackTrace();
  60. } finally {
  61. // This finally block ensures the socket is closed.
  62. // A try-with-resources block cannot be used because
  63. // the socket is passed into a thread, so it isn't
  64. // created and closed in the same block
  65. try {
  66. clientSocket.close();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70.  
  71. }
  72.  
  73.  
  74. }
  75. }
  76.  
  77.  
  78. }}
  79. Client's code :
  80.  
  81.  
  82. import java.io.IOException;
  83. import java.io.OutputStream;
  84. import java.io.OutputStreamWriter;
  85. import java.io.PrintWriter;
  86. import java.net.Socket;
  87. import java.nio.charset.StandardCharsets;
  88.  
  89. public class ClientClass {
  90. public static void main(String args[]) {
  91. try (Socket socket = new Socket("192.168.1.3", 5000);) {
  92. // We'll reach this code once we've connected to the server
  93.  
  94. // Write a string into the socket, and flush the buffer
  95. OutputStream outStream = socket.getOutputStream();
  96. PrintWriter writer = new PrintWriter(
  97. new OutputStreamWriter(outStream, StandardCharsets.UTF_8));
  98. writer.println("Hello world!");
  99. writer.flush();
  100. } catch (IOException e) {
  101. // Exception should be handled.
  102. e.printStackTrace();
  103. }
  104. }
  105. }
  106.  
  107.  
  108. error :
  109. java.net.ConnectException: Connection refused: connect
  110. at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  111. at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
  112. at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
  113. at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
  114. at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
  115. at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  116. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  117. at java.net.Socket.connect(Socket.java:589)
  118. at java.net.Socket.connect(Socket.java:538)
  119. at java.net.Socket.<init>(Socket.java:434)
  120. at java.net.Socket.<init>(Socket.java:211)
  121. at ClientClass.main(ClientClass.java:10)
  122. BUILD SUCCESSFUL (total time: 2 seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement