Only I know how tuf it is to write the second part. Not because of technical things, but simply because of my nature. I am very curious by nature , but flicking. I started SOCKETS article to make an app, then once I knew how it can be done, I start becoming disinterested.
But PROCRASTINATION , is not going to take me anywhere. So even if universe ends today, I have to complete this before it.
So lets start directly.
Now as I see right now, maybe this article might not end with Article 2, but spill further into one more article.
Now I will try to have code + theory at same time, else interest goes away. As we have seen in last article knowing just SOCKET API/functions are not enuf, we need to know also about OS. So in this article we will see threads and async task, as per our requirement.
> Layout of our Program. If anyone has used Android Studio or Eclispe before for Android application they know about
Manifest.xml , Layour.xml , .java files. We will not see those in here, possibly I give put snapshot of those in my next article.
Now in Android, when we make application, we divide them into Activities, Each Activity which we create has a UI part, which is govern by .xml file and .java part which has the code.
So in this article we will focus on code part.
So in both the Server and Client application, we will have 1 activity which will be main activity
Server Main activity will create a Server class which will have function to open socket, accept client socket and send data to client. Server Class will be defined in separate .Java file, we will not need activity for this as we don't need UI for this.
Client Main activity will create a Client class which will have function to create socket, and once connection is created listen to what Server is sending and display it on UI. Note Client class will be defined in Client.java file, no activity is needed for it. Display will be done on the Clint Main activity UI.
Now below picture is for Server App in eclipse,
You can see Mainactivity.java which will have code for MAIN activity. Since it is activity it will have a corresponding xml file, which is activity_main.xml which will have UI.
Then the whole server code will go into Server.Java file. In this article we will see only Server.java and Client.Java.
Then there is a AndroidManifest.xml file which governs the properties of Android app, we will see its use at the end.
So we have got enuf skeleton of our app, we will now start with to put flesh and muscles to it.
Now first starting with Server app, Server app has to accept Client socket request and also send data to existing clients. So we need to have two simultaneous operation ongoing to support this.
So we will need threads. THREADS helps to run task in parallel. In java already THREAD class is present, we need to extend that class to get the work done.
Code wise
private class ServerSendData extends Thread { // here ServerSendData is our class in which we will have code for sending data to client and it is extending the thread class, so our class ServerSendData will run as totally independent body with respect to other part of Server program.
> Now get started
The below code is psuedo-code for understanding , main code files I will try to upload.
private class Server {
int port_no = 8080;
void opensocket() {
ServerSocket serverSocket = new ServerSocket(port_no); // SERVER socket is created
Socket socket_c = serverSocket.accept(); // this will accept only ONE client connections.
ServerSendData ssd = new ServerSendData(socket_c); // Here we have create another class to send data
ssd.run(); // we called the other class function
}
private class ServerSendData extends Thread { // this will run indepently to above code
string msg = " I am server";
socket client_socket;
ServerSendData(socket clientsocket)
{
client_socket = clientsocket;
}
public void run()
{
OutputStream outputStream = client_socket.getOutputStream();// Here we are getting outstream of socket
PrintStream printStream = new PrintStream(outputStream); // printstream will put all message pass to it to outstream
printStream.print(msg); // Finally we have send message}
}
}
The above code shows how to achieve functionality of sending data from Server. Below we will see code at Client Side.
private class Client{
void opensocket() {
Socket socket = new Socket(Server IP address, Server Port); // Client socket is created
InputStream inputStream = socket.getInputStream(); // this will get data from socket.
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
String x = "";
x = r.readLine(); // So now in x we have our input data, which we can display
}NOTE : the client code is very simple, since i wanted to show how to achieve functionality code-wise.
In reality, we at client side also run socket creation/communication code as ASYNC task or seperate thread.
Since , reading /writing data from socket can take time, which will make our whole app to hang. So we do that functionality in background.
A bit about ASYNC task : Classes which implements ASYNC task will run in background so that they don't affect over-all functionality.
Code wise : Classes implement ASYNC task like below
class
ClientTask
extends
AsyncTask {
And then this class needs to implement some fucntions related to Async task like
1) Preexecute : Function which will be done before Background function.
2) DoinBackground : The code part of this fucntions execute in background.
3) PostExecute : This will get execute after background function is completed.
4) Progress : If you are reading large amounts of data from socket and periodically wants to display that data , you can call this function from inside of DoinBackground. ( Ever notice , in MYNTRA, flipkart,amazon as we scroll items they gets loaded )
More on Async task can be read from internet like : its return type, parameters etc.
Finally ...I have written the article. Its so difficult to sit on your ass from long-time. Its time to massage it .
In third article , there will be zip file of complete code, possible some video also showing communication between Server and Client. And ...this is far-fetched ...but how to have continuous communication between Server/Client.
No comments:
Post a Comment