java小聊天程序介绍
服务器端:
packageas;
importjava.awt.BorderLayout;
importjava.awt.Container;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.*;
importjava.net.ServerSocket;
importjava.net.Socket;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclassCharServerTextextendsJFrameimplementsActionListener
{
JLabeljl=newJLabel("输入您要说的话");
JPaneljp=newJPanel();
JTextFieldjtf=newJTextField(15);
JTextAreajta=newJTextArea(10,10);
ServerSocketss;
Socketsk;
BufferedReaderbr;
PrintWriterpw;
publicCharServerText()
{
super("Server端");
Containercon=getContentPane();
jp.add(jl);
jp.add(jtf);
con.add(jp,BorderLayout.SOUTH);
con.add(jta,BorderLayout.CENTER);
jtf.addActionListener(this);
addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
}
);
setSize(300,250);
show();
try
{
ss=newServerSocket(9010);
sk=ss.accept();
System.out.println("1111");
jta.append("已连接到客户机"+sk.getInetAddress().getHostName()+" ");
br=newBufferedReader(newInputStreamReader(sk.getInputStream()));
pw=newPrintWriter(sk.getOutputStream(),true);
while(true)
{
Stringii=br.readLine();
if(ii!=null){
System.out.println("1");
jta.append("Client说:"+ii+" ");
}
}
}
catch(Exceptione)
{
e.printStackTrace();
}
finally
{
try{
ss.close();
sk.close();
br.close();
pw.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
publicvoidactionPerformed(ActionEventarg0)
{
try{
Stringstr=jtf.getText();
jtf.setText("");
pw.println(str);
pw.flush();
jta.append("Server说:"+str+" ");
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args)
{
newCharServerText();
}
}
importjava.awt.BorderLayout;
importjava.awt.Container;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.*;
importjava.net.ServerSocket;
importjava.net.Socket;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclassCharServerTextextendsJFrameimplementsActionListener
{
JLabeljl=newJLabel("输入您要说的话");
JPaneljp=newJPanel();
JTextFieldjtf=newJTextField(15);
JTextAreajta=newJTextArea(10,10);
ServerSocketss;
Socketsk;
BufferedReaderbr;
PrintWriterpw;
publicCharServerText()
{
super("Server端");
Containercon=getContentPane();
jp.add(jl);
jp.add(jtf);
con.add(jp,BorderLayout.SOUTH);
con.add(jta,BorderLayout.CENTER);
jtf.addActionListener(this);
addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
}
);
setSize(300,250);
show();
try
{
ss=newServerSocket(9010);
sk=ss.accept();
System.out.println("1111");
jta.append("已连接到客户机"+sk.getInetAddress().getHostName()+" ");
br=newBufferedReader(newInputStreamReader(sk.getInputStream()));
pw=newPrintWriter(sk.getOutputStream(),true);
while(true)
{
Stringii=br.readLine();
if(ii!=null){
System.out.println("1");
jta.append("Client说:"+ii+" ");
}
}
}
catch(Exceptione)
{
e.printStackTrace();
}
finally
{
try{
ss.close();
sk.close();
br.close();
pw.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
publicvoidactionPerformed(ActionEventarg0)
{
try{
Stringstr=jtf.getText();
jtf.setText("");
pw.println(str);
pw.flush();
jta.append("Server说:"+str+" ");
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args)
{
newCharServerText();
}
}
本文地址:http://www.45fan.com/dnjc/71803.html