954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Socket connect.

Hi DaniWeb,

I'm currently writing a client server program.

I have my client and server sending data and everything fine.

But I'm trying to get my client to reconnect.

E.g if I get the server to kill the clients connection how can I get the client to realise it is no longer connected and then reconnect.

I've tried using the command socket.connect() but it is asking for a SocketAddress.

I've looked on SUN's website and there is nothing about SocketAddress just that it is a class.

public boolean ConnectToServer() throws Exception
    {
    	if(!Globals.socket10123.isConnected())
    	{
    		
    		Globals.socket10123.connect ("192.168.0.23:10123");
    	}
		
	if(Globals.socket10123.isConnected())
	{			
	     return true;
	}

	return false;
    }
ajst
Junior Poster
121 posts since Nov 2010
Reputation Points: 30
Solved Threads: 3
 

Can you make a small simple program that compiles and executes with client and server parts to demonstrate the problem?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Ummm... yea after looking at my first post i guess i need to add more info.

problem being my server code is in delpi and client is java(android app) and this is just a small problem of a big application.

so this might help clear things up

public static Socket socket10123;

socket10123 = new Socket("192.168.0.23"10123);

while(ConnectToServer)
{
//do something
//see code from original post.
}


so in my global's class I declare my socket and initialise it.
It connects to the server so while connect it works fine.

but once it the server disconnects the client.

it should open the connection again. it should never return false because it will perform the check every time.

eg if the connection is dropped next time on loop it will recreate the connection so will return true.

ajst
Junior Poster
121 posts since Nov 2010
Reputation Points: 30
Solved Threads: 3
 

Sorry, I don't know anything about the Android environment.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Maybe something like...

SocketAddress addr = new InetSocketAddress("192.168.0.23", 10123);
...
Socket socket10123 = new Socket(addr);
...
public boolean ConnectToServer()  {
  try {
    if (!Globals.socket10123.isConnected()) {
       Globals.socket10123.connect (addr);
     }
     return Globals.socket10123.isConnected();
  } catch(Exception e) {
     e.printStackTrace();
     return false;
  }
}

... but you will still have to deal with the input/output Streams that will have closed when the connection was dropped, and any reads that were blocked waiting on an input Stream.

JamesCherrill
Posting Genius
Moderator
6,372 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Thanks JamesCherrill!

I couldn't do

Socket10123 = new Socket(addr)

because it says The constructor Socket(SocketAddress) is undefined.

So what I did was declare the Socket Address the way you showed me and then when I use the connect method I use that, and when I create my socket I use the way I all ready was to initialise it.

ajst
Junior Poster
121 posts since Nov 2010
Reputation Points: 30
Solved Threads: 3
 

Opps, sorry. my mistake. Yes - just create a new Socket(), then use connect(SocketAddress) instead. (or just create a Socket() and let your ConnectToServer method do the rest - better IMHO because of less duplicated code).

JamesCherrill
Posting Genius
Moderator
6,372 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

@ajst doing little of your own "homework" would have already got you there

SocketAddress addr = new InetSocketAddress("192.168.0.23", 10123);
Socket socket10123 = new Socket(addr);


just need to be written as

Socket socket10123 = new Socket(InetAddress.getByName("192.168.0.23"), 10123);
peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

it was when I googled socket address java and came back with

http://download.oracle.com/javase/1.4.2/docs/api/java/net/SocketAddress.html

which explained nothing to me.

And yes if I looked into jamesCherill answer rather then just using it I would of had a better line of code like yours ;) and understand it more.

But thanks peter budo

ajst
Junior Poster
121 posts since Nov 2010
Reputation Points: 30
Solved Threads: 3
 

it was when I googled socket address java and came back with

http://download.oracle.com/javase/1.4.2/docs/api/java/net/SocketAddress.html

which explained nothing to me.

And yes if I looked into jamesCherill answer rather then just using it I would of had a better line of code like yours ;) and understand it more.

But thanks peter budo


Just make one wonder what on earth you doing in oracle documentation when you should be checking Android API

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

Because Android api is based on java.

Funny as soon as I put in socket in the android website I found my way to the answer.

But thanks Peter as well as James.

ajst
Junior Poster
121 posts since Nov 2010
Reputation Points: 30
Solved Threads: 3
 

@ajst not to be rude but "Because Android api is based on java" is wrong answer.
1) Hibernate, Spring, JPA and other technologies are Java based and we are not using Oracle API to find specific information on classes, but rather their own APIs.
2) Android devices may use different technologies example Python , Ruby and you can also check this interesting discussion on stackoverflow - Which programming languages can I use on Android Dalvik?

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

I'll have a read of it.

I do apologise, but its my first time developing for Android but have used java for a couple of years.

And I will make sure I start looking at the android website not Oracle's.

ajst
Junior Poster
121 posts since Nov 2010
Reputation Points: 30
Solved Threads: 3
 

No problem. I just want to make sure that you or someone else that may come latter would understand that even though that Android development at the moment is pre-dominantly based on Java language there are API differences and that you should check on Android API not just Java general API.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

Wow just read that link.

I thought you could only program in Java for android I am impressed.

Thanks again.

ajst
Junior Poster
121 posts since Nov 2010
Reputation Points: 30
Solved Threads: 3
 

is this possible to coonet in that way....

maninaction
Junior Poster in Training
76 posts since Jun 2011
Reputation Points: 1
Solved Threads: 7
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: