diff --git a/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Server.java b/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Server.java index 5ac4add01b7fdce95552411072547291f94a7dac..6d37c1262ee92a895f709003bd1a01e2df62a554 100644 --- a/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Server.java +++ b/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Server.java @@ -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