Skip to content
Snippets Groups Projects
Commit 30a1ef2c authored by Aylen Ricca's avatar Aylen Ricca
Browse files

basic server

parent ca0d3e6f
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ package uy.edu.chesstrack.communication;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
......@@ -13,102 +14,43 @@ import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
public class Server {
private static final String TAG = "SERVER";
private ServerSocket serverSocket;
Handler updateConversationHandler;
Thread serverThread = null;
private TextView text;
private Socket clientSocket;
private BufferedReader input;
private DataOutputStream output;
public static final int SERVERPORT = 6000;
public static final int SERVERPORT = 5555;
public Server() {
updateConversationHandler = new Handler();
this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
clientSocket = null;
try {
serverSocket = new ServerSocket(SERVERPORT);
clientSocket = serverSocket.accept();
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
String read = input.readLine();
Log.i(TAG, "line="+ read);
this.output = new DataOutputStream(this.clientSocket.getOutputStream());
this.output.writeBytes(new String("AYLEN RICCA"));//(read.toCharArray());
} catch (IOException e) {
e.printStackTrace();
}
}
public void Stop() {
try {
serverSocket.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class ServerThread implements Runnable {
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
try {
socket = serverSocket.accept();
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class CommunicationThread implements Runnable {
private Socket clientSocket;
private BufferedReader input;
private BufferedWriter output;
public CommunicationThread(Socket clientSocket) {
this.clientSocket = clientSocket;
try {
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
this.output = new BufferedWriter(new OutputStreamWriter(this.clientSocket.getOutputStream()));
String str = "AYLEN RICCA";
PrintWriter out = new PrintWriter(this.output);
out.println(str);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
updateConversationHandler.post(new updateUIThread(read));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class updateUIThread implements Runnable {
private String msg;
public updateUIThread(String str) {
this.msg = str;
}
@Override
public void run() {
text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment