TCP Client: receive bytes from TCP Connection - Java, Android - java

I am developing an Android App which receives some data from a server.
I used this tutorial for the TCP Client: TCP Tutorial
Unlike in the tutorial, I need the data as byte[] and not as a String but I can't figure out how to do this.
My TCP Client:
public class TCPClient {
private OnMessageReceived mMessageListener = null;
private BufferedInputStream input;
private byte[] serverMessage; // message received from server
private boolean mRun = false;
/**
* Constructor:
* OnMessagedReceived listens for the messages received from server.
*/
public TCPClient(OnMessageReceived listener) {
mMessageListener = listener;
}
/**
* Connection thread
*/
public void run() {
mRun = true;
try {
// Server address:
InetAddress serverAddr = InetAddress.getByName(10.0.0.170);
Log.e("TCP Client", "C: Connecting...");
// Connection socket:
Socket socket = new Socket(serverAddr, 4848);
try {
// Input stream from server:
input = new BufferedInputStream(socket.getInputStream());
// Listen for messages from server:
while (mRun) {
int bytesRead = input.read();
serverMessage = new byte[bytesRead];
if (serverMessage != null && mMessageListener != null) {
mMessageListener.messageReceived(serverMessage);
}
serverMessage = null; // reset serverMessage
}
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
public void stopClient(){
mRun = false;
}
/**
* The method messageReceived(byte[] message) must be implemented in the
* MainActivity class at on asynckTask doInBackground
*/
public interface OnMessageReceived {
public void messageReceived(byte[] message);
}
}
Main Activity:
public class MainActivity extends AppCompatActivity {
// TCP Connection:
private TCPClient mTcpClient;
connectTask mConnectTask;
Button connectBtn;
// Debugging variable:
private static final String TAG = MainActivity.class.getName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Relate Button and EditText from java to the one in XML:
connectBtn = (Button) findViewById(R.id.connectBtn);
mConnectTask = new connectTask(); // prepare for TCP connection
// Connect to Server after connect btn pressed
connectBtn.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
Log.i(TAG, "Connect button pressed.");
mConnectTask.execute(); // starts Async task
}
}
);
}
public class connectTask extends AsyncTask<byte[], byte[], TCPClient> {
#Override
protected TCPClient doInBackground(byte[]... message) {
// Create a TCPClient object
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
#Override
// Implementation of messageReceived method
public void messageReceived(byte[] message) {
publishProgress(message); // calls the onProgressUpdate method
}
});
mTcpClient.run();
return null;
}
#Override
protected void onProgressUpdate(byte[]... values) {
super.onProgressUpdate(values);
byte b[] = values[0];
// TODO delete
String str = new String(b, Charset.forName("UTF-8"));
Log.d(TAG, "Data received: " + str);
for (int i = 0; i < b.length; i++) {
int c = b[i] & 0xFF;
Log.d(TAG, "c: " + c);
// Further processing where data is needed as byte[]
}
}
}
}
The Log.d
Log.d(TAG, "Data received: " + str);
logs out that data (unrecognizable bytes) is received. But when I log out c, which is supposed to be the received byte as an int, it is always 0.
Any help is highly appreciated :)

Related

Receive message from server in TCP Client and set it in TextView

I'm having some troubles while trying to visualize the message send from the TCP Server as response to my TCP Client
Here is my Client.java code
public class Client {
public static String SERVER_IP; //server IP address
public static String ipp;
public static final int SERVER_PORT = 4444;
// message to send to the server
private String mServerMessage;
// sends message received notifications
private OnMessageReceived mMessageListener = null;
// while this is true, the server will continue running
private boolean mRun = false;
// used to send messages
private PrintWriter mBufferOut;
// used to read messages from the server
private BufferedReader mBufferIn;
/**
* Constructor of the class. OnMessagedReceived listens for the messages received from server
*/
public Client(OnMessageReceived listener) {
mMessageListener = listener;
}
/**
* Sends the message entered by client to the server
*
* #param message text entered by client
*/
public void sendMessage(String message) {
if (mBufferOut != null && !mBufferOut.checkError()) {
mBufferOut.println(message);
mBufferOut.flush();
}
}
/**
* Close the connection and release the members
*/
public void stopClient() {
mRun = false;
if (mBufferOut != null) {
mBufferOut.flush();
mBufferOut.close();
}
mMessageListener = null;
mBufferIn = null;
mBufferOut = null;
mServerMessage = null;
}
public void run() {
mRun = true;
try {
//here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
Log.e("TCP Client", "C: Connecting...");
//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, SERVER_PORT);
try {
//sends the message to the server
mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
//receives the message which the server sends back
mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in this while the client listens for the messages sent by the server
while (mRun) {
mServerMessage = mBufferIn.readLine();
if (mServerMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(mServerMessage);
}
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
//the socket must be closed. It is not possible to reconnect to this socket
// after it is closed, which means a new socket instance has to be created.
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
//Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
//class at on asynckTask doInBackground
public interface OnMessageReceived {
public void messageReceived(String message);
}
}
While here is the MainActivity :
public class MainActivity extends AppCompatActivity {
Server server;
static Client client;
settings Settings;
public static TextView terminale, indr, msg;
TextView log;
static String ipp;
static String trm;
static DataBaseHandler myDB;
allert Allert;
SharedPreferences prefs;
String s1 = "GAB Tamagnini SRL © 2017 \n" +
"Via Beniamino Disraeli, 17,\n" +
"42124 Reggio Emilia \n" +
"Telefono: 0522 / 38 32 22 \n" +
"Fax: 0522 / 38 32 72 \n" +
"Partita IVA, Codice Fiscale \n" +
"Reg. Impr. di RE 00168780351 \n" +
"Cap. soc. € 50.000,00 i.v. \n" + "" +
"REA n. RE-107440 \n" +
"presso C.C.I.A.A. di Reggio Emilia";
ImageButton settings, helps, allerts, home;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Utils.darkenStatusBar(this, R.color.colorAccent);
server = new Server(this);
myDB = DataBaseHandler.getInstance(this);
msg = (TextView) findViewById(R.id.msg);
log = (TextView) findViewById(R.id.log_avviso);
settings = (ImageButton) findViewById(R.id.impo);
helps = (ImageButton) findViewById(R.id.aiut);
allerts = (ImageButton) findViewById(R.id.msge);
home = (ImageButton) findViewById(R.id.gab);
terminale = (TextView) findViewById(R.id.terminal);
indr = (TextView) findViewById(R.id.indr);
final Cursor cursor = myDB.fetchData();
if (cursor.moveToFirst()) {
do {
indr.setText(cursor.getString(1));
terminale.setText(cursor.getString(2));
Client.SERVER_IP = cursor.getString(1);
trm = cursor.getString(2);
} while (cursor.moveToNext());
}
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
ipp = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
startConnection.postDelayed(runnableConnection,5000);
startMessage.postDelayed(runnableMessage,5500);
cursor.close();
server.Parti();
home.setOnClickListener(new View.OnClickListener() {
int counter = 0;
#Override
public void onClick(View view) {
counter++;
if (counter == 10) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setMessage(s1);
builder.show();
counter = 0;
}
}
});
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent impostazioni = new Intent(getApplicationContext(), settingsLogin.class);
startActivity(impostazioni);
}
});
helps.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent pgHelp = new Intent(getApplicationContext(), help.class);
startActivity(pgHelp);
}
});
allerts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Server.count = 0;
SharedPreferences prefs = getSharedPreferences("MY_DATA", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.apply();
msg.setVisibility(View.INVISIBLE);
Intent pgAlert = new Intent(getApplicationContext(), allert.class);
startActivity(pgAlert);
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
server.onDestroy();
}
public static class ConnectTask extends AsyncTask<String, String, Client> {
#Override
protected Client doInBackground(String... message) {
client = new Client(new Client.OnMessageReceived() {
#Override
public void messageReceived(String message) {
messageReceived(message);
}
});
client.run();
return null;
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Log.d("test", "response " + values[0]);
}
}
static Handler startConnection = new Handler();
static Runnable runnableConnection = new Runnable() {
#Override
public void run() {
new ConnectTask().execute("");
}
};
static Handler startMessage = new Handler();
static Runnable runnableMessage = new Runnable() {
#Override
public void run() {
final Cursor cursor = myDB.fetchData();
if (cursor.moveToFirst()) {
do {
Client.SERVER_IP = cursor.getString(1);
trm = cursor.getString(2);
} while (cursor.moveToNext());
}
if (client != null) {
client.sendMessage(ipp + "#" + trm);
}
}
};
}
So what i'm trying to do is receive message from the server and visualize it in help.java activity in a TextView called msgServer set as static.
Actually i don't know which value i have to attribute to the help.msgServer.setText() and where to put it in MainActivity.
Fixed by setting in AsyncTask in MainActivity following code:
msgServer.setTextColor(Color.parseColor("#00FF00"));
msgServer.setText("ONLINE");
in the onProgressUpdate method.
So i identified the right place from where i can get the message sent by the server, the message is contained in:
values
.

Problems with data transmission from Android TCP Client app to ESP8266 module and vice versa

I am trying to write an Android app that could communicate over WiFi with ESP8266 module and exchange some simple, basic text data. My problem is I can not get any communication to work. I am not sure if my problem is with the Android code or some bad network configuration on the ESP.
On the Android side, I am using a standard TCPclient class code from this thread to transmit data. In this app I can get WiFi to work and to connect with ESP module using SSID and password authorization.
This is how my app looks like.
Here is TCPclient.java class code I used.
package com.example.wexfo.wifi_com;
import ...
public class TCPclient {
private String serverMessage;
public static final String SERVER_IP = "192.168.0.102";
public static final int SERVER_PORT = 4444;
private OnMessageReceived messageListener = null;
private boolean run = false;
public static final String LOG_TAG = "TCP";
PrintWriter out;
BufferedReader in;
public TCPclient(OnMessageReceived listener) {
messageListener = listener;
}
public void sendMessage(String message) {
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
}
}
public void stopClient() {
run = false;
if (out != null) {
out.flush();
out.close();
}
messageListener = null;
in = null;
out = null;
serverMessage = null;
}
public void run() {
run = true;
try {
InetAddress serverAddress = InetAddress.getByName(SERVER_IP);
Log.e(LOG_TAG, "C: Connecting...");
Socket socket = new Socket(serverAddress, SERVER_PORT);
try {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
Log.e(LOG_TAG, "C: Sent.");
Log.e(LOG_TAG, "C: Done.");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (run) {
serverMessage = in.readLine();
if (serverMessage != null && messageListener != null) {
messageListener.messageReceived(serverMessage);
}
//serverMessage = null;
}
Log.e(LOG_TAG, "S: Received message: '" + serverMessage + "'.");
}
catch (Exception e) {
Log.e(LOG_TAG, "S: Error", e);
}
finally {
socket.close();
}
}
catch (Exception e) {
Log.e(LOG_TAG, "C: Error", e);
}
}
public interface OnMessageReceived {
void messageReceived(String message);
}
}
Below is MainActivity.java code.
package com.example.wexfo.wifi_com;
import ...
public class MainActivity extends AppCompatActivity {
// Labels and edits
private TextView connectionText;
private EditText messageText;
private TextView chatText;
// Buttons
private ToggleButton connectButton;
private Button sendButton;
// Wifi connection
private static final String NET_SSID = "AI-THINKER";
private static final String NET_PASSWD = "aiTHINKERwifi";
private WifiConfiguration wifiConfig;
private WifiManager wifiManager;
// Other
private TCPclient tcpClient;
private void printChatLine(String text) {
chatText.append("\n>> " + text);
}
private void printChatLine(String who, String text) {
chatText.append("\n" + who + ": " + text);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectionText = (TextView) findViewById(R.id.connectionText);
messageText = (EditText) findViewById(R.id.messageText);
chatText = (TextView) findViewById(R.id.chatText);
chatText.setMovementMethod(new ScrollingMovementMethod());
printChatLine("chat test");
// WiFi setup (authorization hard-coded for now)
wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + NET_SSID + "\"";
wifiConfig.preSharedKey = "\"" + NET_PASSWD + "\"";
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(wifiConfig);
connectButton = (ToggleButton) findViewById(R.id.connectButton);
connectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (connectButton.isChecked()) {
connectionText.setText("Disconnect WiFi");
if (!wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(true);
List<WifiConfiguration> netList = wifiManager.getConfiguredNetworks();
for (WifiConfiguration net : netList) {
if (net.SSID != null && net.SSID.equals("\"" + NET_SSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(net.networkId, true);
wifiManager.reconnect();
break;
}
}
// Start server connection thread
new ConnectTask().execute("");
}
else {
connectionText.setText("Connect WiFi");
if (wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(false);
// Stop server connection thread
if (tcpClient != null)
tcpClient.stopClient();
}
}
});
sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String message = messageText.getText().toString();
printChatLine("ME", message);
messageText.setText("");
// Send message to ESP
if (tcpClient != null)
tcpClient.sendMessage(message);
}
});
}
public class ConnectTask extends AsyncTask<String,String,TCPclient> {
#Override
protected TCPclient doInBackground(String... message) {
tcpClient = new TCPclient(new TCPclient.OnMessageReceived() {
#Override
public void messageReceived(String message) {
publishProgress(message);
}
});
tcpClient.run();
return null;
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
// Received message from ESP
printChatLine("ES", values[0]);
}
}
}
Now the ESP8266 is set to work as SoftAP
AT+CWMODE?
+CWMODE:3
OK
Then I make a basic setup for multiple connections and start a new server.
AT+CIPSTA="192.168.0.102"
OK
AT+CIPMUX=1
OK
AT+CIPSERVER=1,4444
OK
Here is my IP data.
AT+CIFSR
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"1a:fe:34:8e:81:58"
+CIFSR:STAIP,"192.168.0.102"
+CIFSR:STAMAC,"18:fe:34:8e:81:58"
OK
Now when I run my app I can confirm I am connected to ESP's access point.
AT+CWLIF
192.168.4.2,00:27:15:77:82:02
OK
However there is no sign of an opened connection on the ESP and clicking SEND button does not make any data transfer. When I try to connect through ESP, I get an ERROR.
AT+CIPSTART=0,"TCP","192.168.4.2",4444
0,CLOSED
ERROR
My feeling is the app works fine but I do something horribly wrong on the ESP side and I can not figure out what it is. Maybe I have a wrong idea about the entire setup?

Why are my client threads/listeners not receiving broadcasted messages from the server?

I made a simple prototype of a client-server application on Android
I managed to connect two clients to the server and the server can receive their messages. The problem now is that I can't seem to broadcast/receive the messages to other clients.
I try to broadcast the received message through a for loop in the Server class:
private void broadcastMessage(String message) {
for (int i = 0, j = clients.size(); i <= j; i++) {
PrintWriter out = null;
Socket socket = clients.get(i);
try {
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// WHERE YOU ISSUE THE COMMANDS
out.println(message);
Log.d("SERVER Loop", "Broadcasting messages...");
out.close();
}
Log.d("SERVER", "Message Brodcasted");
}
This I then try to receive through a listener in the Client class :
public class ClientThreadListener implements Runnable {
protected Socket serverSocket = null;
protected String mMsgFromServer;
public ClientThreadListener(Socket serverSocket) {
this.serverSocket = serverSocket;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
serverSocket.getInputStream()));
while ((mMsgFromServer = in.readLine()) != null) {
Log.d("MESSAGE FROM SERVER: ", mMsgFromServer);
handler.post(new Runnable() {
#Override
public void run() {
msgFromOtherClients.append('\n'
+ "Message From Server: " + mMsgFromServer);
}
});
}
} catch (Exception e) {
Log.e("ClientListener", "C: Error", e);
connected = false;
}
}
}
I don't get any errors or force closes though. Forgive me I know it is very messy but please bear with me and please focus on the issue at hand instead :D
Here is the full code for the Server class
public class Server extends Activity {
private TextView serverStatus;
// DEFAULT IP
public static String SERVERIP = "10.0.2.15";
// DESIGNATE A PORT
public static final int SERVERPORT = 8080;
private Handler handler = new Handler();
private ServerSocket serverSocket;
private String mMsgFromClient;
private MultiThreadedServer server;
private ArrayList<Socket> clients = new ArrayList<Socket>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server);
serverStatus = (TextView) findViewById(R.id.server_status);
// SERVERIP = getLocalIpAddress();
server = new MultiThreadedServer(8080);
new Thread(server).start();
}
public class MultiThreadedServer implements Runnable {
protected int serverPort = 8080;
protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
protected Thread runningThread = null;
public MultiThreadedServer(int port) {
this.serverPort = port;
}
public void run() {
synchronized (this) {
this.runningThread = Thread.currentThread();
}
openServerSocket();
while (!isStopped()) {
Socket clientSocket = null;
try {
clientSocket = this.serverSocket.accept();
clients.add(clientSocket);
} catch (IOException e) {
if (isStopped()) {
Log.d("SERVER TEXT", "Server Stopped.");
return;
}
throw new RuntimeException(
"Error accepting client connection", e);
}
new Thread(new WorkerRunnable(clientSocket, this)).start();
}
Log.d("SERVER TEXT", "Server Stopped.");
}
private synchronized boolean isStopped() {
return this.isStopped;
}
public synchronized void stop() {
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}
private void openServerSocket() {
try {
this.serverSocket = new ServerSocket(this.serverPort);
} catch (IOException e) {
throw new RuntimeException("Cannot open port 8080", e);
}
}
private void broadcastMessage(String message) {
for (int i = 0, j = clients.size(); i <= j; i++) {
PrintWriter out = null;
Socket socket = clients.get(i);
try {
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// WHERE YOU ISSUE THE COMMANDS
out.println(message);
Log.d("SERVER Loop", "Broadcasting messages...");
out.close();
}
Log.d("SERVER", "Message Brodcasted");
}
}
public class WorkerRunnable implements Runnable {
protected Socket clientSocket = null;
protected String mMsgFromClient = null;
private UUID id;
public WorkerRunnable(Socket clientSocket, MultiThreadedServer server) {
this.clientSocket = clientSocket;
id = UUID.randomUUID();
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
while ((mMsgFromClient = in.readLine()) != null) {
handler.post(new Runnable() {
#Override
public void run() {
serverStatus.append('\n'
+ "Message From Client ID " + getID()
+ ": " + mMsgFromClient);
}
});
}
Log.d("SERVERTEXT", "Proceed to broadcast");
server.broadcastMessage(mMsgFromClient);
} catch (IOException e) {
Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
serverStatus
.append('\n'
+ "Message From Client ID "
+ getID()
+ ": "
+ "Oops. Connection interrupted. Please reconnect your phones.");
}
});
e.printStackTrace();
}
}
private String getID() {
return id.toString();
}
}
}
Here is the full code for the Client class
public class Client extends Activity {
private EditText serverIp;
private EditText chatMsg;
private Button connectPhones;
private Button sendMsg;
private TextView msgFromOtherClients;
private String serverIpAddress = "";
private boolean connected = false;
private boolean willSendMsg = false;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
serverIp = (EditText) findViewById(R.id.server_ip);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener(connectListener);
chatMsg = (EditText) findViewById(R.id.chat_msg);
sendMsg = (Button) findViewById(R.id.send_msg);
sendMsg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
willSendMsg = true;
}
});
msgFromOtherClients = (TextView) findViewById(R.id.msg_from_other_clients);
}
private OnClickListener connectListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
}
};
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, Server.SERVERPORT);
connected = true;
Thread listener = new Thread(new ClientThreadListener(new Socket(serverAddr, Server.SERVERPORT)));
listener.start();
while (connected) {
if (willSendMsg) {
willSendMsg = false;
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
// WHERE YOU ISSUE THE COMMANDS
out.println(chatMsg.getText().toString());
Log.d("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}
public class ClientThreadListener implements Runnable {
protected Socket serverSocket = null;
protected String mMsgFromServer;
public ClientThreadListener(Socket serverSocket) {
this.serverSocket = serverSocket;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
serverSocket.getInputStream()));
while ((mMsgFromServer = in.readLine()) != null) {
Log.d("MESSAGE FROM SERVER: ", mMsgFromServer);
handler.post(new Runnable() {
#Override
public void run() {
msgFromOtherClients.append('\n'
+ "Message From Server: " + mMsgFromServer);
}
});
}
} catch (Exception e) {
Log.e("ClientListener", "C: Error", e);
connected = false;
}
}
}
}
Your code has some issue that prevents it from working.
As already said in other answers, in your code you are closing the socket output stream right after sending the message to the client. call close() only out of your for message loop. Of course closing the socket in the client will have the same effect as closing it on the server. You must close the sockets only when client and server have finished talking. Closing it while transmitting data it's like hanging up the phone in the middle of a conversation.
Second, you create a new socket on the client side:
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, Server.SERVERPORT);
but then you pass to the listener another, newly created, socket (I suppose this is not intended):
connected = true;
Thread listener = new Thread(new ClientThreadListener(new Socket(serverAddr, Server.SERVERPORT)));
listener.start();
Third, always call flush() on an output stream right after sending data, or the data will likely not be sent (the send methods will just enqueue your data in the sending buffer).
Last (This may not be useful to you since I don't know your ultimate goal), if you need to send and receive on sockets, 90% of the time it's better and easier to do this asinchronously, using separate threads for listening and sending.
If it still doesn't work, add here some output or log trace from logcat.
You need to move the line:
server.broadcastMessage(mMsgFromClient);
inside the while:
while ((mMsgFromClient = in.readLine()) != null) {
handler.post(new Runnable() {
#Override
public void run() {
serverStatus.append('\n'
+ "Message From Client ID " + getID()
+ ": " + mMsgFromClient);
}
});
// HERE
Log.d("SERVERTEXT", "Proceed to broadcast");
server.broadcastMessage(mMsgFromClient);
}
Otherwise, you'll only broadcast null.
EDIT: You should make sure that mMsgFromClient is not changed between posting the new Runnable and it actually executing. The best way is to initialize a field in the anonymous class with the current value, and log the value of that field instead.
EDIT2: Unless your server is supposed to close its connection to a client after sending it a broadcast message, you should use out.flush() instead of out.close() in the broadcastMessage method. It's preferrable that client connections are closed after a timeout, or just let the clients disconnect, again with a timeout.
Otherwise, your test will be very limited most of the times: a client connects and sends a message; then it receives its own message and the server closes the connection.
Please try to use AsyncTask in android which will create separate thread for communication with server.

UDP server running on android for LAN

i have server and clients running in the LAN,but my server don't accept more than one message from client app. in LAN, what code shall i put in while loop in order to accept as many messages as i send from client app ?
My server class is:
public class MainActivity extends Activity {
private Boolean shouldRestartSocketListen=true;
TextView tv1;
//MulticastLock lock;
static String UDP_BROADCAST = "UDPBroadcast";
DatagramSocket socket;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.textView1);
startListenForUDPBroadcast();
// lock.release();
}
private void listenAndWaitAndThrowIntent(InetAddress broadcastIP, Integer port) throws Exception {
byte[] recvBuf = new byte[15000];
if (socket == null || socket.isClosed()) {
socket = new DatagramSocket(port, broadcastIP);
//socket.setBroadcast(true);
}
//socket.setSoTimeout(1000);
DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
Log.e("UDP", "Waiting for UDP broadcast");
socket.receive(packet);
String senderIP = packet.getAddress().getHostAddress();
String message = new String(packet.getData()).trim();
// Log.e("UDP", "Got UDB broadcast from " + senderIP + ", message: " + message);
tv1.setText(message);
// broadcastIntent(senderIP, message);
socket.close();
}
private void broadcastIntent(String senderIP, String message) {
Intent intent = new Intent(MainActivity.UDP_BROADCAST);
intent.putExtra("sender", senderIP);
intent.putExtra("message", message);
sendBroadcast(intent);
}
Thread UDPBroadcastThread;
void startListenForUDPBroadcast() {
UDPBroadcastThread = new Thread(new Runnable() {
public void run() {
try {
InetAddress broadcastIP = InetAddress.getByName("192.168.1.255"); //172.16.238.42 //192.168.1.255
Integer port = 11111;
while (shouldRestartSocketListen) {
listenAndWaitAndThrowIntent(broadcastIP, port);
}
//if (!shouldListenForUDPBroadcast) throw new ThreadDeath();
} catch (Exception e) {
Log.i("UDP", "no longer listening for UDP broadcasts cause of error " + e.getMessage());
}
}
});
UDPBroadcastThread.start();
}
void stopListen() {
shouldRestartSocketListen = false;
socket.close();
}
public void onCreate() {
};
#Override
public void onDestroy() {
stopListen();
}
public IBinder onBind(Intent intent) {
return null;
}
}
Don't close the socket unless you want to stop listening (put it outside your while loop).
I've tried using UDPFlood v2.0 from McAfee, with the socket.close() like you have it, you will miss messages as soon there are more than ~5 packets per second (depending how fast your other tasks are). The messages get buffered in your socket (OS) but you discard them by closing it.

How do I get an IP address from an EditText box?

Hi I am quite new to android and i'm sorry if its a silly question.
How do I use an EditText box instead of having the IP address hard coded.
As you can see from the code below I have to put the computers ip address that im connecting to in, what I want to do is make it so I use an EditText box instead such as:
String input= edittextIP.getText().toString();
But i could not get this to work. Any help would be much appreciated.
Thanks
public class Client {
private String serverMessage;
public final String SERVERIP = "100.100.100.61"; //computer IP address
public final int SERVERPORT = 4444;
private OnMessageReceived mMessageListener = null;
private boolean mRun = false;
PrintWriter out;
BufferedReader in;
/**
* Constructor of the class. OnMessagedReceived listens for the messages received from server
*/
public Client(OnMessageReceived listener) {
mMessageListener = listener;
}
/**
* Sends the message entered by client to the server
* #param message text entered by client
*/
public void sendMessage(String message){
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
}
}
public void stopClient(){
mRun = false;
}
public void run() {
mRun = true;
try {
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.e("TCP Client", "C: Connecting...");
//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, SERVERPORT);
try {
//send the message to the server
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
Log.e("TCP Client", "C: Sent.");
Log.e("TCP Client", "C: Done.");
//receive the message which the server sends back
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in this while the client listens for the messages sent by the server
while (mRun) {
serverMessage = in.readLine();
if (serverMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(serverMessage);
}
serverMessage = null;
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
//the socket must be closed. I can't reconnect to this socket
// after it is closed, which means a new socket has to be created.
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
/// public EditText findViewByid(int edittextIP) { <--- this was previous try please
/// disregard this
///
/// return null;
/// }
public interface OnMessageReceived {
public void messageReceived(String message);
}
}
This is my activity below:
public class MyActivity extends Activity
{
private ListView mList;
private ArrayList<String> arrayList;
private Adapter mAdapter;
private Client mTcpClient;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arrayList = new ArrayList<String>();
final EditText editText = (EditText) findViewById(R.id.editText);
Button send = (Button)findViewById(R.id.send_button);
mList = (ListView)findViewById(R.id.list);
mAdapter = new Adapter(this, arrayList);
mList.setAdapter(mAdapter);
// connect to the server
new connectTask().execute("");
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
String message = editText.getText().toString();
arrayList.add("c: " + message);
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
//refresh the list
mAdapter.notifyDataSetChanged();
editText.setText("");
}
});
}
public class connectTask extends AsyncTask<String,String,Client> {
#Override
protected Client doInBackground(String... message) {
//create a TCPClient object and
mTcpClient = new Client(new Client.OnMessageReceived() {
//here the messageReceived is implemented
public void messageReceived(String message) {
publishProgress(message);
}
});
mTcpClient.run();
return null;
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
arrayList.add(values[0]);
mAdapter.notifyDataSetChanged();
}
}
}
You should get the EditText instance from your Activity (the class that extends from Activity), your "findViewById" function has no sense there. "findViewById" is a method inherited from Activity. Then, you could pass the text typed in that EditText from your Activity to your Client instance, for example.
You missed to cast the value and generated an wrong stub. You need to delete your findViewByid function. Try this code:
TextEdit input = (TextEdit)findViewByid(...);
Note that you should pass directly the IP to your class from your activity for example thrue the constructor:
public class ConnectTask extends AsyncTask<String,String,Client> {
private string ip;
public ConnectTask(String ip) {
this.ip=ip;
}
// ...
There you have the IP in the member variable ip. You can set it like this:
ConnectTask task = new ConnectTask(input.getText().toString());

Categories

Resources