关闭

Socket:client

463人阅读 评论(0) 收藏 举报
package comm;
/**
 * @docRoot:socket客户端程序
 * @author:jingshaoping
 * @date:2007-9-17
 */
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Client_Socket extends JFrame {
 private static final long serialVersionUID = 1L;
 
 /**窗体上面部分的面板*/
 private JPanel pan_top;
 /**窗体中间部分的面板*/
 private JPanel pan_center;
 /**窗体下面部分的面板*/
 private JPanel pan_south;
 /**聊天消息部分的滚动面板*/
 private JScrollPane jsp_chat_scroll_contents;
 /**聊天内容显示*/
 private JTextArea  text_area_chat_contents;
 /**建立新的连接*/
 private JButton btn_connection;
 /**要发送的信息*/
 private JTextField text_message;
 /**要发送按钮*/
 private JButton send;
 /**发送的数据**/
 private DataOutputStream out;
 /**传入的数据**/
 private DataInputStream in;
 /**Socket**/
 private Socket socket;
 /**构造方法**/
 public Client_Socket()
 {
  try
  {
   jinint();
  }catch(Exception e)
  {
   JOptionPane.showMessageDialog(this,"程序初始化没有完成");
  }
 }
 /**
  * @docRoot 对程序进行初始化
  */
 private void jinint()
 {
  /**添加连接按钮**/
  pan_top = new JPanel();
  btn_connection =new JButton("建立连接");
  pan_top.add(btn_connection);
  this.getContentPane().add(pan_top,BorderLayout.NORTH);
  /**添加聊天内容**/
  text_area_chat_contents = new JTextArea(12,55);
  text_area_chat_contents.setEditable(false);
  text_area_chat_contents.setFont(new Font("黑体",0,14));
  jsp_chat_scroll_contents = new JScrollPane(text_area_chat_contents);
  pan_center = new JPanel();
  pan_center.add(jsp_chat_scroll_contents);
  this.getContentPane().add(pan_center,BorderLayout.CENTER);
  /**添加message和发送按钮**/
  text_message = new JTextField(30);
  text_message.setFont(new Font("黑体",1,16));
  send = new JButton("发送");
  pan_south = new JPanel();
  pan_south.add(text_message);
  pan_south.add(send);
  this.getContentPane().add(pan_south,BorderLayout.SOUTH);
  /**窗体的总体布局与外观*/
  this.setTitle("聊天程序");
  this.setResizable(false);
  this.setSize(400,300); 
  Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  this.setLocation((screen.width-400)/2,(screen.height-300)/2);
  /**对程序的关闭添加监听*/
  this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  this.addWindowListener(new WindowListener(){
   public void windowOpened(WindowEvent arg0) {}
   public void windowClosing(WindowEvent arg0) {}
   public void windowClosed(WindowEvent arg0) {
    try
    {
     out.writeUTF("bye");
     appendMessage("bye"); 
    }catch(Exception e)
    {
    }
    appClose();  
   }
   public void windowIconified(WindowEvent arg0) {}
   public void windowDeiconified(WindowEvent arg0) {}
   public void windowActivated(WindowEvent arg0) {}
   public void windowDeactivated(WindowEvent arg0) {}
  });
  this.setVisible(true);
  /**添加send控件的监听**/
  send.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0) {
    sendMessage();
   }
  }
  );
  /**添加btn_connection控件的监听**/
  btn_connection.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent arg0) {
    createNewConnection();
   }
  });
 }
 /**建立新的连接**/
 private void createNewConnection()
 {
  try
     {
     socket = new Socket("202.202.1.22",3000);
     out = new DataOutputStream(socket.getOutputStream());
     in= new DataInputStream(socket.getInputStream());
     out.writeUTF("建立连接成功");
     MyThread mythread = new MyThread(this);
     mythread.start();
     }catch(Exception e)
     {
      JOptionPane.showMessageDialog(this,"建立连接失败");
     }
 }
 /**发送消息**/
 private void sendMessage()
 {
  if(!text_message.getText().equalsIgnoreCase(""))
  {
   try
   {
    out.writeUTF(text_message.getText());
    appendMessage("客户机说:"+text_message.getText()+"/n");
    text_message.setText("");
    text_message.setFocusable(true);
   }catch(Exception e)
   {
    JOptionPane.showMessageDialog(this,"消息发送失败,可能是没有建立连接");
   }
  }
 }
 /**关闭应用程序**/
 private void appClose()
 {
  dispose();
  System.exit(0);
 }
 /**main testProgram**/
 public static void main(String[] args)
 {
  new Client_Socket();
 }
 /**传出in**/
 public DataInputStream getIn() {
  return in;
 }
 /**更新text_area_chat_contents里的内容**/
 public void appendMessage(String str)
 {
  text_area_chat_contents.append(str);
 }
 /**传出out**/
 public DataOutputStream getOut() {
  return out;
 }
 /**获取socket**/
 public Socket getSocket() {
  return socket;
 }
}
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:133042次
    • 积分:2918
    • 等级:
    • 排名:第12149名
    • 原创:163篇
    • 转载:1篇
    • 译文:0篇
    • 评论:5条
    文章分类