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;
}
}
/**
* @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;
}
}