±Û¾´ÀÌ :ºÓÀºÀå¹Ì 1998³â 2¿ù 17ÀÏ 03:01:49
¼Ò½º¸¦ Àû¾î µå¸³´Ï´Ù.
----------------------
import java.net.*;
import java.io.*;
import java.lang.*;
class Server {
static int users = 0;
static socket_stuff clients[ ] = new socket_stuff[29];
public static void main(String args[ ]) {
ServerSocket serverSocket = null;
int i;
try {
serverSocket = new ServerSocket(4444);
}
catch (IOException e) {
System.out.println("Could not listen on port : "+4444+","+e);
System.exit(1);
}
Socket clientSocket = null;
connection user = new connection(serverSocket);
if (users == 0) {
try {
clientSocket = serverSocket.accept();
}
catch (IOException e) {
System.out.println("Accept failed : " +4444 + "," +e);
System.exit(1);
}
add_client(new socket_stuff(clientSocket));
}
String inputLine,outputLine;
user.start();
try {
inputLine = "Greeting user.";
i = users;
int test;
boolean flag = true;
while (users >= 0) {
if (users > 0 && i <=users) {
test = clients[i].can_read();
if (test == -1) {
remove_client(i);
}
else if (test == 1) {
inputLine = clients[i].read_string();
flag = process_line(inputLine,i);
if (flag) {
if (clients[i] != null)
write_all(clients[i].name+ ":" +inputLine);
}
}
}
try {
user.join(2);
}
catch(InterruptedException e) {}
if (user.connected() == true) {
add_client(new socket_stuff(user.get_socket()));
user.resume();
}
i++;
if (i>users && users>=1)
i=1;
if (i>users && users<1)
i=0;
}
serverSocket.close();
}
catch(IOException e) {}
}
}
static void send_names() {
String names = new String("##names##");
int i;
if (users < 1)
return;
for (i=1;i<=users;i++)
if (clients[i] != null)
names = names + clients[i].name + "|";
write_all(names);
}
static boolean process_line (String line,int i) {
if (line.startsWith ("##name##")) {
clients[i].name = line.substring(8);
clients[i].write_string("Name changed to" + line.substring(8));
send_names();
return false;
}
else if (line.equals("##quit##")) {
write_all(clients[i].name+ "is logging out.");
remove_client(i);
return false;
}
return true;
}
static void write_all (String text) {
int i;
for (i=1;1<=users;i++)
clients[i].write_string(text);
}
static void add_client(socket_stuff client) {
if (users >= 25) {
client.write_strin("Sorry, there are too users right now.");
client.kill();
}
clients[++users] = client;
clients[users].write_string("You have been connected.");
send_names();
}
static remove_client(int clinet) {
clients[client].kill();
clients[client]=clients[users];
clients[users]=null;
users--;
send_names();
}
}
class connection extends Thread {
Socket socket = null;
public boolean is_connected = false;
ServerSocket server = null;
connection (ServerSocket server_sock) {
server = server_sock;
}
boolean connected() {
return is_connected;
}
Socket get_socket() {
System.out.println("Returning the socket");
is_connected = false;
return socket;
}
public void run() {
while (true) {
is_connected = false;
System.out.println("In run Method.");
if (!is_connected) {
try {
System.out.println("Trying for new connection");
socket = server.accept();
}
catch(IOException e) {
System.out.println("Problem with accept");
is_connected = true;
System.out.println("Received new connection");
this.suspend();
}
}
}
class socket_stuff {
Socket socket;
DataInputStream is;
PrintStream os;
String name = new String("Guest");
socket_stuff(Socket client) {
System.out.println("getting the socket info");
try {
is=new DataInputStream(new BufferedInputStream(client.getInputStream()));
os=new PrintStream(new BufferedOutputStream(client.getOutputStream(),1024),false);
socket = client;
}
catch(IOException e) {}
}
}
public void write_string (String s) {
System.out.println ("Writing to socket");
os.println(s);
os.flush();
}
public String read_string () {
System.out.println("Reading from socket");
try {
String inputLine = is.readLine();
return inputLine;
}
catch(IOException e) {}
return "null";
}
public int can_read() {
boolean test;
if (os.checkError()) {
System.out.println("os.checkError");
return -1;
}
try {
test = (is.available()>1);
}
catch(IOException e) {return -1;}
if (test)
return 1;
else
return 0;
}
pubilc void kill() {
try {
if (os != null)
os.close();
if (is !=null)
is.close();
if (socket != null)
socket.close();
}
catch (IOException e) { }
}
}
-------------------------
ÀÌ»óÀÔ´Ï´Ù.