[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

simple server / client help pls

Posted on 2004-08-15
6
Medium Priority
?
225 Views
Last Modified: 2010-03-31
Hi. i am making a program but im having trouble using server.accept() since it stops the thread that i am using. is there a way to keep running the thread while it waits for a connection so that my program can connect to itself.
thankx
0
Comment
Question by:teamasia
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11804727
Yes, you should use a separate thread to do that in, or use non-blocking IO
0
 
LVL 92

Expert Comment

by:objects
ID: 11806121
post your server code
(why do u need your server to connect to itself)
0
 
LVL 5

Accepted Solution

by:
vilia earned 200 total points
ID: 11806400
In the example provided below (common solution),  server waits for a client
   Socket client = ss.accept();
As a client makes connection, the new thread is created (serveClientConnection) and started. The server waits for the second client, and so on.

serveClientConnection is responsible for server-client communication.

The thread with
   Socket client = ss.accept();
should do nothing but wait for a new client connection and spawn a new thread.


private static final int SERVER_PORT = 1234;
public static void main(String[] argv) {
      try {
            ServerSocket ss = new ServerSocket(SERVER_PORT);
            while (true) {  // server loop
                  // wait for a client
                  Socket client = ss.accept();
                  // start server- client communication in
                  // the new thread
                  Thread serveClientConnection = new Thread() {
                        public void run() {
                              // communicate with client
                        }
                  };
                  serveClientConnection.start();
            }
      } catch (Exception e) {
            e.getStackTrace();
      }
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11807356
>> server.accept() since it stops the thread that i am using

That's what its supposed to do. It waits for clients to connect.

>> is there a way to keep running the thread while it waits for a connection

What *other* processing do you do in that thread, that you want it to keep running?

>> so that my program can connect to itself

Program connect to itself ;-) ??
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11899800
teamasia,

Can you elaborate a little more on what your question was and how that comment helped? I still don't seem to understand:

>> my program can connect to itself

- and how the accepted answer does that ;-) ?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question