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

📄 chatserverthree.java

📁 用不同方法实现聊天室功能的JAVA代码
💻 JAVA
字号:
/*
 * chatserverthree.java
 *
 * Created on 2007年10月27日, 上午10:55
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package chatroom;
import java.net.*; 
import java.io.*; 
import java.util.*; 

public class chatserverthree implements Runnable 

{public static final int PORT=8765; 
protected ServerSocket listen; 
static Vector connections; 
Thread connect; 
public chatserverthree() 
{try{ 
listen=new ServerSocket(PORT); 

} 
catch(IOException e){ 
System.err.println("erro:"+e);System.exit(1); 

} 
connections=new Vector(1000);connect=new Thread(this); 
connect.start(); 

} 
public void run() 
{ 
try 
{ 
while(true) 
{Socket client=listen.accept(); 
firstthread f=new firstthread(this,client); 
f.setPriority(Thread.MIN_PRIORITY);f.start(); 
connections.addElement(f); 

} 
} 

catch (IOException e) 
{System.err.println("Erro:"+e); 
System.exit(1); 
} 
} 
public static void main(String[] args) 
{new chatserverthree();} 
public void broadcast(String msg) 
{int i; firstthread you; 
for(i=0;i<connections.size();i++) 
{you=(firstthread)connections.elementAt(i); 
try 
{you.out.writeUTF(msg); 

} 
catch (IOException e) 
{ 
} 


} 
} 
public void broadcast1(String msg) 
{int i; String s1,s2,s3; 
firstthread you; 
s1=new String("PEOPLE"); 
s2=new String(msg.substring(4)); 
s3=s1.concat(s2); 
for (i=0;i<connections.size() ;i++ ) 
{you=(firstthread) connections.elementAt(i); 
if (s3.startsWith(you.name)) 
{try 
{ 
you.out.writeUTF(msg); 
} 
catch (IOException e) 
{ 
} 
} 
} 
} 
} 
class firstthread extends Thread 
{protected Socket client; 
String line,name; 
int k=0; 
protected DataOutputStream firstout,out; 
protected chatserverthree server; 
protected DataInputStream in; 
public firstthread(chatserverthree server,Socket client) 
{this.server=server;this.client=client; 
try 
{ 
in=new DataInputStream(client.getInputStream()); 
out=new DataOutputStream(client.getOutputStream()); 
firstout=new DataOutputStream(client.getOutputStream()); 
} 
catch (IOException e) 
{try 
{ 
server.connections.removeElement(this); 
client.close(); 
} 
catch (IOException e2) 
{System.err.println("有问题哦:"+e); 
return ; 
} 
if (this.client==null) 
{server.broadcast("QUIT"+this.name);this.name=null; 
} 
} 
} 
public void run() 
{ 
try 
{ 
for (int i=0;i<server.connections.size() ;i++ ) 
{firstthread c=(firstthread)(server.connections.elementAt(i)); 
if (c.name!=null) 
{try 
{ 
firstout.writeUTF(c.name); 
} 
catch (IOException e) 
{ 
} 
} 
} 
} 
catch (ArrayIndexOutOfBoundsException e) 
{ 
} 
catch(NullPointerException e) 
{} 
try 
{ 
while (true) 
{line=in.readUTF(); 
if (line.startsWith("PEOPLE")) 
{try 
{ 
firstthread d=(firstthread)(server.connections.elementAt(server.connections.indexOf(this))); 
if (d.name!=null) 
{d.name=line; 
} 
else if (d.name!=null) 
{server.broadcast("QUIT"+this.name); 
d.name=line; 
} 
} 
catch (ArrayIndexOutOfBoundsException e) 
{ 
} 
catch (NullPointerException e) {} 
finally {server.broadcast(line);} 
} 
else if (line.startsWith("MSG")) 
{server.broadcast(line); 
} 
else if (line.startsWith("QUIT")) 
{server.broadcast("QUIT"+this.name); 
server.connections.removeElement(this); 
this.client.close();this.yield(); 

} 
else if (line.startsWith("BADQUIT")) 
{server.broadcast("MSG"+(this.name).substring(6)+"被踢出去了"); 
server.broadcast("QUIT"+this.name); 
server.connections.removeElement(this); 
this.client.close();this.yield(); 
} 
else if (line.startsWith("newlist")) 
{try 
{ 
for (int i=0;i<server.connections.size() ;i++ ) 
{firstthread c=(firstthread)(server.connections.elementAt(i)); 
if (c.name!=null) 
{try 
{ 
firstout.writeUTF(c.name); 
} 
catch (IOException e) 
{ 
} 
} 
} 
} 
catch (ArrayIndexOutOfBoundsException e) 
{ 
} 
catch(NullPointerException e) { } 
} 
else if (line.startsWith("悄悄的对")) 
{this.out.writeUTF(line+"*"); 
server.broadcast(line); 
} 
else if (line.startsWith("坏悄悄的对")) 
{this.out.writeUTF(line.substring(1)+"*"); 
server.broadcast(line.substring(1)); 
} 
} 
} 
catch (IOException e) 
{server.connections.removeElement(this); 
} 
catch(NullPointerException e) { server.connections.removeElement(this);} 
} 
} 


⌨️ 快捷键说明

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