Android Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsMobile ProgrammingAndroid Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 21st, 2013, 06:55 AM
GaryDoo GaryDoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 GaryDoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 59 sec
Reputation Power: 0
Android server client communication issue

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****

Code:
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****

Code:
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!!!

I have cross posted this in
http://androidforums.com/application-development/679212-android-server-client-communication-issue.html#post5456517

but only because I am desperate, I've been trying to get this going for ages now...so any help is really appreciated

Reply With Quote
  #2  
Old January 21st, 2013, 11:55 AM
GaryDoo GaryDoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 GaryDoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 59 sec
Reputation Power: 0
anyone??

Reply With Quote
  #3  
Old January 23rd, 2013, 12:54 AM
Brus Martin Brus Martin is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 24 Brus Martin Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 3 h 1 m 37 sec
Warnings Level: 10
Number of bans: 2
Reputation Power: 0
You should moved this tread in programming section.

<edit note>De-linked link drop attempt. Poster has been warned about this behavior twice already. As the ump may say, "STRIIIIKE 3 and you're out"</edit>

Last edited by Scorpions4ever : January 26th, 2013 at 02:19 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsMobile ProgrammingAndroid Development > Android server client communication issue

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap