-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientActivity.java
141 lines (135 loc) · 5.11 KB
/
ClientActivity.java
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
package com.example.tyler.client;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
public class ClientActivity extends Activity {
EditText serverIp,smessage;
TextView chat,hat;
ScrollView scrollchat;
Button connectPhones,sent;
String serverIpAddress = "",msg = "",str;
Handler handler = new Handler();
TextView hi;
LinearLayout linearchat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
chat = (TextView) findViewById(R.id.chat);
serverIp = (EditText) findViewById(R.id.server_ip);
scrollchat= (ScrollView) findViewById(R.id.scrollchat);
linearchat=(LinearLayout) findViewById(R.id.linearchat);
//smessage = (EditText) findViewById(R.id.smessage);
/*
sent = (Button) findViewById(R.id.sent_button);
sent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread sentThread = new Thread(new sentMessage());
sentThread.start();
}
});*/
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
serverIpAddress =serverIp.getText().toString();
if (serverIpAddress.equals("")) {
serverIpAddress = "172.20.10.5";
}
else if(!serverIpAddress.equals("")){
//serverIpAddress = "172.20.10.5;
serverIpAddress =serverIp.getText().toString();
}
Thread clientThread = new Thread(new
ClientThread());
clientThread.start();
}
});
}
/*class sentMessage implements Runnable
{
@Override
public void run()
{
try
{
InetAddress serverAddr =
InetAddress.getByName(serverIpAddress);
Socket socket = new Socket(serverAddr,9090); //create client socket
DataOutputStream os = new
DataOutputStream(socket.getOutputStream());
str = smessage.getText().toString();
str = str + "\n";
msg = msg + "Client : " + str;
handler.post(new Runnable() {
@Override
public void run() {
chat.setText(msg);
}
});
os.writeBytes(str);
os.flush();
os.close();
socket.close();
}
catch(IOException e)
{
}
}
}*/
public class ClientThread implements Runnable
{
public void run() {
try {
while (true) {
InetAddress serverAddr =
InetAddress.getByName(serverIpAddress);
System.out.println("hi");
Socket socket = new Socket(serverAddr, 9090); //create client socket
/*******************************************
setup i/p streams
******************************************/
//BufferedReader input =
// new BufferedReader(new InputStreamReader(s.getInputStream()));
DataInputStream in = new
DataInputStream(socket.getInputStream());
String line = null;
while ((line = in.readLine()) != null) {
msg = /*msg +*/ "Server : " + line + "\n";
hi=new TextView(ClientActivity.this);
hi.setText(msg);
hi.setHeight(64);
hi.setTextSize(16);
//linearchat.addView(hi);
handler.post(new Runnable() {
@Override
public void run() {
if(hi.getParent()!=null)
((ViewGroup)hi.getParent()).removeView(hi);
linearchat.addView(hi);
}
});
}
in.close();
socket.close();
Thread.sleep(100);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
}