Solved

simple server / client help pls

Posted on 2004-08-15
6
174 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
Comment Utility
Yes, you should use a separate thread to do that in, or use non-blocking IO
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
>> 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:mayankeagle
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now