Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Server's code :
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.nio.charset.Charset;
- import java.nio.charset.StandardCharsets;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);}
- public class Server{
- public void main(String args[]) {
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(5000);} catch (IOException e) {
- e.printStackTrace();
- }
- while (true) {
- // Wait for a client connection.
- Socket clientSocket = null;
- try {
- clientSocket = serverSocket.accept();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // Create and start a thread to handle the new client
- try {
- // Get the socket's InputStream, to read bytes
- // from the socket
- InputStream in = clientSocket.getInputStream();
- // wrap the InputStream in a reader so you can
- // read a String instead of bytes
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(in, Charset.forName("UTF-8")));
- // Read from the socket and print line by line
- String line;
- while ((line = reader.readLine()) != null) {
- Toast.makeText(getApplicationContext(),line,Toast.LENGTH_LONG).show();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- } finally {
- // This finally block ensures the socket is closed.
- // A try-with-resources block cannot be used because
- // the socket is passed into a thread, so it isn't
- // created and closed in the same block
- try {
- clientSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }}
- Client's code :
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.nio.charset.StandardCharsets;
- public class ClientClass {
- public static void main(String args[]) {
- try (Socket socket = new Socket("192.168.1.3", 5000);) {
- // We'll reach this code once we've connected to the server
- // Write a string into the socket, and flush the buffer
- OutputStream outStream = socket.getOutputStream();
- PrintWriter writer = new PrintWriter(
- new OutputStreamWriter(outStream, StandardCharsets.UTF_8));
- writer.println("Hello world!");
- writer.flush();
- } catch (IOException e) {
- // Exception should be handled.
- e.printStackTrace();
- }
- }
- }
- error :
- java.net.ConnectException: Connection refused: connect
- at java.net.DualStackPlainSocketImpl.connect0(Native Method)
- at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
- at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
- at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
- at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
- at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
- at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
- at java.net.Socket.connect(Socket.java:589)
- at java.net.Socket.connect(Socket.java:538)
- at java.net.Socket.<init>(Socket.java:434)
- at java.net.Socket.<init>(Socket.java:211)
- at ClientClass.main(ClientClass.java:10)
- BUILD SUCCESSFUL (total time: 2 seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement