|  |
DownLoad JavaChat.lzh(ソースコード付き)
########################################################################################
######### ServerSide
######### java JavaChatServer
######### java JavaChatServer 58000 10
////////////////////////////////////////////////////////////////////////////////////
// JavaChatServer.java
// Javaチャットサーバー&クライアントアプレット / I-ichirow Suzuki 1996
//
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
//----------------------------------------------------------------------------------
class JavaChatServer
{
static int maxSession;
static ReceiveSession clients[];
static Socket s[];
static int snstack[];
static int snpoint;
static
{
maxSession = 8;
snpoint = maxSession - 1;
}
public JavaChatServer()
{
}
public static void main(String args[])
{
Object obj = null;
int l = 0;
if(args.length > 2)
{
System.out.println("Usage: java JavaChatServer [PortNumber] [MaxConnection]");
return;
}
for(int j = 0; j < args.length; j++)
switch(j)
{
case 0: // '\0'
l = Integer.parseInt(args[j]);
break;
case 1: // '\001'
maxSession = Integer.parseInt(args[j]);
break;
}
s = new Socket[maxSession];
clients = new ReceiveSession[maxSession];
snstack = new int[maxSession];
for(int k = 0; k < maxSession; k++)
snstack[k] = maxSession - k - 1;
try
{
ServerSocket serversocket = new ServerSocket(l);
System.out.println("JavaChatServer Started.");
System.out.println("[PortNumber = " + serversocket.getLocalPort() + "]");
System.out.println("[MaxConnection = " + maxSession + "]");
do
try
{
Socket socket = serversocket.accept();
int i;
if((i = nextSession()) < 0)
{
socket.close();
} else
{
System.out.println("accept " + i + ".");
s[i] = socket;
clients[i] = new ReceiveSession(s[i], i, clients);
clients[i].start();
}
}
catch(IOException _ex) { }
while(true);
}
catch(IOException _ex)
{
return;
}
}
static int nextSession()
{
if(snpoint < 0)
{
return -1;
} else
{
int i = snstack[snpoint];
snpoint--;
return i;
}
}
public static synchronized void closeSession(int i)
{
clients[i] = null;
try
{
s[i].close();
s[i] = null;
}
catch(IOException _ex)
{
System.out.println("close failed.");
}
snpoint++;
snstack[snpoint] = i;
System.out.println("session " + i + " closed.");
}
}
//////////////////////////////////////////////////////////////////////
// JavaChatClient.java
//
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.Socket;
import java.net.URL;
//--------------------------------------------------------------------
public class JavaChatClient extends Applet
{
Socket s;
InputStream sIn;
OutputStream sOut;
DataInputStream dIn;
DataOutputStream dOut;
InputName inputName;
InThread inThread;
InputMessage inputMes;
Label yourNameLabel;
String userName;
boolean stopFlag;
String hostName;
int portNum;
Image logoImg;
Image onIconImg;
Image offIconImg;
Image iconImg;
//----------------------------------------------------------
public JavaChatClient()
{
portNum = -1;
}
//----------------------------------------------------------
public void update(Graphics g)
{
paint(g);
}
//----------------------------------------------------------
public void paint(Graphics g)
{
g.drawImage(logoImg, 10, 10, this);
g.drawImage(iconImg, 640, 3, this);
}
//----------------------------------------------------------
public synchronized void login()
{
String s1 = inputName.getText();
if(s1.length() < 1 || s1.length() > 12)
{
return;
} else
{
inputName.setActive(false);
openSession(s1);
return;
}
}
//----------------------------------------------------------
public void init()
{
hostName = getDocumentBase().getHost();
setLayout(null);
setBackground(Color.white);
initSession();
logoImg = getImage(getDocumentBase(),"images/" + "logo.gif");
onIconImg = getImage(getDocumentBase(),"images/" + "icon_on.gif");
offIconImg = getImage(getDocumentBase(),"images/" + "icon_off.gif");
iconImg = offIconImg;
yourNameLabel = new Label("Handle name: ", 2);
inputName = new InputName(this);
inThread = new InThread(this);
inputMes = new InputMessage(this);
yourNameLabel.reshape(414, 22, 80, 20);
inputName.reshape(496, 25, 128, 20);
inThread.reshape(5, 56, 685, 252);
inputMes.reshape(5, 310, 685, 20);
add(yourNameLabel);
add(inputName);
add(inThread);
add(inputMes);
if(getParameter("PORT") == null)
{
System.out.println("[JavaChatClient]: Please set 'PORT' parameter.");
disable();
} else
{
portNum = Integer.parseInt(getParameter("PORT"));
}
repaint();
Toolkit.getDefaultToolkit().sync();
}
//--------------------------------------------------------------
public void stop()
{
stopFlag = true;
if(dOut != null)
closeSession();
resetComponent();
initSession();
}
//--------------------------------------------------------------
void resetComponent()
{
inThread.setEditable(false);
inputMes.setEditable(false);
inputName.setActive(true);
inputName.requestFocus();
inputMes.hide();
inputMes.show();
inputMes.validate();
inputName.hide();
inputName.show();
inputName.validate();
if(!Thread.currentThread().getName().equals("inThread"))
inThread.stop();
}
//--------------------------------------------------------------
public synchronized void failed()
{
iconImg = offIconImg;
repaint();
Toolkit.getDefaultToolkit().sync();
inThread.appendText(" 回線を切断しました ");
initSession();
start();
}
//--------------------------------------------------------------
public synchronized void start()
{
stopFlag = false;
if(inThread != null && inThread.thread != null && inThread.thread.isAlive())
System.out.println("warn:thread still alive.");
resetComponent();
}
//--------------------------------------------------------------
public void openSession(String s1)
{
userName = s1;
try
{
s = new Socket(hostName, portNum);
sIn = s.getInputStream();
dIn = new DataInputStream(sIn);
sOut = s.getOutputStream();
dOut = new DataOutputStream(sOut);
sendData('0', userName);
inThread.start(dIn);
iconImg = onIconImg;
inputMes.setEditable(true);
inputMes.requestFocus();
inputMes.hide();
inputMes.show();
inputMes.validate();
inputName.setEditable(false);
inputName.hide();
inputName.show();
inputName.validate();
repaint();
Toolkit.getDefaultToolkit().sync();
return;
}
catch(Exception _ex)
{
failed();
}
}
//--------------------------------------------------------------
public void initSession()
{
s = null;
sIn = null;
dIn = null;
sOut = null;
dOut = null;
}
//--------------------------------------------------------------
public void sendMessage(String s1)
{
sendData('1', s1);
}
//--------------------------------------------------------------
synchronized void sendData(char c, String s1)
{
try
{
dOut.writeChar(c);
for(int i = 0; i < s1.length(); i++)
dOut.writeChar(s1.charAt(i));
dOut.writeChar(0);
dOut.flush();
return;
}
catch(IOException _ex) { }
if(!stopFlag)
failed();
}
//--------------------------------------------------------------
public synchronized void logout()
{
iconImg = offIconImg;
repaint();
Toolkit.getDefaultToolkit().sync();
inThread.appendText(" [" + userName + "] はログアウトしました ");
closeSession();
initSession();
start();
}
//--------------------------------------------------------------
public synchronized void closeSession()
{
if(!Thread.currentThread().getName().equals("inThread"))
inThread.stop();
sendData('z', "");
try
{
dOut.flush();
s.close();
}
catch(IOException _ex) { }
s = null;
}
}
//////////////////////////////////////////////////////////////////////////
// InputMessage.java
//
import java.awt.*;
//------------------------------------------------------------------------
class InputMessage extends TextField
{
JavaChatClient cm;
//------------------------------------------------------
public InputMessage(JavaChatClient cappleclient)
{
super(64);
cm = cappleclient;
}
//------------------------------------------------------
public synchronized boolean action(Event event, Object obj)
{
if(getText().length() < 1)
{
return false;
} else
{
cm.sendMessage(getText());
setText("");
return true;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// InThread.java
//
import java.awt.TextArea;
import java.awt.TextComponent;
import java.io.DataInputStream;
import java.io.IOException;
//------------------------------------------------------------------------------
class InThread extends TextArea implements Runnable
{
Thread thread;
DataInputStream dIn;
JavaChatClient cm;
boolean winFlag;
//------------------------------------------------------------
public InThread(JavaChatClient cappleclient)
{
winFlag = false;
setText("");
cm = cappleclient;
if(System.getProperty("os.name").length() > 6 && System.getProperty("os.name").substring(0, 7).equals("Windows"))
winFlag = true;
}
//------------------------------------------------------------
public void start(DataInputStream datainputstream)
{
dIn = datainputstream;
thread = new Thread(this, "inThread");
thread.start();
}
//------------------------------------------------------------
public void stop()
{
if(thread == null)
return;
if(thread != null && thread.isAlive())
thread.stop();
thread = null;
}
//------------------------------------------------------------
public void run()
{
char ac[] = new char[1056];
try
{
do
{
int i;
char c;
for(i = 0; (c = dIn.readChar()) != 0 && c != 0; i++)
ac[i] = c;
String s = new String(ac, 0, i);
appendText(s);
Thread.yield();
} while(true);
}
catch(IOException _ex)
{
cm.failed();
}
}
//------------------------------------------------------------
public synchronized void appendText(String s)
{
if(winFlag)
{
s = getText() + s + "\n";
setText("");
super.appendText(s);
return;
} else
{
super.appendText(s + "\n");
return;
}
}
}
///////////////////////////////////////////////////////////////////////////
// JeceiveSession.java
//
//
import java.io.*;
import java.net.Socket;
//-------------------------------------------------------------------------
class ReceiveSession implements Runnable
{
public int sessionNum;
public String userName;
OutputStream sOut;
InputStream sIn;
DataInputStream dIn;
DataOutputStream dOut;
Thread inThread;
Object clients[];
//-------------------------------------------------------------
public ReceiveSession(Socket socket, int i, Object aobj[])
{
userName = "nobody";
try
{
sIn = socket.getInputStream();
dIn = new DataInputStream(sIn);
sOut = socket.getOutputStream();
dOut = new DataOutputStream(sOut);
}
catch(IOException _ex) { }
clients = aobj;
sessionNum = i;
}
//-------------------------------------------------------------
public void start()
{
inThread = new Thread(this);
inThread.start();
}
//-------------------------------------------------------------
public void stop()
{
inThread.stop();
inThread = null;
}
//-------------------------------------------------------------
public void run()
{
try
{
while(true)
{
char ac[] = new char[1024];
char c1 = dIn.readChar();
int i;
char c;
for(i = 0; (c = dIn.readChar()) != 0; i++)
ac[i] = c;
String s2 = new String(ac, 0, i);
ac = null;
switch(c1)
{
case 48: // '0'
userName = s2;
String s = " [" + userName + "] がログインしました ";
sendMessage(s);
break;
case 49: // '1'
String s1 = " [" + userName + "] : " + s2;
sendMessage(s1);
break;
case 122: // 'z'
logout();
return;
}
Thread.yield();
}
}
catch(IOException _ex)
{
logout();
}
}
//-------------------------------------------------------------
void logout()
{
sendMessage(" [" + userName + "] がログアウトしました", false);
JavaChatServer.closeSession(sessionNum);
}
//-------------------------------------------------------------
void sendMessage(String s)
{
sendMessage(s, true);
}
//-------------------------------------------------------------
void sendMessage(String s, boolean flag)
{
for(int i = 0; i < JavaChatServer.maxSession; i++)
if((i != sessionNum || flag) && clients[i] != null)
((ReceiveSession)clients[i]).output(s);
}
//-------------------------------------------------------------
public synchronized void output(String s)
{
try
{
for(int i = 0; i < s.length(); i++)
dOut.writeChar(s.charAt(i));
dOut.writeChar(0);
dOut.flush();
return;
}
catch(IOException _ex)
{
System.out.println("output error!");
}
}
}
////////////////////////////////////////////////////////////////////
// InputName.java
//
import java.awt.*;
//------------------------------------------------------------------
class InputName extends TextField
{
JavaChatClient cc;
boolean active;
//-----------------------------------------------------------
public InputName(JavaChatClient cappleclient)
{
super(16);
active = false;
cc = cappleclient;
}
//-----------------------------------------------------------
public synchronized boolean action(Event event, Object obj)
{
if(!active)
{
return false;
} else
{
active = false;
cc.login();
return true;
}
}
//-----------------------------------------------------------
public void setActive(boolean flag)
{
active = flag;
setEditable(active);
}
}
インターネットスタートページ 鈴木維一郎 石橋三重子
|
|
|
|
|
|