急需一个用java 语言写的聊天程序

急需一个用java 语言写的聊天程序,第1张

客户端:
package chatroom;
import javaawt;
import javaawtevent;
import javanet;
import javaio;
import javaxswing;
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("聊天室客户端");
thissetSize(800,600);
thissetResizable(false);
conn=new JPanel();
text=new JPanel();
send=new JPanel();
thisgetContentPane()add(conn);
connsetBounds(0, 0, thisgetWidth(),50);
name=new JLabel("姓名:");
who=new JTextField();
con=new JButton("连接");
connsetLayout(null);
connadd(name);
namesetBounds(30, 10, 50, 25);
connadd(who);
whosetBounds(80, 10, 150, 25);
connadd(con);
consetBounds(250,10, 60, 25);
thisgetContentPane()add(text);
textsetBounds(0,50,thisgetWidth(),450);
textsetLayout(new BorderLayout());
jsp=new JScrollPane();
talk=new JTextArea();
jspgetViewport()setView(talk);
textadd(jsp,"Center");
talksetLineWrap(true);
thisgetContentPane()add(send);
sendsetLayout(null);
sendsetBounds(0, 480, thisgetWidth(), 150);
sendto=new JLabel("发送到:");
snd=new JButton("发送");
list=new JComboBox();
say=new JTextField();
clear=new JButton("清空");
sendadd(sendto);
sendtosetBounds(30, 525, 50, 25);
sendadd(list);
listsetBounds(100, 525, 75, 25);
sendadd(say);
saysetEditable(true);
saysetBounds(200, 525, 300, 25);
sendadd(snd);
sndsetBounds(520, 525, 100, 25);
sendadd(clear);
clearsetBounds(650, 525, 100, 25);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thissetVisible(true);
conaddActionListener(this);
sndaddActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (aegetActionCommand()equals("连接"))
{
try
{
client=new Socket(InetAddressgetLocalHost(),55555);
talkappend("连接成功\n");
consetText("断开");
is=clientgetInputStream();
os=clientgetOutputStream();
th=new Thread(this);
String id=whogetText();
byte b[]=idgetBytes();
DataOutputStream dos=new DataOutputStream(os);
int len=blength;
doswriteInt(len);
doswrite(b);
thstart();
}catch(Exception e){talkappend("连接失败\n"+etoString()+"0000");}
}
else if(aegetSource()==snd)
{
if(listgetSelectedItem()toString()=="所有人")
{
yousay="@open"+saygetText();
byte b[]=yousaygetBytes();
DataOutputStream dos=new DataOutputStream(os);
try{
int len=blength;
doswriteInt(len);
doswrite(b);
}catch(Exception e)
{
Systemoutprint(etoString()+"1111");
}
}
else
{
yousay="@whisper"+saygetText();
byte b[]=yousaygetBytes();
byte w[]=listgetSelectedItem()toString()getBytes();
DataOutputStream dos=new DataOutputStream(os);
try{
int len=blength;
int wlen=wlength;
doswriteInt(len); //内容
doswrite(b);
doswriteInt(wlen); //发送对象
doswrite(w);
}catch(Exception e)
{
Systemoutprint(etoString()+"AAAA");
}
}
}
else if(aegetActionCommand()equals("断开"))
{
try
{
clientclose();
talkappend("连接已断开\n");
consetText("连接");
}catch(Exception e){Systemoutprintln(etoString()+"2222");}
}
}
public void run()
{
while(true)
{
try
{
dis=new DataInputStream(is);
tof=disreadDouble();
if(tof==11)
{
dis=new DataInputStream(is);
int number;
listremoveAllItems();
listaddItem("所有人");
number=disreadInt();
for(int i=0;i<number;i++)
{
int len=disreadInt();
byte b[]=new byte[len];
disread(b);
String name=new String(b);
listaddItem(name);
}
}
else if(tof==22)
{
try
{
dis=new DataInputStream(is);
int len=disreadInt();
byte b[]=new byte[len];
disread(b);
receive=new String(b);
talkappend(receive+"\n");
}catch(Exception e){JOptionPaneshowMessageDialog(this, "连接已断开","消息",JOptionPaneINFORMATION_MESSAGE);break;}
}
}catch(Exception e){JOptionPaneshowMessageDialog(this, "连接已断开","消息",JOptionPaneINFORMATION_MESSAGE);break;}
}
}
public static void main(String[] args) {
client cl=new client();
}
}
服务端:
package chatroom;
import javaawt;
import javaxswing;
import javanet;
import javautilVector;
import javaio;
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("聊天室服务端");
thisgetContentPane()setLayout(new GridLayout(1,1));
thissetSize(400,300);
jp1=new JPanel();
jta=new JTextArea();
jsp=new JScrollPane();
thisgetContentPane()add(jp1);
jp1setLayout(new GridLayout(1,1));
jspgetViewport()setView(jta);
jp1add(jsp);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
server sr=new server();
srsr=new Thread(sr);
srsrstart();
sendtoall sta=new sendtoall();
returnfrom rf=new returnfrom(sr);
returnto rt=new returnto(sr);
stastart();
rfstart();
rtstart();
}
public void run()
{
try
{
server=new ServerSocket(55555);
while(true)
{
socket=serveraccept();
is=socketgetInputStream();
DataInputStream dis=new DataInputStream(is);
int len=disreadInt();
byte b[]=new byte[len];
disread(b);
String id=new String(b);
record v=new record(id,socket);
oladdElement(v);
for (int i=0;i<olsize();i++)
{
if (olelementAt(i)equals(v))
{
no=i;
}
}
login=1;
s=id+"已来到聊天室!";
th[no]=new Thread(new receive(this,no));
th[no]start();
}
}catch(Exception e){Systemoutprintln(etoString()+"aaaa");}
}
}
class receive implements Runnable
{
InputStream is;
OutputStream os;
server sr;
Socket sk;
int i;
String name;
String ip;
receive(server sr,int i)
{
thissr=sr;
thisi=i;
sk=((record)srolelementAt(i))ip;
name=((record)srolelementAt(i))name;
ip=((record)srolelementAt(i))ipgetInetAddress()toString();
}
public void run()
{
while(true)
{
try
{
is=skgetInputStream();
DataInputStream dis=new DataInputStream(is);
int len=disreadInt();
byte b[]=new byte[len];
disread(b);
String abc=new String(b);
srjtaappend(abc);
if(abcsubstring(0,5)equals("@open"))
{
servers=name+"["+ipsubstring(1, iplength())+"]"+"说:"+abcsubstring(5,abclength());
srjtaappend(servers+"\n");
}
else if(abcsubstring(0,8)equals("@whisper"))
{
int wlen=disreadInt();
srw=new byte[wlen];
disread(srw);
serverto=new String(srw);
serverfrom=((record)srolelementAt(i))name;
String ip=((record)srolelementAt(i))ipgetInetAddress()toString();
// servers=serverfrom+"对"+serverto+"["+ipsubstring(1, iplength())+"]"+"悄悄地说:"+abcsubstring(8,abclength());
serverfromtext="你对"+serverto+"["+ipsubstring(1, iplength())+"]"+"悄悄地说:"+abcsubstring(8,abclength());
servertotext=serverfrom+"["+ipsubstring(1, iplength())+"]"+"对你悄悄地说:"+abcsubstring(8,abclength());
srjtaappend(servers+"\n");
}
}catch(Exception e)
{
try
{
DataOutputStream dos=new DataOutputStream(os);
serverolremoveElementAt(i);
servers=name+"已离开聊天室";
serverlogin=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(serverlogin==1)
{
number=0;
number=serverolsize();
dos=new DataOutputStream(os);
for(int i=0;i<serverolsize();i++)
{
st=((record)srolelementAt(i))ip;
os=stgetOutputStream();
dos=new DataOutputStream(os);
doswriteDouble(11);
doswriteInt(number);
for (int j=0;j<number;j++)
{
String name=((record)srolelementAt(j))name;
byte b[]=namegetBytes();
int len=blength;
doswriteInt(len);
doswrite(b);
}
}
serverlogin=0;
}
else if(!serversequals("")&&srolsize()>0)
{
b=serversgetBytes(); //String类型中 汉字占一个字节,但是byte类型中,汉字占两个字节
len=blength;
//len=serverslength();
//b=new byte[len];
//b=serversgetBytes();
for(int i=0;i<serverolsize();i++)
{
st=((record)srolelementAt(i))ip;
os=stgetOutputStream();
DataOutputStream dos=new DataOutputStream(os);
doswriteDouble(22);
doswriteInt(len);
doswrite(b);
}
servers="";
}
}catch(Exception e){Systemoutprintln(etoString()+"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)
{
thissr=sr;
}
public void run()
{
while(true)
{
if(!serverfromtextequals(""))
{
b=serverfromtextgetBytes();
len=blength;
srjtaappend(srfromtext);
try
{
for(int i=0;i<serverolsize();i++)
{
if(((record)srolelementAt(i))nameequals(serverfrom))
{
st=((record)srolelementAt(i))ip;
os=stgetOutputStream();
DataOutputStream dos=new DataOutputStream(os);
doswriteDouble(22);
doswriteInt(len);
doswrite(b);
}
}
}catch(Exception e){Systemoutprintln(etoString()+">
String to="test1@qqcom,test2@gmailcom,test3@163com";
InternetAddress[] toList = new InternetAddress()parse(to);
msgsetRecipients(MessageRecipientTypeTO, toList);

Transportsend(msg);
或者直连邮件网关,并由邮件网关直接转发至收件箱所在的服务器,因此发送速度是基本不受限的。如果没有邮件网关,同时又不想自己完成相关功能,那么建议不要只让一台邮件服务器转发你的邮件,而是多连几台,将自己的邮件平均分配到每台邮件服务器上,这样就不会由于发送过多,而被拒了。
至于处理能力,可以提几点关于提高发送能力的建议
1.SMTP协议是支持长连接的协议,同时mailjar实现了SMTP协议的连接、邮件发送及连接断开,至于对连接的维护没有涉及;同时Spring中的邮件支持是通过封装mailjar实现的,但只提供了单笔发送和批量发送(均为短连接)。
2.如果使用长连接,建议再引入连接池,那样既便于维护连接,又可以提高连接的使用率。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/12680960.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-27
下一篇2025-08-27

发表评论

登录后才能评论

评论列表(0条)

    保存