Hi,
I'm hoping someone here can help me, I am trying to create a basic application that transmits GPS co ordinates of the device to a server. I am a complete noobie to socket programming (well, I covered the bare basics in college this year).
As a stepping stone, I am following a tutorial, that creates the server, and the android client, it is designed to send an input from the user to the server and the server prints it on the console.
I'm becoming quite familar with how to set up the basics of the server and the client i.e. opening ports etc. The problem with this is, when hit send, the app crashes, when tested on the emulator, there is obviously an issue, but I can't see it.
When I test with device, and hit send, there is nothing printed to the console. I've attached both the client and server code below, the ip in the code below is for the emulator, I change this to my device ip with testing with it.
****SERVER****
public class Additional_Server {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
public static void main(String[] args) {
try {
serverSocket = new ServerSocket(2001); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 2001");
}
System.out.println("Server started. Listening to the port 2001");
while (true) {
try {
clientSocket = serverSocket.accept(); // accept the client connection
inputStreamReader = new InputStreamReader(
clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
message = bufferedReader.readLine();
System.out.println(message);
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
}
****CLIENT****
import android.os.Bundle;
import android.app.Activity;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class TCPclient extends Activity {
private Socket client;
private PrintWriter printwriter;
private EditText textField;
private Button button;
private String messsage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tcpclient);
textField = (EditText) findViewById(R.id.Msg); // reference to the text field
button = (Button) findViewById(R.id.bSend); // reference to the send button
// Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
messsage = textField.getText().toString(); // get the text message on the text field
textField.setText(""); // Reset the text field to blank
try {
client = new Socket("127.0.0.1", 2001); // connect to server
printwriter = new PrintWriter(client.getOutputStream(),
true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
client.close(); // closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
I really would appreciate any help, I'm pulling my hair out here. Any specifics would be great. Thanks in advance!!!
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT
Thanks: 235
Thanked 466 Times in 289 Posts
Re: android server client communication issue
Do you have the INTERNET permission declared in your manifest?
Can you post the log cat output?
__________________
My blog, Inverted Keystrokes, is looking for aspiring developers (not necessarily with Android) to post articles. If you have any development experience and are interested in participating, please PM me. =)
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT
Thanks: 235
Thanked 466 Times in 289 Posts
Re: android server client communication issue
Okay, so i looked at your code again, and the problem is that after you accept the connection on the server, you immediately try to read from the buffer. At this point, all kinds of async issues could be happening. First, i would begin by removing all messageing code and make sure the client is actually able to connect to the server. Throw in some logging lines on both server and client and see what you find.
After you know that is working, i would then make your server construct a new thread that will listen for incoming messages in an infinite loop. This way the message passing is asyncronous and the server can continue to listen for new connections.
I tried two things, one, I tried to put my socket code within an async task, this didn't seem to work for me.
2. I tried to strip it back, I just have a simple server client which I have posted below.
SERVER
public class Additional_Server {
private static String message;
public static void main(String[] args) throws Exception {
Socket s;
ServerSocket ss = new ServerSocket(2001);
System.out.println("Server started. Listening to the port 2001");
while (true) {
try {
System.out.println("Server: waiting for connection ..");
s = ss.accept();
InputStream in = s.getInputStream();
Scanner r = new Scanner(in);
OutputStream o = s.getOutputStream();
PrintWriter p = new PrintWriter(o);
String inputLine;
inputLine = r.nextLine();
p.println("Hello " + inputLine + " from Gary");
p.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
}
CLIENT
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Socket s;
private PrintWriter p;
TextView display;
Button test;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = (Button) findViewById(R.id.test);
display = (TextView) findViewById(R.id.Sdisplay);
test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
s = new Socket("192.168.1.*", 2001); // connect to server
OutputStream o = s.getOutputStream();
PrintWriter p = new PrintWriter(o);
InputStream in = s.getInputStream();
Scanner r = new Scanner(in);
p.println("Gary");
p.flush();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
I'm just trying to get the printout to the console, but nothing is happening. I'm trying to do a project on this for college, we haven't done android programming or socket programming before...
What really bothers me here is that it looks right and I can't seem to see what the issue is.
I really would appreciate some of your thoughts or even what you might do in this situation?
I have been stuck on this hurdle for weeks now, and normally I don't mind plugging away, but time is running out for me...