sending packet over 3G network - java

I am trying to write a method that will send a message over a 3G network with a base station to the server. IM trying to send the message multiple times until I decide to stop. But when I tested this, it always stops after a short time and stops sending the message. Anyone know why?
private Runnable commRunnable = new Runnable() {
public void run() {
try {
String message = "Just saying hello!";
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true);
String startReceivingMessage = "Begin sending me data.";
String stopReceivingMessage = "Stop sending me data.";
startSend = false;
stopSend = false;
startReceive = false;
stopReceive = false;
while (!shouldDisconnect) {
if (startSend) {
sendData = true;
startSend = false;
}
if (stopSend) {
sendData = false;
stopSend = false;
}
// Send a message that the server should start transmitting data
// back to us. We only need to transmit this message once.
if (startReceive) {
out.println(startReceivingMessage);
startReceive = false;
receiveData = true;
Thread receiveThread = new Thread(receiveRunnable);
receiveThread.start();
// Tell the server to stop transmitting data.
} else if (stopReceive) {
out.println(stopReceivingMessage);
stopReceive = false;
receiveData = false;
}
if (sendData) {
out.println(message);
}
Thread.sleep(20);
}
} catch (Exception e) {
Log.e("PowerMonitor", e.toString());
} finally {
try {
socket.close();
connected = false;
} catch (Exception e) {
Log.e("PowerMonitor", e.toString());
}
}
}
};
private Runnable receiveRunnable = new Runnable() {
#Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String reply = "";
Log.d("PowerMonitor", "Starting to receive");
while (receiveData) {
Log.d("PowerMonitor", "Listening...");
reply = in.readLine();
Log.d("PowerMonitor", "Got message: " + reply);
}
} catch (Exception e) {
Log.e("PowerMonitor", e.toString());
}
}
};

We need more of your code for better understanding of your question, but from the code you posted here its seems like the socket is being closed by the
socket.close();
call in the finally block.
Also tell about any errors you are getting.

Related

Why the Android TCP Client is not working?

I'm developing an apllication on Android Studio, which has the job of send an string to an C# TCP server on my computer.
My android app has the following code:
public void Send_Command(View v) {
testClass();
Toast.makeText(getApplicationContext(), "Comando Enviado!", Toast.LENGTH_LONG).show();
}
public static void testClass() {
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
public static class ClientThread implements Runnable {
String results = "";
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
System.out.println("C: Connecting...");
while (true) {
results = "";
try {
Socket socket = new Socket("192.168.1.77", 8888);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
out.write("Test");
out.flush();
String inMsg = "";
boolean b = false;
while (!b) {
inMsg = in .readLine();
if (inMsg != "")
b = true;
}
socket.close();
System.out.println("C: Closed.");
} catch (Exception e) {
System.out.println("S: Error" + e.toString());
}
}
} catch (Exception e) {
System.out.println("C: Error" + e);
}
}
}
I put the Send_Message on the onClick of a button and when I execute the app with the C# server on my PC, nothing happends, however, if I execute the same code that I have on android studio on Ecplipse, the string is sended.
I don't have many experience on Android Studio, so, perhaps I am commiting an stupid mistake, however, I don't know where.
Anyone know where the problem might be ?
Thanks

Android client/server application - proper way to receive messages continously

I'm trying to make a client/server application using an Android phone as a client using AsyncTask to send messages from UI.
I've written some very basic implementation just to test the connection and the way that messages are received / sent and I found a very big problem.
The client part seems to work fine..from my perspective. But the server part is the problem. I can't make the server reading and displaying messages countinously from the client.
I tried something like while(line = (in.readLine()) != null) {} but it doesn't seems to work.
After I sent my first word from the client, the server reads null and it stops.
Can someone show me a proper way to keep the server running while the client is not sending nothing?
I'd like to avoid using while(true) if it's not 100% necessary.
Here is the implementation until now:
Server:
public class SocketServerThread extends Thread {
private static final Logger log = Logger.getLogger(SocketServerThread.class);
private static final int SERVER_PORT_NUMBER = 5000;
#Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(SERVER_PORT_NUMBER);
serverSocket.setReuseAddress(true);
log.info("Waiting for connection...");
Socket clientSocket = serverSocket.accept();
log.info("Connected! Receiving message...");
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
try {
while (true) {
String line = in.readLine();
if (line != null) {
log.info(line);
}
}
} catch (Exception e) {
log.error("Unexpected exception while sending / receiving messages.");
e.printStackTrace();
} finally {
in.close();
clientSocket.close();
serverSocket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Client:
public class MyAsyncTask extends AsyncTask<String, Integer, String> {
private static final String TAG = "MyAsyncTask";
private static final String SERVER_IP_ADDRESS = "10.0.2.2";
private static final int SERVER_PORT_NUMBER = 5000;
private PrintWriter out;
#Override
protected String doInBackground(String... params) {
String message = "";
try {
InetAddress address = InetAddress.getByName(SERVER_IP_ADDRESS);
Log.d(TAG, "Connecting...");
Socket socket = new Socket(address, SERVER_PORT_NUMBER);
try {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
Log.d(TAG, "I/O created");
message = params[0];
if (!message.equals("stop")) {
sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
out.flush();
out.close();
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
private void sendMessage(String message) {
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
Log.d(TAG, "Sent message: " + message);
}
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d(TAG, "onPostExecute(), s: " + s);
}
Thank you.
The problem is that your BufferedReader only read the first input stream. In order to receive the text after that, you have to re-read the input stream. I do it by recreating the socket when I am done reading, so that I can read next coming data. I am using the following code in my app. You can use this
private ServerSocket serverSocket;
public static final int SERVERPORT = 5000;
Thread serverThread = null;
public void startSocketServer(){
this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
}
public void stopSocket(){
if(serverSocket != null){
try{
serverSocket.close();
}
catch (IOException e){
e.printStackTrace();
}
}
}
class ServerThread implements Runnable {
public void run() {
Socket socket = null;
try {
Log.wtf(TAG,"Socket: New Socket");
serverSocket = new ServerSocket(SERVERPORT);
if(serverSocket == null){
runOnUiThread(new Runnable() {
#Override
public void run() {
startSocketServer();
}
});
return;
}
while (!Thread.currentThread().isInterrupted() && !serverSocket.isClosed()) {
try {
socket = serverSocket.accept();
Log.wtf(TAG,"Socket: Accepting");
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
} catch (IOException e) {
Log.wtf(TAG,"Socket: Error");
e.printStackTrace();
}
if(Thread.currentThread().isInterrupted()){
Log.wtf(TAG, "Thread Interrupted");
}
if(serverSocket.isClosed()){
Log.wtf(TAG, "serverSocket closed");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class CommunicationThread implements Runnable {
private Socket clientSocket;
public CommunicationThread(Socket clientSocket) {
this.clientSocket = clientSocket;
log.info("Connected! Receiving message...");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
try {
while (true) {
String line = in.readLine();
if (line != null) {
log.info(line);
}
else
break;//This will exit the loop and refresh the socket for next data
}
} catch (Exception e) {
log.error("Unexpected exception while sending / receiving messages.");
e.printStackTrace();
}
finally {
in.close();
clientSocket.close();
}
refreshSocket();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void refreshSocket(){
runOnUiThread(new Runnable() {
#Override
public void run() {
stopSocket();
startSocketServer();
}
});
}
Just call startSocketServer() to start the server socket in your code.

Android - PC usb connection - do it without wifi

I wanted to ask how to change following code, which needs USB connection and WIFI to work... (and I don't know why wifi...), to code, which needs only USB cable and NO WIFI!, because I don't want to be dependent on wifi...
Could you please help me? Some changes or additions in code? Thanks.
Code for Android:
private final Runnable connectToServer = new Thread()
{
#Override
public void run()
{
try
{// Get the server address from a dialog box.
String serverAddress = "192.168.0.23";
// Make connection and initialize streams
Socket socket = new Socket(serverAddress, 38300);
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// Consume the initial welcoming messages from the server
for (int i = 0; i < 3; i++) {
System.out.println(in.readLine());
}
solveCube();
} catch (IOException e) {
e.printStackTrace();
}
}
};
private final Runnable initializeConnection = new Thread()
{
#Override
public void run()
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(generateCubeString());
out.println(generateCubeString());
String response ="";
try {
response = in.readLine();
if (response == null || response.equals("")) {
System.exit(0);
}
} catch (IOException ex) {
}
if (response.contains("Error")) {
} else {
solveCubeAnimate(response);
}
System.out.println(response);
final String finalResponse = response;
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(finalResponse);
}
});
}
};
Code for PC
private static class Capitalizer extends Thread {
private Socket socket;
private int clientNumber;
public Capitalizer(Socket socket, int clientNumber) {
this.socket = socket;
this.clientNumber = clientNumber;
log("New connection with client# " + clientNumber + " at " + socket);
}
/**
* Services this thread's client by first sending the
* client a welcome message then repeatedly reading strings
* and sending back the capitalized version of the string.
*/
public void run() {
try {
// Decorate the streams so we can send characters
// and not just bytes. Ensure output is flushed
// after every newline.
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// Send a welcome message to the client.
out.println("Hello, you are client #" + clientNumber + ".");
out.println("Enter a line with only a period to quit\n");
// Get messages from the client, line by line; return them
// capitalized
while (true) {
String input = in.readLine();
if (input == null || input.equals(".")) {
break;
}
out.println(solveCube(input));
}
} catch (IOException e) {
log("Error handling client# " + clientNumber + ": " + e);
} finally {
try {
socket.close();
} catch (IOException e) {
log("Couldn't close a socket, what's going on?");
}
log("Connection with client# " + clientNumber + " closed");
}
}
/**
* Logs a simple message. In this case we just write the
* message to the server applications standard output.
*/
private void log(String message) {
System.out.println(message);
}
}
private static class Connecter extends Thread {
/**
* Services this thread's client by first sending the
* client a welcome message then repeatedly reading strings
* and sending back the capitalized version of the string.
*/
public void run() {
try {
System.out.println("The capitalization server is running.");
int clientNumber = 0;
ServerSocket listener = new ServerSocket(38300);
try {
while (true) {
new Capitalizer(listener.accept(), clientNumber++).start();
}
} finally {
listener.close();
}
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

Java Sockets sending multiple objects to the same server

I'm trying to send multiple Objects through a socket to a java server.
To have a gerneral type I convert my messages into an instance of the class Message and send this object to the server.
I wrote a little testclass, which sends three objects to the server.
The problem is, only one objects reaches the server.
I tried nearly everything, without success.
My Server:
public class Server {
private ServerConfig conf = new ServerConfig();
private int port = Integer.parseInt(conf.loadProp("ServerPort"));
Logger log = new Logger();
ServerSocket socket;
Chat chat = new Chat();
public static void main(String[] args) {
Server s = new Server();
if (s.runServer()) {
s.listenToClients();
}
}
public boolean runServer() {
try {
socket = new ServerSocket(port);
logToConsole("Server wurde gestartet!");
return true;
} catch (IOException e) {
logToConsole("Server konnte nicht gestartet werden!");
e.printStackTrace();
return false;
}
}
public void listenToClients() {
while (true) {
try {
Socket client = socket.accept();
ObjectOutputStream writer = new ObjectOutputStream(client.getOutputStream());
Thread clientThread = new Thread(new Handler(client, writer));
clientThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void logToConsole(String message) {
System.out.print(message);
}
public class Handler implements Runnable {
Socket client;
ObjectInputStream reader;
ObjectOutputStream writer;
User user;
public Handler(Socket client, ObjectOutputStream writer) {
try {
this.client = client;
this.writer = writer;
this.reader = new ObjectInputStream(client.getInputStream());
this.user = new User();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
while (true) {
Message incomming;
try {
while ((incomming = (Message) reader.readUnshared()) != null) {
logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n");
logToConsole(
"Vom Client: \n" + incomming.getType() + "-----" + incomming.getValue().toString());
handle(incomming);
}
} catch (SocketException se) {
se.printStackTrace();
Thread.currentThread().interrupt();
} catch (IOException ioe) {
ioe.printStackTrace();
Thread.currentThread().interrupt();
} catch (ClassNotFoundException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
private void handle(Message m) throws IOException {
String type = m.getType();
if (type.equals(config.ConstantList.Network.CHAT.toString())) {
chat.sendMessage(m);
} else if (type.equals(config.ConstantList.Network.LOGIN.toString())) {
System.out.println(user.login(m.getValue().get(0), writer));
System.out.println(m.getValue().get(0));
}
}
}
}
The Client:
public class Connect {
Socket client = null;
ObjectOutputStream writer = null;
ObjectInputStream reader = null;
private Config conf = new Config();
//private String host = conf.loadProp("ServerIP");
String host = "localhost";
private int port = Integer.parseInt(conf.loadProp("ServerPort"));
public boolean connectToServer() {
try {
client = new Socket(host, port);
reader = new ObjectInputStream(client.getInputStream());
writer = new ObjectOutputStream(client.getOutputStream());
logMessages("Netzwerkverbindung hergestellt");
Thread t = new Thread(new MessagesFromServerListener());
t.start();
return true;
} catch (Exception e) {
logMessages("Netzwerkverbindung konnte nicht hergestellt werden");
e.printStackTrace();
return false;
}
}
public boolean isConnectionActive() {
if (client == null || writer == null || reader == null){
return false;
}else{
return true;
}
}
public void sendToServer(Message m) {
try {
writer.reset();
writer.writeUnshared(m);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
And I try to send the objects with the class:
public void sendChatMessage(String username, String message) throws InterruptedException {
ChatMessage cm = new ChatMessage();
cm.setChat(username, null, message);
Message m = new Message(cm);
conn.sendToServer(m);
System.out.println("SENDED");
}
public static void main(String[] args) throws InterruptedException {
String username = "testuser";
String chatmessage = "Hallo Welt!";
connect.connect();
sendChatMessage(username, chatmessage);
sendChatMessage(username, chatmessage);
sendChatMessage(username, chatmessage);
}
I know that this is always the same message, but it is only for test purposes.
The messages are the objects they are Serializable and with only one object it works as designed.
Does anyone can see where I made my mistake?
while ((incomming = (Message) reader.readUnshared()) != null) {
Here you are reading an object, and blocking until it arrives.
logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n");
Here you are reading another object, and blocking till it arrives, and then erroneously logging it as the object you already read in the previous line.
Instead of logging reader.readObject(), you should be logging the value of incoming, which you have also misspelt.
And the loop is incorrect. readObject() doesn't return null at end of stream: it throws EOFException. It can return null any time you write null, so using it as a loop termination condition is completely wrong. You should catch EOFException and break.
Found the solution, the line logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n"); in the Server class, blocks the connection.

Infinit Loop when socket client disconnect

I had implemented a Java Multithreading Socket Server. It's working fine and create a new thread for every new client connection. But I have a bug when the TCP connection is cutted by the client, the server enter in a infinit loop and i got this message for every loop:
ERROR : Bad Frame !!! : null
I tried to debug the execution and change the if else condition but always the same bug exist when a client disconnect.
Here is my server Code
public class TCPSockServer implements Runnable {
Socket sock;
static int counter = 0;
private static int TIMEOUT = 50000;
private static int MAX_TIMEOUT = 100000;
long lastReadTime;
public TCPSockServer(Socket sock) {
this.sock = sock;
}
public static void main(String[] args) {
try {
ServerSocket serverSock = new ServerSocket(12000);
System.out.println("TCPSockServer : Listening to PORT 12000 ...");
while (true) {
Socket newSock = serverSock.accept();
counter++;
InetAddress addr = newSock.getInetAddress();
System.out.println("TCPSockServer : Connection Number : "+ counter);
System.out.println("TCPSockServer : Connection made to "
+ addr.getHostName() + " : (" + addr.getHostAddress()
+ ")");
newSock.setSoTimeout(TIMEOUT);
newSock.setKeepAlive(true);
new Thread(new TCPSockServer(newSock)).start();
}
} catch (IOException e) {
System.err.println("Trackiz: Main : ERROR Connection Failed");
e.printStackTrace();
}
}
#Override
public void run() {
int clientID = counter;
try {
BufferedReader inStream = new BufferedReader(new InputStreamReader(
sock.getInputStream()));
StringBuilder inString = new StringBuilder();
String frame = null;
PrintStream outStream = new PrintStream(sock.getOutputStream());
while (true) {
if (inString.append((String) inStream.readLine()) == null) {
System.out.println("TCPSockServer : CLIENT NOT CONNECTED");
sock.close();
break;
} else {
lastReadTime = System.currentTimeMillis();
frame = inString.toString();
if (cond1(frame)) {
......
}else{
system.err.println("\nERROR : Bad Frame !!! : "+frame); // why it enter this else in an infinit loop
}
outStream.println(provt.sendCommand("TCPSockServer : ACK TO CLIENT"));
inString = null;
inString = new StringBuilder();
}
}
inStream.close();
outStream.close();
sock.close();
} catch (SocketTimeoutException e) {
if (!isConnectionAlive()) {
System.out.println("\nCONNECTION TERMINATED FROM CLIENT !"
+ clientID);
logger.log(Level.SEVERE, "TCPSockServer : Connection terminated with Client");
try {
sock.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
// TODO sendHeartBeat();
System.out.println("Sending HeartBeat ...");
}
e.printStackTrace();
} catch (IOException e) {
System.err.println("TCPSockServer : Connection Timeout. Try to reconnect !");
e.printStackTrace();
}
}
public boolean isConnectionAlive() {
return System.currentTimeMillis() - lastReadTime < MAX_TIMEOUT;
}
}
The append method will never return null, so this is not right, the if-branch will never be entered:
if (inString.append((String) inStream.readLine()) == null) {
The code should probably look more like this:
String line = inStream.readLine();
if (line == null) {
...
} else {
inString.append(line);
...
}

Categories

Resources