You are on page 1of 6

ASSIGNMENT 1

Develop a telnet client for your phone.

The purpose of this assignment is to give you some experience developing a


simple socket based application.

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

telnet address port

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:

1. The client should be an application running on your phone.


2. The interface should consist of two views:
the yet_to_connect view and the connected view.
The yet_to_connect view should comprise two text entry fields, specify
the host address and port number of the host that the user wishes to connect to,
and a connect button.
Once the user has entered this information and presses the connect
button, the client should attempt to establish a network connection to the host
and switch to the connected view.
The connected view should comprise three controls: a multiline text
field, a text entry field, and a finish button. The multiline text field is for
displaying both the received text and the text that the user enters. The text entry
field is used to enter text that is to be sent to the remote host. The Finish button
is used by the user to terminate the current telnet session. We encourage you to
create additional functionality in your telnet client.
MainActivity.java

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.*;

public class MainActivity extends AppCompatActivity {

private Socket socket=null; private Scanner sockIn=null; private


PrintWriter sockOut=null;
private String IPAddress; int port;
private Button btnConnect, btnDisconnect, btnSend;
private EditText txtIPAddress, txtPortNo, txtMsgToSend;
private TextView txtResponse, txtStatus;

@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

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sounak.telnet">
<uses-permission android:name="android.permission.INTERNET" />

<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>

You might also like