0

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;
    }
5
Contributors
15
Replies
16
Views
7 Years
Discussion Span
Last Post by maninaction
Featured Replies
  • 1

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

  • Maybe something like... [CODE]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; } }[/CODE] ... but you will still have to deal with the input/output … Read More

  • 1

    @ajst doing little of your own "homework" would have already got you there [code=Java] SocketAddress addr = new InetSocketAddress("192.168.0.23", 10123); Socket socket10123 = new Socket(addr);[/code] just need to be written as [code=Java] Socket socket10123 = new Socket(InetAddress.getByName("192.168.0.23"), 10123); [/code] Read More

1

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

0

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.

Edited by ajst: n/a

1

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.

0

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.

0

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

1

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

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.

0

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

Edited by peter_budo: typo

0

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.

0

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.

0

Wow just read that link.

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

Thanks again.

-2

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

Votes + Comments
Your comments are not appreciated since they are off topic and you seem to be spamer, just failed to include your links
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.