0

In my project server socket doesn't respond properly in android. I don't know what's wrong in that? My server side code is stuck in serversocket.accept().

In manifest file i have given internet and foreground service permission. I am beginner and I am trying this for past 3 days and I have spent too much time in that.

This is my server side code:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;



public class MainActivity extends AppCompatActivity {
    Socket s;
    ServerSocket serverSocket;
    TextView textView;
    Button button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.texview);
        button=findViewById(R.id.buttonstart);
        connect();


    }

    public void connect(){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
                button.setEnabled(false);
                Thread thread=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            
                            serverSocket=new ServerSocket(8888);
                            
                            while (true){
                               
                                s=serverSocket.accept();

                               
                                DataInputStream datainput=new DataInputStream(s.getInputStream());
                                String txt=(String)datainput.readUTF();
                                textView.setText(txt);

                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
                thread.start();

            }
        });
    }

    public void stop(View view) {
        try {
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

And this is client side code for testing in java format

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class client {
    public static void main(String[] args) {
        try {
            Socket s=new Socket("192.168.43.24", 9999);//for bind by ip & port 1
            DataOutputStream m=new DataOutputStream(s.getOutputStream());// for send msg
            Scanner scan= new Scanner(System.in);
            while(true){
                System.out.print("Type you msg here :- ");
                String send=scan.nextLine();
                m.writeUTF(send);
                m.flush();
            }
        } catch (Exception e) {
            System.out.println(e);//print error
            //TODO: handle exception
        }
    }
    
}
  • `textView.setText(txt);` You can not change the gui in the run() of a thread. Log the string first then use runOnUiThread() to set the text of the textview. – blackapps Jul 12 '21 at 13:13
  • `public void connect()` Do not call that connect as a server socket can not connect. It has to wait until a client connects. Call it listen() or startServer(). – blackapps Jul 12 '21 at 13:14
  • `String txt=(String)datainput.readUTF();` The client is not sending an utf string i think. Try a stream that implements .readLine(). And let the client send a line: s.sendall(b'Hello, world\n') – blackapps Jul 12 '21 at 13:18
  • `data = s.recv(1024)` The server is not sending anything so there is nothing to receive. – blackapps Jul 12 '21 at 13:19
  • @blackapps thanks for your response bro but in the server side server socket not accepting any connection i dont know why i track my code by using log code was stuck in serversocket.accept() and logcat error section empty not getting any error. Once again thanx bro for your time – Mahendra Kaperavenollu Jul 12 '21 at 13:49
  • @blackapps bro python code is just testing for my android for see connection is making or not – Mahendra Kaperavenollu Jul 12 '21 at 13:52

1 Answers1

-1

Confirm your client is connecting to the correct IP and port. For starters, your server is set to port 8888, but your client is attempting to connect to server port 9999?

Joe Mattioni
  • 79
  • 1
  • 5
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](/help/whats-reputation) you will be able to [comment on any post](/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – Suraj Rao Jan 13 '22 at 16:36