Click here to Skip to main content
15,896,557 members

Android Client Server Issues

GaryDoo asked:

Open original thread
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...

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

Java
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


Java
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();  
                }  
            }  
        });  
  
    }  
}


Thanking you in advance

Regards,
Gary
Tags: Mobile Apps (Android), Server, Connection

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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