user2071791
user2071791

Reputation: 29

How to connect to particular ip & port in android

I am new in socket programming.

I want to get connected to particular IP (e.g.: 184.267.345.65) & port (e.g.: 53) in Android so how could it be possible?

Thanks in advance.

Upvotes: 2

Views: 8963

Answers (2)

Manish Lomte
Manish Lomte

Reputation: 329

URLConnection connection = null;
URL url = null;
InputStream is = null;
String mUrl="IP ADDRESS AND PORT NUMBER";

try {
url = new URL(mUrl);

    connection = url.openConnection();
    connection.setRequestProperty("Accept", "SPECIFY ACCEPT TYPE");
is = connection.getInputStream();
String data=convertStreamToString(is);

} catch (Exception e) {
    Log.i("Exception while reading inputstream  From URL :"+mUrl, e.getMessage());
    e.printStackTrace();
}

Upvotes: -2

iTech
iTech

Reputation: 18460

Socket s = new Socket("184.267.345.65", 53)

See here, here and here for complete example code

Upvotes: 4

Related Questions