Friday, June 21, 2013

Re: [android-developers] Socket Programing

Please friend check my code..

On 21 June 2013 18:16, Ibrahim Sada <ibrahim.in2em@gmail.com> wrote:
package com.in2em.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {

        try {
            // First we create a server socket and bind it to port 9999.
            ServerSocket myServerSocket = new ServerSocket(9999);


            // wait for an incoming connection...
            System.out.println("Server is waiting for an incoming connection on host="
                    + InetAddress.getLocalHost().getCanonicalHostName()
                    + " port=" + myServerSocket.getLocalPort());
            Socket skt = myServerSocket.accept();

            // ok, got a connection.  Let's use java.io.* niceties to read and write from the connection.
            BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            PrintStream myOutput = new PrintStream(skt.getOutputStream()); 

            // attempt to read input from the stream.
            String buf = myInput.readLine();

            // if we got input, print it out and write a message back to the remote client..
            if (buf != null) {
                System.out.println("Server read: [" + buf + "]");
                myOutput.print("got it");
            }

            // close the connection.
            skt.close();
            System.out.println("Server is exiting");

        } catch (IOException ex) {
            ex.printStackTrace();
            System.out.println("Whoops, something bad happened!  I'm outta here.");

        }



    }

}


On 21 June 2013 18:16, Ibrahim Sada <ibrahim.in2em@gmail.com> wrote:
package com.in2em.clientactivity;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ClientActivity extends Activity {
    /** Called when the activity is first created. */
     private EditText serverIp;

        private Button connectPhones;

        private String serverIpAddress = "192.168.1.27";

        private boolean connected = false;

        private Handler handler = new Handler();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.client);

           // serverIp = (EditText) findViewById(R.id.server_ip);
            connectPhones = (Button) findViewById(R.id.connect_phones);
            connectPhones.setOnClickListener((android.view.View.OnClickListener) connectListener);
        }

        private OnClickListener connectListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!connected) {
                  //  serverIpAddress = serverIp.getText().toString();
                  //  if (!serverIpAddress.equals("")) {
                        Thread cThread = new Thread(new ClientThread());
                        cThread.start();
                   // }
                }
            }
        };

        public class ClientThread implements Runnable {

            public void run() {
                try {
                    InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                    Log.d("ClientActivity", "C: Connecting...");
                    Socket socket = new Socket(serverAddr, 9999);
                  
                    while (connected) {
                       
                        try {
                            Log.d("ClientActivity", "C: Sending command.");
                            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                                        .getOutputStream())), true);
                                // where you issue the commands
                                out.println("Hey Server!");
                                Log.d("ClientActivity", "C: Sent.");
                        } catch (Exception e) {
                            Log.e("ClientActivity", "S: Error", e);
                        }
                    }
                    socket.close();
                    Log.d("ClientActivity", "C: Closed.");
                } catch (Exception e) {
                    Log.e("ClientActivity", "C: Error", e);
                    connected = false;

                }
            }
        }
}

On 21 June 2013 18:15, Ibrahim Sada <ibrahim.in2em@gmail.com> wrote:
if u want i can send u my code...


On 21 June 2013 17:58, Kristopher Micinski <krismicinski@gmail.com> wrote:
Please talk in more understandable English :-),

What specifically are you having trouble with? There's no special
function you have to call on Android that makes things "magically
work."

Kris

On Fri, Jun 21, 2013 at 8:18 AM, Ibrahim Sada <ibrahim.in2em@gmail.com> wrote:
> I used bro..i can do java server client properly..bt i cant do server as
> java and android as client....
> i cant do yaar
>
>
> On 21 June 2013 17:36, Kristopher Micinski <krismicinski@gmail.com> wrote:
>>
>> You should just be able to use any Java sockets tutorial, that isn't
>> the hard part,
>>
>> Kris
>>
>> On Fri, Jun 21, 2013 at 6:53 AM, Ibrahim Sada <ibrahim.in2em@gmail.com>
>> wrote:
>> > Hi Friends I Want To Connect Server As Pc And Client As Android
>> > device(emulator)
>> > After connecting i want to sends and recive mesgs from eachothers..
>> > I Am not able to do so..
>> > Please Help me put friends please
>> > Thanks...In Advance
>> >
>> > --
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "Android Developers" group.
>> > To post to this group, send email to android-developers@googlegroups.com
>> > To unsubscribe from this group, send email to
>> > android-developers+unsubscribe@googlegroups.com
>> > For more options, visit this group at
>> > http://groups.google.com/group/android-developers?hl=en
>> > ---
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Android Developers" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to android-developers+unsubscribe@googlegroups.com.
>> > For more options, visit https://groups.google.com/groups/opt_out.
>> >
>> >
>>
>> --
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Android Developers" group.
>> To post to this group, send email to android-developers@googlegroups.com
>> To unsubscribe from this group, send email to
>> android-developers+unsubscribe@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/android-developers?hl=en
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Android Developers" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to android-developers+unsubscribe@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Android Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to android-developers+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.






--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment