views:

1249

answers:

6

Hi, I am trying to connect to a standalone desktop tcp server(java) from tcp client application in android device.

But I am not able to connect to that.

I tried to write a desktop tcp client and tried to connect to server(remote server). It is working fine.

But When I am trying to connecting from android, I am getting a IOException while creating Socket instance.

Can anyone help me in this ? Thanks in advance....

A: 

I am answering my own question :)

Now, I am able to connect to server running in my PC from client running in Android device.

The problem is,

My server running in PC is connected to different IP N/W than the Wi-Fi N/W used by my Mobile. So even if I give the same IP address in client to connect to server, it can not connect, because IP address is relative(local) to the IP N/W.

So, if we are using relative IP for Server, we need to make sure that the both Server and client running devices should be connected over the same IP N/W.

It is a silly question and silly mistake and silly answer. :)

Andhravaala
Hi All, I need to know one thing. I need to keep on check the Socket health(is it alive or not). For this I am keep on writing 1 byte of data to the server's outstream.With this I am able to monitor the connection. But I kept this in infinite loop as I need to keep on check that forever. It is consuming more CPU cycles. Instead of that is there any way to check the connection status?Thanks in advance..
Andhravaala
A: 

Ok.... for time being we are doing this like checking the connection for every n seconds.

Timer.scheduleAtFixedRate( new TimerTask() { public void run() { ..... } }

But, still I want to know is there any way like getting callback upon socket connection lost ..

If anyone found this please help...

Thanks in advance..

Andhravaala
You should make the callback yourself. socket.getInputStream().read() will return -1 when the socket is closed, so when your 'read thread' encounters this you can call your own callback. But yeah, I wish such a callback was build in too sometimes :).
MrSnowflake
Hi, thanks for the comment, The problem is In Idle mode if I want to check socket status I cant do socket.getInputStream.read() to read from Server, because it may not send anything to in Idle mode. So,I am using socket.getOutputStream().write() to write and at the same time I can check the socket status. But still with this method, I am making unneccessary traffic on N/W and my app consume more CPU.I have seen one API socket.isConnected(), but unfortunatly it is giving proper result when I closed the socket connection at server side.Please let me know if U come across any solution. Thanks
Andhravaala
A: 

Hi all,

I have a similar problem: I am trying to connect from an Android emulator to a SIP servlet running on another computer (windows 7). Like this:

Address to = addressFactory.createAddress("sip:[email protected]:5060");
Address from = addressFactory.createAddress(addressFactory.createSipURI("310260000000000", "10.0.2.15:8070"));

ArrayList viaHeaders = new ArrayList();
viaHeaders.add(headerFactory.createViaHeader("10.0.2.15", 5070, "udp", null));

ToHeader toHeader = headerFactory.createToHeader(to, null);
FromHeader fromHeader = headerFactory.createFromHeader(from, "310260000000000");
fromHeader.setTag("12345");

Request request = messageFactory.createRequest(addressFactory.createURI("sip:[email protected]:5060"), Request.REGISTER, udpProvider.getNewCallId(),headerFactory.createCSeqHeader(1L, Request.REGISTER),fromHeader, toHeader, viaHeaders, headerFactory.createMaxForwardsHeader(70));

request.addHeader(getContactHeader());

clientTransaction = udpProvider.getNewClientTransaction(request);
clientTransaction.sendRequest();

The server listens at address 192.168.2.145:8060. I can see - with a packet sniffer app - the UDP packets being sent away, but nothing arrives to the server pc. Otherwise I can ping the server machine from my pc.

Can anybody help?

Biro Gabor
Hi, If U are using Android 1.5 or 1.6, U may need to do port forwarding, but for 2.1(currently I am using 2.1) we no need to do port forwarding or redirect. If U are already using 2.1 then, need to check again for client connection creation.
Andhravaala
A: 

Hi yar,

can you please email me your project code and also post to this site.my email id is [email protected]

I am desperately in need of a client code that can connect to a remote server(another system) also send msg to server.The server is in another system (already exists).i am given its ip address and port number.I also am supposed to get response.Once connection established then automatically i will get "connected" msg from server.I tried so many thing but i am not getting response.Can you please help

Thanks and Regards, pushpa

A: 

Hi Pushpa, I cant completely share my project code here, but check this out, it may help out.

Client Creation:

socket = new Socket(TCP_SERVER_IP, TCP_SERVER_PORT); //will create socket connection for U.

reading data from server:

if((socket.getInputStream().available()) > 0) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String s = bufferedReader.readLine(); }

Please take care about syntax. and U need to keep this if block in infinite loop to keep on check for the messages from server.

Andhravaala
A: 

Hi yar,

I tried with the above but i am still not getting the response from the server.I have attached my project and logcat file.Please can you go through that and give me some idea.I really need it because my manager will ask.Please help me.or else please send your project (to connect to remote server) to [email protected] I will send my project to your mail id.Please send me your mail id because i am not getting attach option over here

Thanks and Regards, Pushpa