- 论坛徽章:
- 0
|
一年前写的。幸好找到了。
客户端:
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
public class Client extends JFrame implements
ItemListener,ActionListener
{
JButton jb,jb2,jb3;
JTextField jtf1,jtf2;
JTextArea jta;
JLabel jl;
JComboBox jbox;
String strbox[]={"所有人 "};
DataInputStream dis;
DataOutputStream dos;
Socket socket;
JScrollPane jScrollPane1 = new JScrollPane();
clientThread th;
String StrPerson=null;
String myName=null;
Client()
{Container con=getContentPane();
con.setLayout(new FlowLayout());
jb=new JButton("发言"
jb.setEnabled(false);
jb2=new JButton("登陆"
jb3=new JButton("下线"
jtf1=new JTextField(1;
jtf2=new JTextField(12);
jta=new JTextArea(15,25);
jta.setAutoscrolls(true);
jl=new JLabel(" 发送给:"
jbox=new JComboBox(strbox);
//jbox.setSize(120,30);
jta.setWrapStyleWord(true);
jScrollPane1.getViewport().add(jta, null);
jta.setEditable(false);
con.add(jScrollPane1);
con.add(jtf1);
//jtf1.setVisible(false);
//jb.setVisible(false);
con.add(jb);
con.add(jl);
con.add(jbox);
con.add(jtf2);
con.add(jb2);
con.add(jb3);
jbox.addItemListener(this);
jb.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
try
{socket=new Socket("localhost",4331);
dis=new DataInputStream(socket.getInputStream());
dos=new DataOutputStream(socket.getOutputStream());
th=new clientThread(socket,this);
th.start();
}
catch(IOException ee){}
}
public void itemStateChanged(ItemEvent e)
{if(e.getItemSelectable()==jbox)
{if(jbox.getSelectedIndex()==0)
StrPerson=null;
else
StrPerson=jbox.getSelectedItem().toString();
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb)
{String kk=null;
String ss=jtf1.getText();
if(ss!=null)
{if(StrPerson==null)
kk="MSG:"+ss;
else
kk="ONE"+StrPerson+":"+ss;
try
{dos.writeUTF(kk);}
catch(IOException e1){System.out.println("eee :" }
}
}
if(e.getSource()==jb2)
{
myName=jtf2.getText();
if((myName!=null)&&(myName.trim()!="")
{
jb.setEnabled(true);
try
{dos.writeUTF("LOGIN:"+myName);}
catch(IOException e1){System.out.println("eee :" }
}
}
}
public static void main(String args[])
{Client c=new Client();
c.setBounds(100,100,300,420);
c.setResizable(false);
c.setVisible(true);
}
}
class clientThread extends Thread
{DataInputStream in;
Socket client;
Client temp;
clientThread(Socket s,Client c)
{client=s;
temp=c;
try
{in=new DataInputStream(client.getInputStream());
}
catch (IOException e) {}
}
public void run()
{
String s=null;
try{
while(true)
{
s=in.readUTF();
if(!s.startsWith("NAMELIST:")
temp.jta.append(s+"\n"
else
{StringTokenizer token=new StringTokenizer(s.substring
(9),"#"
while(token.hasMoreTokens())
{
String sss=token.nextToken();
boolean b=true;
for(int y=0;y<(temp.jbox.getItemCount());y++)
{String ch=temp.jbox.getItemAt(y).toString();
if(ch.trim().equals(sss.trim())||(sss.equals
(temp.myName)))
{b=false;}
}
if(b)
temp.jbox.addItem(sss);
}
}
}
}
catch(IOException e1){}
}
}
服务器端:
import java.net.*;
import java.io.*;
import java.util.*;
public class Server implements Runnable
{public static final int PORT=4331;
protected ServerSocket listen;
static Vector connections;
String nameList="";
Thread connect;
public Server()
{try{listen=new ServerSocket(PORT);}
catch(IOException e){System.err.println("erro:"+e);System.exit(1);}
connections=new Vector(10);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 Server();}
public void broadcast(String msg)
{int i; firstthread you;
for(i=0;i<connections.size();i++)
{you=(firstthread)connections.elementAt(i);
System.out.println("you :"+you.toString());
try
{you.out.writeUTF(msg);}
catch (IOException e){}
}
}
public void sendmsg(String msg,String person)
{int i; firstthread you;
for(i=0;i<connections.size();i++)
{you=(firstthread)connections.elementAt(i);
if(you.name.equals(person))
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 Server server;
protected DataInputStream in;
firstthread d;
public firstthread(Server 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) {}
}
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();
try
{d=(firstthread)server.connections.elementAt(server.connections.indexOf(this));}
catch(Exception ee){}
if(line.startsWith("MSG:"))
{if(d.name==null)
{try
{d.out.writeUTF("!!你没有登陆!请先登陆!!");}
catch (IOException e){}
}
else
{server.broadcast(d.name+" 说: "+line.substring(4));}
}
else if(line.startsWith("LOGIN:"))
{d.name=line.substring(6);
server.broadcast("欢迎"+line.substring(6)+"上线");
server.nameList=server.nameList+line.substring(6)+"#";
server.broadcast("NAMELIST:"+server.nameList);
}
else if(line.startsWith("ONE"))
{int nn=line.indexOf(":",1);
server.sendmsg(d.name+" 说: "+line.substring(nn),line.substring(3,nn));
}
}
}
catch (IOException e)
{server.connections.removeElement(this);}
catch(NullPointerException e) { server.connections.removeElement(this);}
}
} |
|