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;
    }

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

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.

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

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.

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.

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).

@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);

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

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 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?

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.

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.

Wow just read that link.

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

Thanks again.

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

commented: Your comments are not appreciated since they are off topic and you seem to be spamer, just failed to include your links -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.