Solved

Single Java Socket

Posted on 1998-07-07
2
178 Views
Last Modified: 2010-04-16
I have a Client/Server Java application written and working via a Socket.
Now what I want is for only ONE connection to be allowed to this socket.
How can I code it so that only one user can access the Server Socket at a time?
I have tried:
kkSocket = new Socket("rock", 5555,1);
But it is not doing anything when two people connect at the same time.
Thanx!

Don
0
Comment
Question by:afsac
2 Comments
 
LVL 5

Accepted Solution

by:
msmolyak earned 100 total points
Comment Utility
My guess, you control that behavior on the ServerSocket side. When you create a server socket you have to call serverSocket.accept() before client sockets can connect to it. Usually this is done in a loop where you accept a connection, spawn a separate thread to process it and continue listening. In your case you call accept() (which blocks the thread) and then connection comes in you stop listening (by not calling accept() again, or call accept() only after the existing connection is closed).

If that does not work, you can provide some application level logic where you count number of incoming connections and after establishing one connection you reject all the subsequent ones until that one disconnects.
0
 

Author Comment

by:afsac
Comment Utility
I've been looking at the server side. As of now I don't have it doing what
I want.  But I see what you are talking about.  I'll keep working it, I think
that I can make it happen.  Thanx!

Don
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 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

12 Experts available now in Live!

Get 1:1 Help Now