Java socket code(not Android) works well..
But it's my first time writing socket code in Android SDK.
So I tried to write sample code in web.
The code is...

--------    AndroidUDP.java  -----------

package com.android.UDPTest;

import android.app.Activity;
import android.os.Bundle;

public class AndroidUDP extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        new Thread(new Server()).start();
        try {
               Thread.sleep(500);
          } catch (InterruptedException e) { }

        new Thread(new Client()).start();
    }
}


--   Server.java  -----

package com.android.UDPTest;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import android.util.Log;

public class Server implements Runnable {

     public static final String SERVERIP = "127.0.0.1"; // 'Within'
the emulator!
     public static final int SERVERPORT = 9001;

     @Override
     public void run() {
          try {

               InetAddress serverAddr = InetAddress.getByName
(SERVERIP);

               Log.d("UDP", "S: Connecting...");
               /* UDP-Socket */
               DatagramSocket socket = new DatagramSocket(SERVERPORT,
serverAddr);


               byte[] buf = new byte[17];

               DatagramPacket packet = new DatagramPacket(buf,
buf.length);
               Log.d("UDP", "S: Receiving...");

               socket.receive(packet);
               Log.d("UDP", "S: Received: '" + new String
(packet.getData()) + "'");
               Log.d("UDP", "S: Done.");
          } catch (Exception e) {
               Log.e("UDP", "S: Error", e);
          }
     }
}

------- Client.java ------------

package com.android.UDPTest;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import android.util.Log;

public class Client implements Runnable {
     @Override
     public void run() {
          try {
               // Retrieve the ServerName
               InetAddress serverAddr = InetAddress.getByName
(Server.SERVERIP);

               Log.d("UDP", "C: Connecting...");
               /*  UDP-Socket */
               DatagramSocket socket = new DatagramSocket();

               /* Prepare some data to be sent. */
               byte[] buf = ("Hello from Client").getBytes();

               DatagramPacket packet = new DatagramPacket(buf,
buf.length, serverAddr, Server.SERVERPORT);
               Log.d("UDP", "C: Sending: '" + new String(buf) + "'");

               socket.send(packet);
               Log.d("UDP", "C: Sent.");
               Log.d("UDP", "C: Done.");
          } catch (Exception e) {
               Log.e("UDP", "C: Error", e);
          }
     }
}

and Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      package="com.android.UDPTest"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
        <activity android:name=".AndroidUDP"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

What is the problem?
It is just CODE I paste from web. But not work..
Thx..again.

On Mar 20, 8:22 pm, Sean Hodges <[email protected]> wrote:
> Can you post up the socket code that you are trying to use?
>
> If this is your first time writing socket code in Java, you might
> benefit from writing a small test program on your PC (not based on
> Android SDK) that confirms what you are trying to do... An example
> tutorial that might 
> help:http://java.sun.com/docs/books/tutorial/networking/sockets/readingWri...
>
>
>
> On Fri, Mar 20, 2009 at 11:14 AM, PluckyMan <[email protected]> wrote:
>
> > I'm sorry, but I set permission in my manifest.xml..
> > But it didn't works.
> > Someone said to me , permission's position is very important.
> > I set permission behind of </application>. Is this true?
> > And If this is true,do you have another solution for this problem?
> > Thanks very much for your help.
>
> > On Mar 20, 2:08 am, Sebastian Kaspari <[email protected]>
> > wrote:
> >> did you set proper permissions in your manifest.xml?
>
> >> <uses-permission
> >>         android:name="android.permission.INTERNET" />
>
> >> android will throw an unknown socket error if the permission is
> >> missing... that's not very helpful :)- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to