简单的java聊天程序

学习了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 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();
				}
	    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值