The Socket example of pages 130 and 131 of the OSBook Abraha
The Socket example of pages 130 and 131 of the OS-Book (Abraham Silberschatz 8th edition) is the basis of this problem. However, instead of using the Loopback IP (127.0.0.1), which emulates the socket connection on the host computer, we will use real IP. We will therefore use a mobile device ( Android) to take pictures and send them to a Notebook using a wireless communication network.
Our goal here is to write a mobile application for Android that would send data (pictures) from an Android device (or emulator) to a Java application on a Notebook using a socket connection. The mobile device will act as client while the Notebook is the server.
Write the client and server code for this problem and test it with your mobile device and your computer.
Extend your code to allow the transmission a video. A video is defined in this case as a sequence of minimum 10 pictures
You need to read the Android development guide (online) to understand how to read a file (picture from your picture repository) in the Android environment and send it through a socket connection.
Solution
/* mobile application from an andriod device to a java application on a notebook using socket connection*/
/*server side program using notebook as server*/
public class ServerActivity extends Activity {
private TextView tv;
public static String SERVERIP = \"10.0.2.15\";
public static final int SERVERPORT = 8080;
private Handler handler = new Handler();
private ServerSocket serverSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server);
tv = (TextView) findViewById(R.id.server_status);
SERVERIP = getLocalIpAddress();
Thread th = new Thread(new ServerThread());
th.start();
}
public class ServerThread implements Runnable {
public void run() {
try {
if (SERVERIP != null) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText(\"Listening on IP: \" + SERVERIP);
}
});
s_Socket = new S_Socket(SERVERPORT);
while (true)
Socket client = s_Socket.accept();
handler.post(new Runnable() {
@Override
public void run() {
tv.setText(\"Connected.\");
}
});
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
Log.d(\"ServerActivity\", line);
handler.post(new Runnable() {
@Override
public void run(){
}
});
}
break;
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
tv.setText(\"Oops. Connection has been interrupted. Plzz reconnect your phones.\");
}
});
e.printStackTrace();
}
}
} else {
handler.post(new Runnable() {
@Override
public void run() {
tv.setText(\"Couldn\'t detect internet connection.\");
}
});
}
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
tv.setText(\"Errormsg\");
}
});
e.printStackTrace();
}
}
}
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
}
}
} catch (SocketException ex) {
Log.e(\"ServerActivity\", ex.toString());
}
return null;
}
@Override
protected void onStop() {
super.onStop();
try{
tv.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/* client program using mobile phone*/
public class ClientActivity extends Activity {
private EditText et;
private Button b;
private String serverIpAddress = \"\";
private boolean connected = false;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
et= (EditText) findViewById(R.id.et);
b = (Button) findViewById(R.id.b);
b.setOnClickListener(connectListener);
}
private OnClickListener connectListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals(\"\")) {
Thread ct = new Thread(new ClientThread());
ct.start();
}
}
}
};
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d(\"ClientActivity\", \"C: Connecting...\");
Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
connected = true;
while (connected) {
try {
Log.d(\"ClientActivity\", \"C: Sending command.\");
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
// WHERE YOU ISSUE THE COMMANDS
out.println(\"Hey Server!\");
Log.d(\"ClientActivity\", \"C: Sent.\");
} catch (Exception e) {
Log.e(\"ClientActivity\", \"S: Error\", e);
}
}
socket.close();
Log.d(\"ClientActivity\", \"C: Closed.\");
} catch (Exception e) {
Log.e(\"ClientActivity\", \"C: Error\", e);
connected = false;
}
}
}
}



