Program delays in java

B

Brian Pugh

I am writing a 2 player network game for a class of mine and I have
the problem of when my app starts a new game, it creates a server (new
thread) then connects to that server through sockets (makes is simple
cause the other player uses the same connection code). My problem is
though that when my client trys to connect to the server, the server
is not guarantee to be waiting for connections then, leaving my client
to disconnect. I could just have the client loop a couple of times
until it gets a connection but I am wondering if there is a way to
delay the client thread for a bit (much like sleep(int) in C).

Brian Pugh
(e-mail address removed)
 
J

Jason Bell

Brian Pugh said:
but I am wondering if there is a way to
delay the client thread for a bit (much like sleep(int) in C).

Guess what, there is. Ironically it's a method called sleep(long millis) in
the Thread class.
Please read the API docs, that's what they are there for. Otherwise
consider the course you are on...

http://java.sun.com/j2se/1.4.2/docs/api/

Regards
Jase Bell
 
J

Joona I Palaste

Brian Pugh said:
I am writing a 2 player network game for a class of mine and I have
the problem of when my app starts a new game, it creates a server (new
thread) then connects to that server through sockets (makes is simple
cause the other player uses the same connection code). My problem is
though that when my client trys to connect to the server, the server
is not guarantee to be waiting for connections then, leaving my client
to disconnect. I could just have the client loop a couple of times
until it gets a connection but I am wondering if there is a way to
delay the client thread for a bit (much like sleep(int) in C).

Have a look at Thread.sleep(). It causes the currently executing thread
to delay for a specific number of milliseconds.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
- Karri Kalpio
 
R

Roedy Green

My problem is
though that when my client trys to connect to the server, the server
is not guarantee to be waiting for connections then, leaving my client
to disconnect. I could just have the client loop a couple of times
until it gets a connection but I am wondering if there is a way to
delay the client thread for a bit (much like sleep(int) in C).

Check out the code for EchoServer posted at
http://mindprod.com/products.html#ECHO

It shows you an absolutely minimal server and how it creates
connections.

There is no need for a loop or delay as you apparently need in C.

That is handled by code like this that spins off one new accepted
connection each time through the loop. ServerSocket.accept waits
until a new connection arrives.

ServerSocket echo = new ServerSocket( PORT );
while ( true )
{
Socket socket = echo.accept();
( new EchoClientThread( ++streamCount, socket) ).start();
}

However if you want to do delays, there is Thread.sleep.

See http://mindprod.com/jgloss/thread.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top