Click here to Skip to main content
15,881,693 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I'm hoping someone can help me, I've created a java server and an android client (A), the client(A) gets it's GPS coordinates and sends them to the server, I need the server to send them to client(B). I'm not entirely sure as to what to do, whether I should have separate ports etc or what.

Below is my two clients and server


SERVER

Java
import java.io.BufferedReader;
import java.io.DataInputStream;
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 TCPServer {

	public static void main(String[] args) throws Exception {

		Socket socketChild;
		Socket socketParent;
		
		ServerSocket ServerSocketChild = new ServerSocket(5555);
		ServerSocket ServerSocketParent = new ServerSocket(6666);
		

		System.out.println("Server started. Listening to the port 2001");
		System.out.println("Server: waiting for connection ..");

		while (true) {
			try {
				socketChild = ServerSocketChild.accept();
				socketParent = ServerSocketParent.accept();

				if (socketChild != null) {
					
					InputStream fromChild = socketChild.getInputStream();

					while (socketChild.isConnected() && socketParent.isConnected()) {
						
						System.out.println("Child Connected");
						System.out.println("Parent Connected");

						Scanner r = new Scanner(fromChild);
						String location;
						location = r.nextLine();
						System.out.println(location);

						OutputStream out = socketParent.getOutputStream();
						PrintWriter toParent = new PrintWriter(out);
						toParent.print(location);

						toParent.close();
					}
				}
				
				
			} catch (IOException ex) {
				System.out.println("Problem in message reading");
			}
		}
	}
}



CLIENT A

Java
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;


public class Child extends Activity implements LocationListener {

	private Socket s;
	private PrintWriter p;
	public static double latitude;
	public static double longitude;
	String coordinates;
	

	@Override
	public void onCreate(Bundle savedInstanceState)

	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.child);

		LocationManager mlocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		LocationListener mlocationListener = new MyLocationListener();

		mlocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
				mlocationListener);			
		
		String hello = "hello";  //FOR TESTING PURPOSES
		
		Transmit (hello);
		
		Log.d("test", "test");
	}



   public class MyLocationListener implements LocationListener {

        public void onProviderDisabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Disabled",
                    Toast.LENGTH_SHORT).show();
        }

        public void onProviderEnabled(String provider) {
           Toast.makeText(getApplicationContext(), "Gps Enabled",
                   Toast.LENGTH_SHORT).show();
       }
        public void onStatusChanged(String provider, int status, Bundle extras) {
 
        }

		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
		     latitude = location.getLatitude();
	         longitude = location.getLongitude();
	         
	         coordinates = ("TESTING " + latitude + longitude);
	         	         	         
	         //Transmit(coordinates);         
		}	
    }

	private void Transmit(final String message) {

		Thread trans = new Thread(new Runnable() {
			
			public void run() {

				Log.d("TRANSMIT", "CALLED");

				// TODO Auto-generated method stub
				try {
					
					s = new Socket("192.168.3.101", 5555); // connect to
															// server
					Log.d("CONNECTED", "Connected");

					DataOutputStream _OutPut = new DataOutputStream(
							s.getOutputStream());
					_OutPut.writeBytes(message + "\n");
					_OutPut.flush();
					_OutPut.close();

				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		trans.start();
	}

	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}


}


CLIENT B

Java
import java.io.DataOutputStream;
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.content.Context;
//import android.location.LocationListener;
//import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

public class Parent extends Activity {

	private Socket s;
	private PrintWriter p;
	String location;

	public void onCreate(Bundle savedInstanceState)

	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.parent);
		
		Thread rec = new Thread(new Runnable() {

			public void run() {

				//Log.d("TRANSMIT", "CALLED");

				// TODO Auto-generated method stub
				try {

					s = new Socket("192.168.3.101", 6666); // connect to
															// server
					InputStream fromServer = s.getInputStream();
					//Log.d("CONNECTED", "Connected");

					while (s.isConnected()) {

						Scanner r = new Scanner(fromServer);
						location = r.nextLine();
						
						//OutputStream out = s.getOutputStream();
						//PrintWriter p = new PrintWriter(out);
			
					}

				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		rec.start();
		
		TextView myTextview = (TextView) findViewById(R.id.dist);
		myTextview.setText(location);
	}	
}


With this, I run the server, then run CLIENT B in the emulator and CLIENT A on the device, once I run CLIENT A, CLIENT B force shuts.

CLIENT A / SERVER connectivity is 100%, it prints to console (for testing). What I need to try to find out is how do I send the data to CLIENT B, I have used different ports and i'm sure that's why CLIENT B force shuts. I'm only learning Android a few months and I'm teaching myself, so any help would be greatly appreciated.

Regards,
Gary
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900