Skip to content

Commit

Permalink
Added example socket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
km2442 committed May 6, 2019
1 parent 4ef8d13 commit f0a64e3
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 11 deletions.
6 changes: 6 additions & 0 deletions Android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.kamilklecha.autoswitchmobile">

<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
Expand All @@ -13,11 +14,27 @@
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

Button btn;
private Socket socket;

private static final int SERVERPORT = 21;
private static final String SERVER_IP = "192.168.1.160";

private TextView mTextViewReplyFromServer;
private EditText mEditTextSendMessage;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -49,6 +66,18 @@ public void onClick(View v) {
}
};
btn.setOnClickListener(cl);

Button buttonSend = (Button) findViewById(R.id.btn_send);

mEditTextSendMessage = (EditText) findViewById(R.id.edt_send_message);
mTextViewReplyFromServer = (TextView) findViewById(R.id.tv_reply_from_server);

buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage(mEditTextSendMessage.getText().toString());
}
});
}

@Override
Expand Down Expand Up @@ -81,4 +110,47 @@ public boolean onNavigationItemSelected(MenuItem item) {
drawer.closeDrawer(GravityCompat.START);
return true;
}

private void sendMessage(final String msg) {

final Handler handler = new Handler();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {

try {
//Replace below IP with the IP of that device in which server socket open.
//If you change port then change the port number in the server side code also.
Socket s = new Socket(SERVER_IP, SERVERPORT);

OutputStream out = s.getOutputStream();

PrintWriter output = new PrintWriter(out);

output.println(msg);
output.flush();
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
final String st = input.readLine();

handler.post(new Runnable() {
@Override
public void run() {

String s = mTextViewReplyFromServer.getText().toString();
if (st.trim().length() != 0)
mTextViewReplyFromServer.setText(s + "\nFrom Server : " + st);
}
});

output.close();
out.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});

thread.start();
}
}
60 changes: 49 additions & 11 deletions Android/app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="59dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.65999997"
app:srcCompat="@drawable/icon" />

<TextView
Expand All @@ -30,30 +27,71 @@
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="100dp"
android:text="@string/home_welcome"
app:layout_constraintBottom_toTopOf="@+id/Main_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView3" />

<Button
android:id="@+id/Main_btn"
android:layout_width="379dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="@string/home_next_screen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/btn_send"
android:layout_width="379dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Socket"
app:layout_constraintBottom_toTopOf="@+id/Main_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_reply_from_server" />

<EditText
android:id="@+id/edt_send_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@string/home_next_screen"
app:layout_constraintBottom_toTopOf="@+id/guideline"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<android.support.constraint.Guideline
android:id="@+id/guideline"
<TextView
android:id="@+id/tv_reply_from_server"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/edt_send_message" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8548148" />
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Result:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_send_message" />

</android.support.constraint.ConstraintLayout>

0 comments on commit f0a64e3

Please sign in to comment.