简单的java聊天程序
2015-11-25 16:50
369 查看
学习了Socket编程,做了一个简单的聊天程序练习,代码如下 :
//服务器端 import java.awt.BorderLayout; import java.awt.Button; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class SimpleChat implements ActionListener { JFrame f=new JFrame("chat测试版服务器端");//容器 JTextArea chatCotent=new JTextArea();//聊天区 JTextField sendCotent=new JTextField(15);//待发送框内容 Button send=new Button("发送");//发送按钮 Thread recieveThread=new Thread(); //控制接收线程 ServerSocket server; Socket client; InputStream is; OutputStream os; String ip=null; public static void main(String argv[]) throws IOException{ new SimpleChat(); } public SimpleChat() throws IOException{ f.setLayout(new BorderLayout()); f.setSize(600, 400); f.setLocationRelativeTo(null); JPanel p1=new JPanel(); f.add("Center",chatCotent); p1.add(sendCotent); p1.add(send); f.add("South",p1); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); send.addActionListener(this);//监听发送按钮 sendCotent.addActionListener(this);//监听文本框 server=new ServerSocket(8888); client=server.accept(); chatCotent.append("客户端"+client.getInetAddress().getHostName()+"已连接\n\n"); ip=client.getInetAddress().getHostName(); os=client.getOutputStream(); is=client.getInputStream(); recieveThread=new Thread(new Myrun()); recieveThread.start(); } //设置接收 class Myrun implements Runnable{ public void run() { while(true){ try { is = client.getInputStream(); byte buff[]=new byte[512]; is.read(buff); String cotent=new String(buff); if(sendCotent.getText()!=null) chatCotent.append(ip+cotent+":\n\n"); } catch (IOException e) { e.printStackTrace(); } } } } //信息发送 public void actionPerformed(ActionEvent e){ try { byte buf[]=sendCotent.getText().getBytes(); chatCotent.append(ip+":\n"+sendCotent.getText()); os.write(buf); chatCotent.append("\n\n"); sendCotent.setText(""); } catch (IOException e1) { e1.printStackTrace(); } } }
//客户端 import java.awt.BorderLayout; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class chatClient implements ActionListener { JFrame f=new JFrame("chat测试版客户端");//容器 JTextArea chatCotent=new JTextArea();//聊天区 JTextField sendCotent=new JTextField(15);//待发送框内容 Button send=new Button("发送");//发送按钮 Socket client; OutputStream os; InputStream is; String ip=null; public chatClient() throws IOException{ f.setLayout(new BorderLayout()); f.setSize(600, 400); f.setLocationRelativeTo(null); JPanel p1=new JPanel(); f.add("Center",chatCotent); p1.add(sendCotent); p1.add(send); f.add("South",p1); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); send.addActionListener(this); sendCotent.addActionListener(this); Socket client=new Socket("192.168.1.105",8888); chatCotent.append("已经和服务器连接:"+client.getInetAddress().getHostName()+"\n\n"); ip=client.getInetAddress().getHostName(); is=client.getInputStream(); os=client.getOutputStream(); Thread recieveThread=new Thread(new Myrun()); recieveThread.start(); } public static void main(String[] args) throws IOException { new chatClient(); } //接受信息 class Myrun implements Runnable{ public void run() { while(true){ try { byte buff[]=new byte[512]; is.read(buff); String cotent=new String(buff); if(sendCotent.getText()!=null) chatCotent.append(ip+cotent+":\n\n"); } catch (IOException e) { e.printStackTrace(); } } } } //信息发送 public void actionPerf 7f48 ormed(ActionEvent e) { try { byte buf[]=sendCotent.getText().getBytes(); chatCotent.append(ip+":\n"+sendCotent.getText()); os.write(buf); chatCotent.append("\n\n"); sendCotent.setText(""); } catch (IOException e1) { e1.printStackTrace(); } } }
相关文章推荐
- java向上转型和向下转型
- JAVA线程安全之synchronized关键字的正确用法
- Eclipse下安装Scala插件
- java设计模式之单利模式
- Spring概述
- JAVA正则表达式匹配,替换,查找,切割
- 走进Java(五)JSTL和EL表达式
- Spring MVC 接收Json格式参数
- lombok eclipse 安装安装
- Java NIO (六) Selector
- java 静态代码块 静态方法区别
- 构建轻量级webservice-Spring整合jaxws开发
- ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed
- Struts1.1应用初步
- JAVA中的jna
- Java 传递参数 中文乱码解决
- CKEditor struts2上传
- eclipse下maven项目的创建以及问题的解决
- Eclipse配置
- java.lang.RuntimeException: Method called after release()