-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPrincipalClienteChat
186 lines (151 loc) · 4.3 KB
/
PrincipalClienteChat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.example.clientechatsocket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class PrincipalClienteChat extends Activity {
private EditText texto;
private TextView salidaTxt;
private TextView contador;
private Button btnEnviar;
Socket sk;
DataInputStream entrada;
public DataInputStream getEntrada() {
return entrada;
}
public void setEntrada(DataInputStream entrada) {
this.entrada = entrada;
}
DataOutputStream salida;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
String msg = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal_cliente_chat);
texto = (EditText) findViewById(R.id.EditText01);
salidaTxt = (TextView) findViewById(R.id.TextView01);
contador = (TextView) findViewById(R.id.textView1);
btnEnviar = (Button) findViewById(R.id.button1);
btnEnviar.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.v(getClass().getCanonicalName(), "Iniciando Socket...");
Log("Enviando... " + texto.getText().toString() + "\n");
ejecutaClienteSocket();
}
});
String ip = "192.168.0.12";
int puerto = 9831;
Log.v(this.getClass().getClass().getCanonicalName(), "Socket " + ip
+ " " + puerto);
try {
sk = new Socket(ip, puerto);
Log.v(getClass().getCanonicalName(), "Se ha iniciado conexion...");
Log("Conexion con servidor: "+ip+" Puerto: "+puerto);
entrada = new DataInputStream(new BufferedInputStream(
sk.getInputStream()));
salida = new DataOutputStream(new BufferedOutputStream(
sk.getOutputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
salidaTxt.append(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
salidaTxt.append(e.getMessage());
}
// ---------------------------------------------------------------
final Handler myHandler = new Handler();
(new Thread(new Runnable() {
public void run() {
while (true) {
String recibe;
try {
recibe = getEntrada().readUTF();
if (recibe != null) {
PrincipalClienteChat.this.setMsg(recibe);
Log.v(getClass().getCanonicalName(),
"Recibiendo datos.." + recibe);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myHandler.post(new Runnable() {
public void run() {
actualizarMensajes();
}
});
}
}
})).start();
// ---------------------------------------------------------------
Log.v(getClass().getCanonicalName(),
"Terminando de iniciar componentes");
}
private void ejecutaClienteSocket() {
try {
Log.v(getClass().getCanonicalName(), "Enviando datos..."
+ texto.getText().toString());
salida.writeUTF(texto.getText().toString());
salida.flush();
} catch (Exception e) {
Log.v(this.getClass().getClass().getCanonicalName(), e.getMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_principal_cliente_chat, menu);
return true;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
try {
sk.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static int val = 0;
protected void Log(String dato) {
Log.v("Enviando a log valor: " + val, dato);
salidaTxt.append(dato + "\n");
val = val + 1;
}
static int cont = 0;
private void delay(long tiempo) {
try {
Thread.sleep(tiempo);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void actualizaContador() {
cont = cont + 1;
contador.setText(Integer.toString(cont));
}
private void actualizarMensajes() {
this.Log(getMsg());
}
}