用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

Sommer    -  云代码空间

——

TCP聊天小程序

2013-03-12|1694阅||

摘要:GUI写的javaTCP通讯


//GUI布局及主函数

package cn.itcast.TCPDemo2;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class ChatGui {

 private Frame f;
 private TextArea read,write;
 private Button b;
 
 public ChatGui() {
  init();
 }
 public void init(){
  f=new Frame("TCP");
  f.setBounds(300, 100, 430, 500);
  f.setLayout(new FlowLayout());
  read=new TextArea(18,50);
  write=new TextArea(7,50);
  read.setEditable(false);
  
  b=new Button("发送");
  
  f.add(read);
  f.add(write);
  f.add(b);

  f.setVisible(true);
  event();
 }
 public void event(){
  f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
       System.exit(0);
      }

  });
 
 
  b.addActionListener(new ActionListener() {
   
  
   public void actionPerformed(ActionEvent e) {
    mothed();
    
   }
  });
  //键盘事件暂不可用,待维护
  /*b.addKeyListener(new KeyAdapter(){
  
   public void keyPressed(KeyEvent e) {
    if (e.isShiftDown()&&(e.getKeyCode()==e.VK_ENTER)){
     mothed();
    }
   }
  });*/
 
 }
 public void mothed(){
  
  try {
   Socket s=new Socket("127.0.0.1",10002);
   
   new Thread(new TcpClient(s, write.getText())).start();
   
   write.setText("");
  } catch (Exception e1) {
   // TODO Auto-generated catch block
   new RuntimeException();
  }
 }
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  ChatGui cg=new ChatGui();
  ServerSocket ss=new ServerSocket(10002);
  new Thread(new TcpServer(ss, cg.read)).start();
  
 }

}
//客户端代码

package cn.itcast.TCPDemo2;

import java.awt.TextArea;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

public class TcpClient implements Runnable {

 private Socket s;
 private String text;
 TcpClient(Socket s,String text){
  this.s=s;
  this.text=text;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  try {
   PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
   
   if(text!=""){
    pw.println(text);
    
   }
  } catch (IOException e) {
   new RuntimeException();
  }
 }

}
//服务端代码

package cn.itcast.TCPDemo2;

import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import javax.xml.soap.Text;

public class TcpServer implements Runnable {

 private ServerSocket ss;
 private TextArea ta;
 TcpServer(ServerSocket ss,TextArea ta){
  this.ss=ss;
  this.ta=ta;
 }
 public void run() {
  while(true){
   
   try {
    Socket s=ss.accept();
    String ip=s.getInetAddress().getHostAddress();
    InputStreamReader bfr=
      new InputStreamReader(s.getInputStream());
    
    //假如此处代码为一下,则每次发送的只能是第一行,换行后将无法显示
    /*
    String line=null;
    if((line=bfr.readLine()).length()>0){
     ta.append("("+ip+")"+" "+Tool.time()+"\r\n"+line+"\r\n\r\n");
    }*/
   
    int num=0;
    char[] buf=new char[1024];
    if((num=bfr.read(buf))>0){
     ta.append("("+ip+")"+" "+Tool.time()+"\r\n"+new String(buf,0,num)+"\r\n\r\n");
     
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    new RuntimeException();
   }
  }
 
 }

}
//工具类使用,时间

package cn.itcast.TCPDemo2;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Tool {
 public static String time(){
  Date d=new Date();
  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  return sdf.format(d);
  
 }
}

顶 9踩 1收藏
文章评论
    发表评论

    个人资料

    • 昵称: Sommer
    • 等级: 初级程序员
    • 积分: 35
    • 代码: 3 个
    • 文章: 2 篇
    • 随想: 0 条
    • 访问: 6 次
    • 关注

    最新提问

      站长推荐