Solved

simple server / client help pls

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to see all occupied ports on windows 10 laptop 15 82
servlet web applications   metadata-complete="true" or false 3 188
jboss wildfly 10.1 10 404
ejb entity bean example 2 31
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…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.

685 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