Develop A Telnet Client For Your Phone.: Assignment 1
Develop A Telnet Client For Your Phone.: Assignment 1
Telnet is a simple application that allows you to connect to a port on remote host
and send/receive text to/from the host. Telnet turns your device into a client so
that you can access data and programs on a telnet server. An example of a telnet
client can be found on most systems: simply open a shell and run the command
Where the address is the domain name or IP address of the host and port is the
port number to connect to. Once connected, you can send text to the port by
typing it on the keyboard (stdin) and all text received is displayed on the
console (stdout).
Your task, is to write a telnet client for your phone. The client should meet the
following minimum requirements:
package com.example.sounak.telnet;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.net.*;
import java.util.*;
import java.io.*;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnConnect=(Button)findViewById(R.id.btnConnect);
btnDisconnect=(Button)findViewById(R.id.btnDisconnect);
btnSend=(Button)findViewById(R.id.btnSend);
txtIPAddress=(EditText)findViewById(R.id.txtIPAddr);
txtPortNo=(EditText)findViewById(R.id.txtPortNo);
txtMsgToSend=(EditText)findViewById(R.id.txtMsgToSend);
txtResponse=(TextView)findViewById(R.id.txtResponse);
txtStatus=(TextView)findViewById(R.id.txtStatus);
btnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
try {
txtStatus.post(new Runnable() {
@Override
public void run() {
txtStatus.setText("Status: Connecting...");
}
});
socket = new
Socket(((TextView)findViewById(R.id.txtIPAddr)).getText().toString(),
Integer.parseInt(((TextView)findViewById(R.id.txtPortNo)).getText().toString
()));
sockIn = new Scanner(socket.getInputStream());
sockOut = new PrintWriter(socket.getOutputStream(),
true);
btnConnect.post(new Runnable() {
public void run() {
btnConnect.setEnabled(false);
}
});
btnDisconnect.post(new Runnable() {
public void run() {
btnDisconnect.setEnabled(true);
}
});
txtStatus.post(new Runnable() {
@Override
public void run() {
txtStatus.setText("Status: Connected");
}
});
new Thread(new Runnable(){
@Override
public void run() {
while(true)
{
final String response=sockIn.nextLine();
txtResponse.post(new Runnable(){
@Override
public void run() {
txtResponse.setText(response + "\n" +
txtResponse.getText());
}
});
}
}
}).start();
}
catch (Exception e) {
e.printStackTrace();
txtStatus.post(new Runnable() {
@Override
public void run() {
txtStatus.setText("Status: Connection Failed");
}
});
}
}
}).start();
}
});
btnDisconnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
try {
socket.close(); socket=null;
btnConnect.post(new Runnable() {
public void run() {
btnConnect.setEnabled(true);
}
});
btnDisconnect.post(new Runnable() {
public void run() {
btnDisconnect.setEnabled(false);
}
});
txtStatus.post(new Runnable() {
@Override
public void run() {
txtStatus.setText("Status: Disconnected");
}
});
}
catch (Exception e) {
e.printStackTrace();
txtStatus.post(new Runnable() {
@Override
public void run() {
txtStatus.setText("Status: Disconnection
interrupted");
}
});
}
}
}).start();
}
});
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
txtStatus.post(new Runnable() {
@Override
public void run() {
txtStatus.setText("Status: Sending message to
server...");
}
});
sockOut.println(txtMsgToSend.getText());
txtStatus.post(new Runnable() {
@Override
public void run() {
txtStatus.setText("Status: Message Sent.");
}
});
}
}).start();
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
if(socket!=null) {
try {
socket.close(); socket = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>