Hi,
I'm really hoping someone here can help me out. I'm working on an android project in college, I haven't done android programming, nor have I done any socket programming in college. My first step in this project is to have a client server communication. I have, based on notes and tutorials etc I have found online created a simple client and server, where a string is sent to the server when a button is clicked on the client side. This string is then mean to print to console...
My end game is to send gps co ordinates to my server where another client will take them from the server. I'm doing this as the first step, and build on it.
I've included my code below, unfortunately, the string doesn't print to the console, it looks correct to me. Could someone please have a quick look over this and see where I might be going wrong?
I'm pulling my hair out at this stage!!
SERVER
CLIENT
I'd really appreciate any pointers or help, as I've said, I'm very new to android programming so all help appreciated!!
Regards,
Gary
I'm really hoping someone here can help me out. I'm working on an android project in college, I haven't done android programming, nor have I done any socket programming in college. My first step in this project is to have a client server communication. I have, based on notes and tutorials etc I have found online created a simple client and server, where a string is sent to the server when a button is clicked on the client side. This string is then mean to print to console...
My end game is to send gps co ordinates to my server where another client will take them from the server. I'm doing this as the first step, and build on it.
I've included my code below, unfortunately, the string doesn't print to the console, it looks correct to me. Could someone please have a quick look over this and see where I might be going wrong?
I'm pulling my hair out at this stage!!
SERVER
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class Additional_Server { public static void main(String[] args) throws Exception { Socket s; ServerSocket ss = new ServerSocket(2012); System.out.println("Server started. Listening to the port 2012"); while (true) { try { System.out.println("Server: waiting for connection .."); s = ss.accept(); InputStream in = s.getInputStream(); Scanner r = new Scanner(in); String inputLine; inputLine = r.nextLine(); System.out.println("Hello " + inputLine + " from Gary"); } 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 Client 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.3", 2012); // connect to server OutputStream o = s.getOutputStream(); 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'd really appreciate any pointers or help, as I've said, I'm very new to android programming so all help appreciated!!
Regards,
Gary