Saturday, 24 August 2013

Making a small program part as a bigger one in java. Multithreading?. Noobs

Making a small program part as a bigger one in java. Multithreading?. Noobs

I am working on a small project where I have to communicate to an Android
app on my phone and with Arduino.
Now, i have the connection between Android and laptop (used as server, I
have a small amount of data stored here), and I can change the contents of
text files when I send certain instructions from Android app.
This is how I do it:
I have a ServerSide class that listens on port 3000 and I read the text I
stream from phone, then I make certain changes in text files for different
messages.
The code: public class ServerSide {
public static void main(String[] args) throws IOException {
while (true) {
ServerSocket serverSocket = null;
// check if client is trying to connect
try {
serverSocket = new ServerSocket(3000);
} catch (IOException e) {
System.err.println("Cannot communicate on this port");
System.exit(1);
}
Socket clientSocket = null;
// move to another socket
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed");
System.exit(1);
}
// stream that will be sent to client. "true" is for creating from
// existing
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
// stream that comes from the client
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String recivedData, sendData;
ServerProtocol communicationProtocol = new ServerProtocol();
while ((recivedData = in.readLine()) != null) {
sendData = communicationProtocol.process(recivedData);
out.println(sendData);
System.out.println("The text should now be written in file");
System.out.println(sendData);
}
in.close();
out.close();
clientSocket.close();
serverSocket.close();
}
}
}
ServerProtocol.process() is the method that updates the files
By the way, this is a good version of a program that implies connection
via sockets (if anyone should need information about this, at a future
time).
Everything works great, I can see my updates immediatly after I send them,
the server is up and running, waiting for messages.
I forgot to mention, I am new to java and a novice in programming, in
general.
Now, I want this code I managed to write to be part of a bigger "server".
By "server", I understand a program that "serves", performs a service.
When it runs on my laptop, it takes information that comes from the
Internet on the port I specify, change things in files according to my
messages, keeps theese files updated and in the same time it uses theese
files to "interpert" data I send from phone, and then sends according
messages to Arduino Shield. (THIS IS WHAT I WANT TO ACHIVE)
I guess that what I miss, is the following:
How do i make this code I have written untill now, part of a bigger
project, that does all that?
I managed to split the project in 3 parts: 1.Communication laptop -
Android 2.Constant data updates 3.Communication laptop - Arduino
Ive done some reaserch, and I came accross threads. So I thought about
having the communication with Android on a separate thread of a
MainServer. I clearly got it wrong, because it doesnt do what I expect it
to do, so here is the code:
I create the ServerSide class that extends Thread, and has a run() method
that should be called when I start the thread. The just like the one
above, but it just lays inside a run() method: public class ServerSide
extends Thread {
@Override
public void run() {
while (true) {
ServerSocket serverSocket = null;
// check if client is trying to connect
try {
serverSocket = new ServerSocket(3000);
} catch (IOException e) {
System.err.println("Cannot communicate on this port");
System.exit(1);
}
Socket clientSocket = null;
// move to another socket
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed");
System.exit(1);
}
// stream that will be sent to client. "true" is for creating from
// existing
PrintWriter out = null;
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// stream that comes from the client
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String recivedData, sendData;
recivedData = null;
sendData = null;
ServerProtocol communicationProtocol = new ServerProtocol();
try {
while ((recivedData = in.readLine()) != null) {
try {
sendData = communicationProtocol.process(recivedData);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println(sendData);
System.out
.println("The text should now be written in file");
System.out.println(sendData);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.close();
try {
clientSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Then, I have the MainServer:
public class MainServer {
public static void main(String[] args) throws IOException {
System.out.println("Started");
Thread myThread = new Thread(new ServerSide());
myThread.start();
System.out.println("Started2");
while (true);
}
}
It should do nothing, just start the new thread. I expect this new thread
do act just like the old ServerSide above (the one with main() method).
Someone, please tell me where I got it wrong !?!

No comments:

Post a Comment