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?