當前位置:編程學習大全網 - 源碼下載 - 急需壹個用java 語言寫的聊天程序

急需壹個用java 語言寫的聊天程序

客戶端:

package chatroom;

import java.awt.*;

import java.awt.event.*;

import java.net.*;

import java.io.*;

import javax.swing.*;

public class client extends JFrame implements ActionListener,Runnable{

JPanel conn,text,send;

JLabel name,sendto;

JComboBox list;

JButton con,snd,clear;

JTextArea talk;

JTextField who,say;

JScrollPane jsp;

Socket client;

InputStream is;

OutputStream os;

PrintStream ps;

BufferedReader br;

String receive,yousay;

Thread th;

DataInputStream dis;

Double tof;

client()

{

super("聊天室客戶端");

this.setSize(800,600);

this.setResizable(false);

conn=new JPanel();

text=new JPanel();

send=new JPanel();

this.getContentPane().add(conn);

conn.setBounds(0, 0, this.getWidth(),50);

name=new JLabel("姓名:");

who=new JTextField();

con=new JButton("連接");

conn.setLayout(null);

conn.add(name);

name.setBounds(30, 10, 50, 25);

conn.add(who);

who.setBounds(80, 10, 150, 25);

conn.add(con);

con.setBounds(250,10, 60, 25);

this.getContentPane().add(text);

text.setBounds(0,50,this.getWidth(),450);

text.setLayout(new BorderLayout());

jsp=new JScrollPane();

talk=new JTextArea();

jsp.getViewport().setView(talk);

text.add(jsp,"Center");

talk.setLineWrap(true);

this.getContentPane().add(send);

send.setLayout(null);

send.setBounds(0, 480, this.getWidth(), 150);

sendto=new JLabel("發送到:");

snd=new JButton("發送");

list=new JComboBox();

say=new JTextField();

clear=new JButton("清空");

send.add(sendto);

sendto.setBounds(30, 525, 50, 25);

send.add(list);

list.setBounds(100, 525, 75, 25);

send.add(say);

say.setEditable(true);

say.setBounds(200, 525, 300, 25);

send.add(snd);

snd.setBounds(520, 525, 100, 25);

send.add(clear);

clear.setBounds(650, 525, 100, 25);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

con.addActionListener(this);

snd.addActionListener(this);

}

public void actionPerformed(ActionEvent ae) {

if (ae.getActionCommand().equals("連接"))

{

try

{

client=new Socket(InetAddress.getLocalHost(),55555);

talk.append("連接成功...\n");

con.setText("斷開");

is=client.getInputStream();

os=client.getOutputStream();

th=new Thread(this);

String id=who.getText();

byte b[]=id.getBytes();

DataOutputStream dos=new DataOutputStream(os);

int len=b.length;

dos.writeInt(len);

dos.write(b);

th.start();

}catch(Exception e){talk.append("連接失敗\n"+e.toString()+"0000");}

}

else if(ae.getSource()==snd)

{

if(list.getSelectedItem().toString()=="所有人")

{

yousay="@open"+say.getText();

byte b[]=yousay.getBytes();

DataOutputStream dos=new DataOutputStream(os);

try{

int len=b.length;

dos.writeInt(len);

dos.write(b);

}catch(Exception e)

{

System.out.print(e.toString()+"1111");

}

}

else

{

yousay="@whisper"+say.getText();

byte b[]=yousay.getBytes();

byte w[]=list.getSelectedItem().toString().getBytes();

DataOutputStream dos=new DataOutputStream(os);

try{

int len=b.length;

int wlen=w.length;

dos.writeInt(len); //內容

dos.write(b);

dos.writeInt(wlen); //發送對象

dos.write(w);

}catch(Exception e)

{

System.out.print(e.toString()+"AAAA");

}

}

}

else if(ae.getActionCommand().equals("斷開"))

{

try

{

client.close();

talk.append("連接已斷開\n");

con.setText("連接");

}catch(Exception e){System.out.println(e.toString()+"2222");}

}

}

public void run()

{

while(true)

{

try

{

dis=new DataInputStream(is);

tof=dis.readDouble();

if(tof==1.1)

{

dis=new DataInputStream(is);

int number;

list.removeAllItems();

list.addItem("所有人");

number=dis.readInt();

for(int i=0;i<number;i++)

{

int len=dis.readInt();

byte b[]=new byte[len];

dis.read(b);

String name=new String(b);

list.addItem(name);

}

}

else if(tof==2.2)

{

try

{

dis=new DataInputStream(is);

int len=dis.readInt();

byte b[]=new byte[len];

dis.read(b);

receive=new String(b);

talk.append(receive+"\n");

}catch(Exception e){JOptionPane.showMessageDialog(this, "連接已斷開","消息",JOptionPane.INFORMATION_MESSAGE);break;}

}

}catch(Exception e){JOptionPane.showMessageDialog(this, "連接已斷開","消息",JOptionPane.INFORMATION_MESSAGE);break;}

}

}

public static void main(String[] args) {

client cl=new client();

}

}

服務端:

package chatroom;

import java.awt.*;

import javax.swing.*;

import java.net.*;

import java.util.Vector;

import java.io.*;

public class server extends JFrame implements Runnable{

JPanel jp1;

JTextArea jta;

JScrollPane jsp;

Socket socket=null;

ServerSocket server;

InputStream is;

OutputStream os;

static int i=0,login;

int no;

static String s="";

Thread sr;

Thread th[]=new Thread[20];

static Vector ol=new Vector();

static public byte w[]=null;

static String from=""; //人名

static String to="";

static String fromtext=""; //內容

static String totext="";

server()

{

super("聊天室服務端");

this.getContentPane().setLayout(new GridLayout(1,1));

this.setSize(400,300);

jp1=new JPanel();

jta=new JTextArea();

jsp=new JScrollPane();

this.getContentPane().add(jp1);

jp1.setLayout(new GridLayout(1,1));

jsp.getViewport().setView(jta);

jp1.add(jsp);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public static void main(String[] args) {

server sr=new server();

sr.sr=new Thread(sr);

sr.sr.start();

sendtoall sta=new sendtoall();

returnfrom rf=new returnfrom(sr);

returnto rt=new returnto(sr);

sta.start();

rf.start();

rt.start();

}

public void run()

{

try

{

server=new ServerSocket(55555);

while(true)

{

socket=server.accept();

is=socket.getInputStream();

DataInputStream dis=new DataInputStream(is);

int len=dis.readInt();

byte b[]=new byte[len];

dis.read(b);

String id=new String(b);

record v=new record(id,socket);

ol.addElement(v);

for (int i=0;i<ol.size();i++)

{

if (ol.elementAt(i).equals(v))

{

no=i;

}

}

login=1;

s=id+"已來到聊天室!";

th[no]=new Thread(new receive(this,no));

th[no].start();

}

}catch(Exception e){System.out.println(e.toString()+"aaaa");}

}

}

class receive implements Runnable

{

InputStream is;

OutputStream os;

server sr;

Socket sk;

int i;

String name;

String ip;

receive(server sr,int i)

{

this.sr=sr;

this.i=i;

sk=((record)sr.ol.elementAt(i)).ip;

name=((record)sr.ol.elementAt(i)).name;

ip=((record)sr.ol.elementAt(i)).ip.getInetAddress().toString();

}

public void run()

{

while(true)

{

try

{

is=sk.getInputStream();

DataInputStream dis=new DataInputStream(is);

int len=dis.readInt();

byte b[]=new byte[len];

dis.read(b);

String abc=new String(b);

sr.jta.append(abc);

if(abc.substring(0,5).equals("@open"))

{

server.s=name+"["+ip.substring(1, ip.length())+"]"+"說:"+abc.substring(5,abc.length());

sr.jta.append(server.s+"\n");

}

else if(abc.substring(0,8).equals("@whisper"))

{

int wlen=dis.readInt();

sr.w=new byte[wlen];

dis.read(sr.w);

server.to=new String(sr.w);

server.from=((record)sr.ol.elementAt(i)).name;

String ip=((record)sr.ol.elementAt(i)).ip.getInetAddress().toString();

// server.s=server.from+"對"+server.to+"["+ip.substring(1, ip.length())+"]"+"悄悄地說:"+abc.substring(8,abc.length());

server.fromtext="妳對"+server.to+"["+ip.substring(1, ip.length())+"]"+"悄悄地說:"+abc.substring(8,abc.length());

server.totext=server.from+"["+ip.substring(1, ip.length())+"]"+"對妳悄悄地說:"+abc.substring(8,abc.length());

sr.jta.append(server.s+"\n");

}

}catch(Exception e)

{

try

{

DataOutputStream dos=new DataOutputStream(os);

server.ol.removeElementAt(i);

server.s=name+"已離開聊天室.";

server.login=1;

break;

}catch(Exception f){}

}

}

}

}

class sendtoall extends Thread

{

int len,number;

byte b[];

server sr;

Socket st;

OutputStream os;

DataOutputStream dos;

public void run()

{

while(true)

{

try

{

if(server.login==1)

{

number=0;

number=server.ol.size();

dos=new DataOutputStream(os);

for(int i=0;i<server.ol.size();i++)

{

st=((record)sr.ol.elementAt(i)).ip;

os=st.getOutputStream();

dos=new DataOutputStream(os);

dos.writeDouble(1.1);

dos.writeInt(number);

for (int j=0;j<number;j++)

{

String name=((record)sr.ol.elementAt(j)).name;

byte b[]=name.getBytes();

int len=b.length;

dos.writeInt(len);

dos.write(b);

}

}

server.login=0;

}

else if(!server.s.equals("")&&sr.ol.size()>0)

{

b=server.s.getBytes(); //String類型中 漢字占壹個字節,但是byte類型中,漢字占兩個字節

len=b.length;

//len=server.s.length();

//b=new byte[len];

//b=server.s.getBytes();

for(int i=0;i<server.ol.size();i++)

{

st=((record)sr.ol.elementAt(i)).ip;

os=st.getOutputStream();

DataOutputStream dos=new DataOutputStream(os);

dos.writeDouble(2.2);

dos.writeInt(len);

dos.write(b);

}

server.s="";

}

}catch(Exception e){System.out.println(e.toString()+"sdasd");}

}

}

}

class returnfrom extends Thread

{

int len,wlen;

byte b[];

byte w[];

server sr;

Socket st;

OutputStream os;

DataOutputStream dos;

//String from="",to="";

returnfrom(server sr)

{

this.sr=sr;

}

public void run()

{

while(true)

{

if(!server.fromtext.equals(""))

{

b=server.fromtext.getBytes();

len=b.length;

sr.jta.append(sr.fromtext);

try

{

for(int i=0;i<server.ol.size();i++)

{

if(((record)sr.ol.elementAt(i)).name.equals(server.from))

{

st=((record)sr.ol.elementAt(i)).ip;

os=st.getOutputStream();

DataOutputStream dos=new DataOutputStream(os);

dos.writeDouble(2.2);

dos.writeInt(len);

dos.write(b);

}

}

}catch(Exception e){System.out.println(e.toString()+"wwww");}

server.fromtext="";

server.from="";

}

}

}

}

class returnto extends Thread

{

int len,wlen;

byte b[];

byte w[];

server sr;

Socket st;

OutputStream os;

DataOutputStream dos;

//String from="",to="";

returnto (server sr)

{

this.sr=sr;

}

public void run()

{

while(true)

{

if(!server.totext.equals(""))

{

w=server.totext.getBytes();

wlen=w.length;

try

{

for(int i=0;i<server.ol.size();i++)

{

if(((record)sr.ol.elementAt(i)).name.equals(server.to))

{

st=((record)sr.ol.elementAt(i)).ip;

os=st.getOutputStream();

DataOutputStream dos=new DataOutputStream(os);

dos.writeDouble(2.2);

dos.writeInt(wlen);

dos.write(w);

}

}

}catch(Exception e){System.out.println(e.toString()+"wwww");}

server.totext="";

server.to="";

}

}

}

}

class record

{

String name;

Socket ip;

record(String id,Socket socket)

{

name=id;

ip=socket;

}

}

以前學習java時寫的,是個在線聊天室只有群聊,私聊,上下線提示,搞不好裏面還有錯誤- -其他功能自己看著加吧,妳要的功能實在是很好很強大.

  • 上一篇:使用Netty搭建Dubbo
  • 下一篇:無極調速是什麽意思?
  • copyright 2024編程學習大全網