This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Limit number of connections  RSS feed

 
Sree Kumari
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • 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)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!