2 Replies - 3695 Views - Last Post: 10 February 2013 - 02:29 PM

#1 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 130
  • Joined: 10-February 13

Sending Data from Android Client to PC Server

Posted 10 February 2013 - 11:03 AM

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

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

Is This A Good Question/Topic? 0
  • +

Replies To: Sending Data from Android Client to PC Server

#2 GaryDoo  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 130
  • Joined: 10-February 13

Re: Sending Data from Android Client to PC Server

Posted 10 February 2013 - 01:00 PM

just as a quick follow on, I have created a java server and java client with the same code above which runs correctly on the same machine. I really need specifics of where I'm going wrong as I'm at this for weeks now and I'm exhausted by it! >_<
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: Sending Data from Android Client to PC Server

Posted 10 February 2013 - 02:29 PM

Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1