简体   繁体   中英

Client Socket in my app not connecting to a Java server

We have developed an android application-just to on the wi-fi service of the smartphone and then connect to a java server running on a different pc.The client code is using the IP address of that particular pc on which the server is running.The client code is also using the same port number as the server code.But still the client socket is not connecting to the particular server or rather not responding at all.

My client code :

JAVA File :

package com.example.android.try1;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class First extends AppCompatActivity {
Button bt1;
TextView smsg;
ObjectOutputStream oos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    Button bt1 = (Button)findViewById(R.id.bt1);

    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        WifiManager wifi = (WifiManager)
          getApplicationContext().getSystemService(Context.WIFI_SERVICE);

            final boolean b = wifi.setWifiEnabled(true);

            Socket clientSocket = null;
            try {
                Log.d("Initializing client","");
                clientSocket = new Socket("192.168.43.193", 6666);

                if (clientSocket!=null)
                {
                    Log.d("Tested","connected");

                    oos = new           ObjectOutputStream(clientSocket.getOutputStream());

        oos.writeUTF("hii");

        oos.close();
                }
                else
                {
                    Log.d("Client Socket","NULL");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}

}
      getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        final boolean b = wifi.setWifiEnabled(true);

is async an operation and takes some time to connect to available wifi network So once you switch on the wifi you need to wait for network connection then try to connect.

    <receiver android:name=".NetworkChangeReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>

You should register for network change broadcast and once you get broadcast check for connection and then connect to your server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM