java简单的局域网内聊天源码

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

此代码实现了简单的局域网内聊天,可以参考学习
/**
* 客户端界面代码
*/
import javax.swing.*;
public class MyClient extends JFrame{

public static JTextArea jta=null;
public static JTextArea jta1=null;
public static JButton jbt=null;
public static JScrollPane jsp=null;
public static JPanel jpl=null;

public MyClient()
{
//创建各组件
jta=new JTextArea();
jsp=new JScrollPane(jta);
jta1=new JTextArea(4,15);
jbt=new JButton("发送");

jpl=new JPanel();
//显示布局
jpl.add(jta1);
jpl.add(jbt);

this.add(jsp,"Center");
this.add(jpl,"South");
//创建主窗口
this.setBounds(200, 200, 300, 300);
this.setTitle("我的QQ客户端");
this.setVisible(true);
}

}
/**
* 客户端执行代码
*/
import java.io.*;
import java.awt.event.*;
import .*;

public class clientjava implements ActionListener{

Socket s=null;
PrintWriter pw=null;

public static void main(String[] args) {
new MyClient();
new clientjava();

}

public clientjava()
{
MyClient.jbt.addActionListener(this);
try {
s=new Socket("192.168.1.111",9988);

InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);

pw=new PrintWriter(s.getOutputStream(),true);
while(true)
{
//不停在服务器读取消息
String info=br.readLine();
MyClient.jta.append("客户端对服务器说:"+info+"\r\n");

}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==MyClient.jbt)
{
String info=MyClient.jta1.getText();
//把客户端的消息不停的追加到jta中
MyClient.jta.append("客户端对服务器说:"+info+"\r\n");

pw.println(info);

MyClient.jta1.setText("");
}
}

}
/**
* 服务器端界面代码
*/
import javax.swing.*;
public class MyServer extends JFrame{

public static JTextArea jta=null;
public static JTextArea jta1=null;
public static JButton jbt=null;
public static JScrollPane jsp=null;
public static JPanel jpl=null;

public MyServer()
{
//创建各组件
jta=new JTextArea();
jsp=new JScrollPane(jta);
jta1=new JTextArea(4,15);
jbt=new JButton("发送");

jpl=new JPanel();
//显示布局
jpl.add(jta1);
jpl.add(jbt);

this.add(jsp,"Center");
this.add(jpl,"South");
//创建主窗口
this.setBounds(200, 200, 300, 300);
this.setTitle("我的QQ服务器端");
this.setVisible(true);
}

}
/**
* 服务器端执行代码
*/
import .*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class Serverjava implements ActionListener{
//把信息发送给客户端的对象
PrintWriter

pw=null;
ServerSocket ss=null;
public static void main(String[] args) {

new MyServer();
new Serverjava();
}
public Serverjava()
{
MyServer.jbt.addActionListener(this);

try {
//服务器端监听9988端口
ss=new ServerSocket(9988);
//等待客户连接,阻塞
Socket s=ss.accept();
//读取从客户端发来的信息
InputStreamReader isr=new InputStreamReader(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
//向客户端发送消息
pw=new PrintWriter(s.getOutputStream(),true);
//读取从客户端发来的信息循环读取
while(true)
{
String info=br.readLine();
//追加形式才可以
MyServer.jta.append("服务器对客户端说:"+info+"\r\n");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//如果用户按下发送按钮
if(arg0.getSource()==MyServer.jbt)
{
//把内容发送给客户端
String info=MyServer.jta1.getText();

MyServer.jta1.append("服务器对客户端说:"+info+"\r\n");

pw.println(info);
//清空发送内容
MyServer.jta1.setText("");
}
}

}

相关文档
最新文档