Solved

simple server / client help pls

Posted on 2004-08-15
6
193 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
6 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 50 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

776 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