title: udp android server over wifi
body: <p>I have made a program which is a server Android for receiving an UDP Packets from a C program client over wifi, but actually the problem is that I can only connect my phone to my PC client through USB cable in order to run my application in my phone .
  So as I'm newbie in Android world, I would ask you to guide me on how to set the connection via wifi without using the cable ?</p>
  
  <p>Help !</p>
  
  <p>this is my server code :</p>
  
  <pre><code>package com.example.server_android;
  
  import java.io.IOException;
  import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  import java.net.SocketException;
  import java.net.UnknownHostException;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.EditText;
  import android.widget.TextView;
  
  public class Server_Android extends Activity{
  
      private final static int SERVER_PORT = 1234;
      public final static int RECEIVING_TIMEOUT_SERVER = 3000;
      DatagramSocket socket;
      DatagramPacket packetOut;
      DatagramPacket packetIn;
      byte[] DataIn;
      byte[] DataOut;
  
      /*Android widgets*/
      TextView text;
      EditText edit;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_server_android);
          //text = (EditText)findViewById(R.id.editText1);
          text =(TextView)findViewById(R.id.textView2);   
          text.setText("");
  
          /* Thread for receiving Data from CLient */
          new Thread(new Receiver()).start();
              try {   
          Thread.sleep(500);
          }catch(InterruptedException e){
              Log.e("UDP", "UDP receive failed!");
          }
      }
  
      public class Receiver implements Runnable{
  
          public void run(){
  
              try {
  
                  while(socket != null &amp;&amp; !socket.isClosed()){
  
              DataOut = new byte[1024];
  //          InetAddress fromaddress = InetAddress.getByName("10.4.0.11");
              socket = new DatagramSocket(SERVER_PORT);
              socket.setSoTimeout(RECEIVING_TIMEOUT_SERVER);
              packetIn = new DatagramPacket(DataOut,DataOut.length);          
  
              socket.receive(packetIn);
              text.append("source port :" + packetIn.getPort() + "\n");
              text.append("source address :" + packetIn.getAddress().toString() + "\n");
              Log.d("UDP", "Packet receveid");        
              String message = new String(packetIn.getData());
              text.append(message +"\n"+ packetIn.getPort() +"\n"+ packetIn.getAddress().toString());
              Log.d("UDP", "le message reçu");
              Log.d("Message : ", "" + message);          
              }
              }
              catch(UnknownHostException exc) {
                  System.out.println("Destinataire inconnu");
              }
              catch(SocketException exc) {
                  System.out.println("Probleme d'ouverture de la socket");
              }
              catch(IOException exc) {
                  System.out.println("Probleme sur la reception du message");
              }
              }
          }
  
  }
  </code></pre>
  
  <p>and my client code : 
  //----------------------------------------------------------------------------</p>
  
  <pre><code>#include "biblio.h"
  
  #define portnumber 1234
  int
  main(int argc,
       char **argv)
  {
  //_________Seting_Destination_IP_adress____________________
  
  in_addr_t ipAddress= inet_addr("10.4.0.156");
  
  //---- create UDP socket ----
  int udpSocket=socket(PF_INET,SOCK_DGRAM,0);
  if(udpSocket==-1)
    { perror("socket"); exit(1); }
  
  struct sockaddr_in toAddr;
  int lenght = sizeof(toAddr);
  
  
  // ... allowing broadcast (optional)
  int on=1;
      if(setsockopt(udpSocket,SOL_SOCKET,SO_BROADCAST,&amp;on,sizeof(int))==-1)
      { perror("setsockopt"); exit(1); }
  
  int s;
  for(;;)
    {
    //---- read next line on standard input ----   
    char msg[0x100];
    char *buffer = malloc(100);   
    if(!fgets(msg,0x100,stdin)) { break; } 
    //---- send message to the specified destination/port ----
    bzero(&amp;toAddr,lenght);    
    toAddr.sin_family=AF_INET;
    toAddr.sin_port=htons(portnumber);
    toAddr.sin_addr.s_addr=ipAddress;
    s = sendto(udpSocket,msg,strlen(msg),0,(struct sockaddr *)&amp;toAddr,lenght);
      printf("Destination address is %s -- port is : %d ",inet_ntoa(toAddr.sin_addr),ntohs(toAddr.sin_port));
    if( s == -1)  
    { perror("sendto"); exit(1); } 
  }
  
  //---- close UDP socket ----
  close(udpSocket);
  return 0;
  }
  
  //----------------------------------------------------------------------------
  </code></pre>
  
  <hr>
  
  <p>This is the log file :   </p>
  
  <pre><code> 08-20 10:07:56.885: D/libEGL(22622): loaded /system/lib/egl/libEGL_mali.so
      08-20 10:07:56.890: D/libEGL(22622): loaded /system/lib/egl/libGLESv1_CM_mali.so
      08-20 10:07:56.895: D/libEGL(22622): loaded /system/lib/egl/libGLESv2_mali.so
      08-20 10:07:56.900: D/(22622): Device driver API match
      08-20 10:07:56.900: D/(22622): Device driver API version: 10
      08-20 10:07:56.900: D/(22622): User space API version: 10 
      08-20 10:07:56.900: D/(22622): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Mon Mar  5 09:47:55 KST 2012 
      08-20 10:07:56.925: D/OpenGLRenderer(22622): Enabling debug mode 0
      08-20 10:08:05.555: W/dalvikvm(22622): threadid=11: thread exiting with uncaught exception (group=0x40a2d1f8)
      08-20 10:08:05.570: E/AndroidRuntime(22622): FATAL EXCEPTION: Thread-1053
      08-20 10:08:05.570: E/AndroidRuntime(22622): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:709)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.view.View.requestLayout(View.java:12675)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.view.View.requestLayout(View.java:12675)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.view.View.requestLayout(View.java:12675)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.view.View.requestLayout(View.java:12675)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:268)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.view.View.requestLayout(View.java:12675)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.widget.TextView.checkForRelayout(TextView.java:6773)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.widget.TextView.setText(TextView.java:3306)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.widget.TextView.setText(TextView.java:3162)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.widget.TextView.append(TextView.java:2822)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at android.widget.TextView.append(TextView.java:2812)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at com.example.test_udp.MainActivity$Receiver.run(MainActivity.java:70)
      08-20 10:08:05.570: E/AndroidRuntime(22622):    at java.lang.Thread.run(Thread.java:856)
      08-20 10:08:05.790: D/OpenGLRenderer(22622): Flushing caches (mode 0)
      08-20 10:08:06.325: D/OpenGLRenderer(22622): Flushing caches (mode 1)
      08-20 10:13:05.715: I/Process(22622): Sending signal. PID: 22622 SIG: 9
  </code></pre>