• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Limit number of connections

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Server program:
import java.net.*;
import java.io.*;

class Connection extends Thread
{
static int i;
ServerSocket ss;
public Connection(ServerSocket SS)
{
super("Client " + (++i));
ss = SS;
this.start();
}

public void run()
{
try
{
Socket cs = ss.accept();
System.out.println("Connection from " + this.getName() + " Accepted");

InputStream is = cs.getInputStream();
while (true)
{
byte tmp[] = new byte[50];
is.read(tmp);
String t = new String(tmp);
System.out.println(this.getName() + " : " + t);
}
}
catch(IOException ioe)
{}
}
}
class Client
{
public static void main(String args[]) throws IOException
{
InetAddress iadd;
try
{
iadd = InetAddress.getLocalHost();
}
catch(UnknownHostException uhe)
{
System.out.println(uhe);
return;
}
System.out.println("LocalHost : " + iadd);

System.out.println("Attempting a connection to Server....");
int port = 100;
Socket cs = new Socket(iadd,port);
System.out.println("Connection Established");

OutputStream os = cs.getOutputStream();
DataInputStream is = new DataInputStream(System.in);
while (true)
{
byte tmp[] = new byte[50];
is.read(tmp);
os.write(tmp);
}
}
}
This program should allow only 2 users but even if a 3rd user is connencted it shows connection established.I don't want to allow a 3rd connection.what should I do?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We're missing the bit that creates Connection objects. From the look of things, each Connection instance will only accept one client connection and then exit. How have you tested and how can you tell you have three clients connected at once?
 
Sree Kumari
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry,
I have left out this part

class Server1
{
public static void main(String args[]) throws IOException
{
int port=100;
ServerSocket ss = new ServerSocket(port);

System.out.println("Listening for connection...");
Connection con[] = new Connection[2];
con[0] = new Connection(ss);
con[1] = new Connection(ss);
}
}
I have tested this application in a single m/c taking 3 command prompts
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your while(true) loop will take any number of clients one at a time. I don't really see how it could take more than one at a time though. Can you confirm that all three clients were connected and sending data at the same time? Did their messages come out mixed up on the console?

Here's how I built one of these that will take any number of concurrent connections. We could throttle it to "n" at one time by using a thread pool with a fixed number of threads.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic