Problem while creating a Socket

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Problem while creating a Socket

Postby Jorge33212 » Sun Aug 26, 2012 9:02 pm

Hello guys,

I've been trying to set up a small client/server application, but the "SocketTimeOutException" keeps poping up everytime I try to create the socket which is going to connect my Android device with my PC's server (java coded).

I don't really know how to solve this as I'm a complete newbie into Android, but had some basic Java knowledge from last year (hope it helps). The exception pops out when I'm trying to create the socket, before I even attempt to send the data through it.

Here's the code for my Android Activity:
Code: Select all


import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends Activity implements OnClickListener {

   // Inicializo los dos botones
   Button btnSend;
   Button btnCancel;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      btnSend = (Button) findViewById(R.id.boton_send);
      btnCancel = (Button) findViewById(R.id.boton_cancel);
      btnSend.setOnClickListener(this);
      btnCancel.setOnClickListener(this);

   }

   public void onClick(View v) {

      
      if (v == btnSend) {
         // Button send was clicked

         Socket socket = null;
         DataOutputStream dos = null;
         EditText editMsg = (EditText) findViewById(R.id.textoEnviar);
         String mensaje = editMsg.getText().toString();
         //Retrieving ip and port (direccion and puerto)
         String direccion = new String(((EditText) findViewById(R.id.var_ip)).getText().toString());
         int puerto = Integer.parseInt(((EditText) findViewById(R.id.var_puerto)).getText().toString());
         
         try {
            socket = new Socket(direccion, puerto);      //HERE IS WHERE THE EXCEPTION POPS OUT
      
                                //the app doesn't even make it to this line below. It jumps to the IOException catch
            socket.setSoTimeout(10*1000);
            dos = new DataOutputStream(socket.getOutputStream());
            //sending my message through the data output stream
            dos.writeChars(mensaje);

         } catch (UnknownHostException e) {
            e.printStackTrace();
         } catch (IOException e) {
            e.printStackTrace();
         }

         finally {
            if (socket != null) {
               try {
                  socket.close();
               } catch (IOException e) {
                  
                  e.printStackTrace();
               }
            }
            if (dos != null) {
               try {
                  dos.close();
               } catch (IOException e) {
                  
                  e.printStackTrace();
               }
            }
         }

      }
      //if cancel button is pressed
      else if (v == btnCancel) {
         finish();

      }
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
}



At first I was going to post the Java server's code too, but It worked when I tried to connect to it from another pc so I guess the server is not causing the issue.

Thanks in advance and sorry for my spanish messy english
Jorge33212
Freshman
Freshman
 
Posts: 2
Joined: Thu Aug 23, 2012 5:48 pm

Top

Re: Problem while creating a Socket

Postby Jorge33212 » Sun Aug 26, 2012 9:46 pm

Okay it's solved, I had been working so many hours with this and I got obssessed. I just had forgot to manually change the port forwarding on my router so now I've just done that works like a charm.

Sorry for the inconvenience
Jorge33212
Freshman
Freshman
 
Posts: 2
Joined: Thu Aug 23, 2012 5:48 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Google [Bot] and 4 guests