ServerSocket:Socket & ServerSocket

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 Server_Socket extends JFrame {
 private static final long serialVersionUID = 1L;
 
 /**窗体中间部分的面板*/
 private JPanel pan_center;
 /**窗体下面部分的面板*/
 private JPanel pan_south;
 /**聊天消息部分的滚动面板*/
 private JScrollPane jsp_chat_scroll_contents;
 /**聊天内容显示*/
 private JTextArea  text_area_chat_contents;
 /**要发送的信息*/
 private JTextField text_message;
 /**要发送按钮*/
 private JButton send;
 /**发送的数据**/
 private DataOutputStream out;
 /**传入的数据**/
 private DataInputStream in;
 /**Socket**/
 private Socket socket;
 /**serversocket**/
 private ServerSocket serversocket;
 /**构造方法**/
 public Server_Socket()
 {
  try
  {
   jinint();
  }catch(Exception e)
  {
   JOptionPane.showMessageDialog(this,"程序初始化没有完成");
  }
 }
 /**
  * @docRoot 对程序进行初始化
  */
 private void jinint()
 {
  /**添加聊天内容**/
  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);
  /**对等待连接的端口进行初始化**/
  try
  {
   serversocket = new ServerSocket(3000);
   JOptionPane.showMessageDialog(this,"连接成功");
  }catch(Exception e)
  {
   JOptionPane.showMessageDialog(this,"连接失败");
  }
  /**建立连接**/
  try
  {
   socket = serversocket.accept();
   out = new DataOutputStream(socket.getOutputStream());
   in = new DataInputStream(socket.getInputStream());
   out.writeUTF("客户机连接成功");
   MyThread mythread = new MyThread(this);
   mythread.start();
  }catch(Exception e){
  }
  /**对程序的关闭添加监听*/
  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();
   }
  }
  );
 }
 /**发送消息**/
 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 Server_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;
 }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值