爱心技术专栏专题

一个c/s的聊天室(3)

摘录:爱心 来源:爱心 加入时间:2006年09月14日
摘要:
一个c/s的聊天室(3)


public void jButton1_actionPerformed(ActionEvent e) {
setClection2();


}

p…

转载:转载请保留本信息,本文来自
http://www.51dibs.com
/html/2006/article/info/a_995c3b41c49085c1.htm

一个c/s的聊天室(3)

站点:爱心种子小博士 关键字:一个c/s的聊天室(3)

   
一个c/s的聊天室(3)
一个c/s的聊天室(3)

public void jButton1_actionPerformed(ActionEvent e) {
setClection2();


}

public void jButton2_actionPerformed(ActionEvent e) {
jTextArea1.setText("");
}

public void jTextArea2_keyPressed(KeyEvent e) {
if(e.isControlDown() &&e.getKeyCode()==KeyEvent.VK_ENTER ){
setClection2();
}
}
}

class Frame1_jTextArea2_keyAdapter extends KeyAdapter {
private Frame1 adaptee;
Frame1_jTextArea2_keyAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}

public void keyPressed(KeyEvent e) {
adaptee.jTextArea2_keyPressed(e);
}
}

class Frame1_jButton2_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}

class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {

adaptee.jButton1_actionPerformed(e);
}
}
//主程序
public class Application1 {
boolean packFrame = false;

/**
* Construct and show the application.
*/
public Application1() {
Frame1 frame = new Frame1();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
} else {
frame.validate();
}

// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);

}

/**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}

new Application1();
}
});
}
}
//
服务器
import java.io.*;
import java.net.*;
import java.util.*;

public class Server extends ServerSocket
{
private static final int SERVER_PORT = 10000;
Vector vector1 =new Vector();
Vector vector2 =new Vector();

public Server() throws IOException
{
super(SERVER_PORT);

try
{
while (true)
{
Socket socket = accept();

new CreateServerThread(socket,vector1,vector2);
}
}
catch (IOException e)
{}
finally
{
close();
}
}
public static void main(String[] args) throws IOException
{
new Server();
}
}
//--- CreateServerThread
class CreateServerThread extends Thread
{
Socket client;

DataInputStream in;
DataOutputStream out;

Vector vector1;
Vector vector2;
public boolean bool=false;
String ss=null;
String string=null;
int iii;
String str=null;
Enumeration enu;

public CreateServerThread(Socket s,Vector vec1,Vector vec2) throws IOException
{
client = s;
vector1=vec1;
vector2=vec2;

out=new DataOutputStream(client.getOutputStream());
in=new DataInputStream(client.getInputStream());

start();
}

public void run()
{
StringTokenizer st;
StringTokenizer stc;
try
{


while (true)
{
ss=in.readUTF();
if(ss.startsWith("新的朋友")){
if(vector1.contains(str)){ iii=vector1.indexOf(str);
vector1.remove(iii);vector2.removeElement(this);}
if(vector1.contains(ss) ){
out.writeUTF("不可以");

}
else{
out.writeUTF("可以");
str=ss;
vector1.add(ss);
Enumeration enu=vector1.elements();
while(enu.hasMoreElements()){

out.writeUTF((String)enu.nextElement());
}


bool=true;
Enumeration enuc=vector2.elements();
while(enuc.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enuc.nextElement();
th.out.writeUTF(ss);
}
stc=new StringTokenizer(ss,":");
string=stc.nextToken();
string=stc.nextToken();
Enumeration enuc1=vector2.elements();
while(enuc1.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enuc1.nextElement();
th.out.writeUTF(string+"...上线了");
}
vector2.add(this);
}
}


else if(ss.startsWith("下线了")||client.isClosed()){
st=new StringTokenizer(ss,":");
string=st.nextToken();
string=st.nextToken();
iii=vector1.indexOf(str);
vector1.remove(iii);
Enumeration enu=vector2.elements();
while(enu.hasMoreElements()){

CreateServerThread th=(CreateServerThread)enu.nextElement();
if(th!=this&&th.isAlive())
th.out.writeUTF("下线了:"+str.substring(str.indexOf(":")+1));
th.out.writeUTF(str.substring(str.indexOf(":")+1)+"...下线了");
}
vector2.remove(this);
break;


}
else{

enu=vector2.elements();
while(enu.hasMoreElements()){
CreateServerThread th=(CreateServerThread)enu.nextElement();
th.out.writeUTF(ss);
}

}
}
}
catch (IOException e)
{
try{
iii=vector1.indexOf(str);
vector1.remove(iii);
Enumeration enun=vector2.elements();
while(enun.hasMoreElements()){

CreateServerThread th=(CreateServerThread)enun.nextElement();
if(th!=this&&th.isAlive())
th.out.writeUTF("下线了:"+str.substring(str.indexOf(":")+1));
th.out.writeUTF(str.substring(str.indexOf(":")+1)+"...下线了");
}
vector2.remove(this);
return;
}catch(Exception ee){return;}
}
} (全文完)

客户服务中心信箱:[email protected] [email protected] 网站地图

声明

合作伙伴: