⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 laotian1111.txt

📁 Java 聊天中国科-大中国科大BBS站的聊天程序
💻 TXT
字号:
发 表 人:Junhua_Wang(yoyo)                       版面名称:Java视图C[197]
文章提要:Java 聊天站源代码 (三)                                           
源 发 站:中国科大BBS站(Sat, 09 Nov 1996 18:55:43)

/* Source begin */ 
import java.io.*; 
import java.net.*; 
import java.util.Vector; 
 
class ThreadedEchoHandler extends Thread 
{  Socket incoming; 
   ThreadedEchoServer server; 
   PrintStream out; 
   int counter; 
   int clientwindow=1; 
   int inputwindow=21; 
   ThreadedEchoHandler(Socket i, int c, ThreadedEchoServer s) 
   {  incoming=i;counter=c;server=s; 
   } 
   public void run() 
   {  try 
      {  DataInputStream in=new DataInputStream(incoming.getInputStream()); 
         out=new PrintStream(incoming.getOutputStream()); 
         out.println("Welcome to Venus Cafe ... \r"); 
         out.print("Please enter your name:"); 
         name=new String(""); 
         byte ch=in.readByte(); 
         while (ch!=10&&ch!=13) 
         {  out.write(ch); 
            name=name+(char)ch; 
            ch=in.readByte(); 
         } 
         out.println("\r\n"); 
         System.out.println("No."+counter+": "+name+" connected in."); 
         server.sendString("***** "+name+" has come.\r"); 
         out.println("\u001b[2J\u001b[20;0H---------------Your Input Area-----------------\r"); 
         server.register(this,1); 
         boolean done=false; 
         while (!done) 
         {  String str=new String(""); 
            ch=in.readByte(); 
            while (ch=='\n'||ch=='\r') ch=in.readByte(); 
            while (ch!='\n'&&ch!='\r') 
            {  str=str+(char)ch; 
               if ((str.length()+79)/80+inputwindow>26) 
               {  for (int j=25;j>=21;j--) out.print("\u001b["+j+";0H\u001b[K");    
                  inputwindow=21; 
               }    
               out.write(ch); 
               ch=in.readByte(); 
            } 
            out.println("\r"); 
            inputwindow+=(str.length()+79)/80; 
            if (str.trim().equals("/bye")) { 
               out.println("Welcome to our cafe again.\r"); 
               done=true; 
               server.sendString("***** "+name+" has left.\r"); 
            }    
            else server.sendString(name+": "+str+"\r"); 
         } 
         incoming.close(); 
         System.out.println("No."+counter+": "+name+" left."); 
      } 
      catch (Exception e) 
      {  System.out.println(e); 
      } 
      finally 
      {   server.register(this,-1); 
      } 
   } 
 
   void sendMessage(String str) 
   {  try 
      {  out.print("\u001b[s\u001b["+clientwindow+";0H"); 
         int l=(str.length()+79)/80; 
         if (clientwindow+l>20) 
         {  for (int j=19;j>=0;j--) out.print("\u001b["+j+";0H\u001b[K"); 
            clientwindow=l+1; 
         } 
         else clientwindow+=l;    
         out.print(str); 
         out.print("\u001b[u"); 
      } 
      catch(Exception e) 
      {  System.out.println(e); 
      }    
   } 
   private String name; 
} 
 
public class ThreadedEchoServer 
{  private static Vector handlers; 
   public static void main(String[] args) 
   {  int i=1; 
      try 
      {  System.out.println("Venus Cafe Starting ..."); 
         ThreadedEchoServer server=new ThreadedEchoServer(); 
         handlers=new Vector(); 
         ServerSocket s = new ServerSocket(23); 
         System.out.println("System ready.\nWaiting for connect in..."); 
         for (;;) 
         {  Socket incoming=s.accept(); 
            new ThreadedEchoHandler(incoming,i,server).start(); 
            i++; 
         } 
      } 
      catch(Exception e) 
      {  System.out.println(e); 
      } 
   } 
   void register(ThreadedEchoHandler h, int r) 
   {  if (r>0) 
      {  if (h!=null) handlers.addElement(h); 
      } 
      else 
      {  int j=0; 
         while (j<handlers.size()) 
         {  if ((ThreadedEchoHandler)handlers.elementAt(j)==h) 
            {  handlers.removeElementAt(j); 
               return; 
            } 
            j++; 
         } 
      } 
   } 
   void sendString(String str) 
   {  for (int j=0;j<handlers.size();j++) 
         if (handlers.elementAt(j)!=null)  
            ((ThreadedEchoHandler) handlers.elementAt(j)).sendMessage(str); 
   } 
} 
 
-- 
※ 来源: 中国科大BBS站 [bbs.ustc.edu.cn] 



发 表 人:Junhua_Wang(yoyo)                       版面名称:Java视图C[196]
文章提要:Java 聊天站原代码 (二)                                           
源 发 站:中国科大BBS站(Sat, 09 Nov 1996 18:54:19)

Please use WinZip32 for Win95/NT extract the ChatCafe. 
 
ChatCafe is a simple Chat system written in Java, which can run on a  
Windows95/NT system.  Using "java ThreadedEchoServer" (Please notice the  
case, not "threadedechoserver" or some else) to start the chat service,  
then you can use telnet to connect to the ChatCafe.  Use "/bye" to exit 
the chat on client, and Ctrl_C to quit the server service. The default TCP  
port is 23, the same as telnet port.  However, you can change it to any 
other port in the program.  The client doesn't need Win95/NT, only  
telnet is enough. 
 
This is just a simple test, which may contain many bugs, and cannot be 
applied to real use.  Anyone who is interested in this, please send me  
an email.   
My email add: Junhua_Wang@bbs.ustc.edu.cn 
 
System Requirement for Server: 
. Windows 95 or NT 
. 8MB RAM, 16M RAM is recommended 
. TCP/IP 
. Java Develop Kits, or any other Java tools, such as Symantec's Cafe,  
  Sunsoft's Java WorkShop, or MS Visual J++, etc. 
 
Known Problems: 
. Under Windows NT, because of the double_byte Unicode, the display will 
  be some wrong. 
   
 
-- 
※ 来源: 中国科大BBS站 [bbs.ustc.edu.cn] 



发 表 人:Junhua_Wang(yoyo)                       版面名称:Java视图C[195]
文章提要:Java 聊天站原代码 (一)                                           
源 发 站:中国科大BBS站(Sat, 09 Nov 1996 18:53:25)

ChatCafe 已经上载若干天了, 站长还是不肯收容, 无可奈何, 只好在此上载了. 
请将文件名自己改为 ThreadedEchoServer.java, 在Win95下编译运行即可. 
还望duo提宝贵意见! 
 
-- 
※ 来源: 中国科大BBS站 [bbs.ustc.edu.cn] 




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -