⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c6_02_05.java

📁 移动CMPP3.0协议短信网关连接程序
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class c6_02_05 extends JFrame implements ActionListener
{
  public static void main(String[] args)
    {
     c6_02_05 frame=new c6_02_05();
    }
  JButton command,command2,command3;
  JRadioButton rb[]=new JRadioButton[2];
  JTextArea tal;
  JTextField tf1,tf2,tf3;
  ServerSocket socket1;
  Socket insocket1,socket2;
  String inbuf;
  BufferedReader in1;
  PrintWriter out1;
  ml625_server t1;
  ml625_client t2;
  //构造函数
  public c6_02_05()
  {
  super("c6_02_05");
  Container c=getContentPane();
  c.setLayout(null);
  JLabel lb=new JLabel("TCP通信程序");
  lb.setFont(new Font("宋体",Font.BOLD,16));
  lb.setForeground(Color.black);
  lb.setSize(2000,20);
  lb.setLocation(10,2);
  c.add(lb);
  //JRadioButton组件
  String strl[]={"服务器端","客户端"};
  ButtonGroup bgl=new ButtonGroup();//按钮群组
  for(int i=0;i<2;i++)
      {
      rb[i]=new JRadioButton(strl[i]);
      rb[i].setFont(new Font("宋体",Font.BOLD,16));
      rb[i].setForeground(Color.black);
      rb[i].setSize(80,20);
      rb[i].setLocation(10+i*80,27);
      c.add(rb[i]);
      bgl.add(rb[i]);
      }
      rb[0].setSelected(true);

  JLabel lb1=new JLabel("连接主机IP");
  lb1.setFont(new Font("宋体",Font.BOLD,16));
  lb1.setForeground(Color.black);
  lb1.setSize(120,25);
  lb1.setLocation(10,55);
  c.add(lb1);

  tf1=new JTextField("127.0.0.1");
  tf1.setForeground(Color.black);
  tf1.setSize(250,25);
  tf1.setLocation(120,55);
  c.add(tf1);
  command=new JButton("连接");
  command.setFont(new Font("宋体",Font.BOLD,16));
  command.setSize(110,20);
  command.setLocation(380,55);
  command.addActionListener(this);
  c.add(command);

  JLabel lb2=new JLabel("接收到的信息");
  lb2.setFont(new Font("宋体",Font.BOLD,16));
  lb2.setForeground(Color.black);
  lb2.setSize(120,20);
  lb2.setLocation(10,85);
  c.add(lb2);

  tal=new JTextArea();
  tal.setForeground(Color.black);
  tal.setLocation(120,85);
  tal.setSize(250,200);
  c.add(tal);

  JLabel lb3=new JLabel("发送信息");
  lb3.setFont(new Font("宋体",Font.BOLD,16));
  lb3.setForeground(Color.black);
  lb3.setSize(120,25);
  lb3.setLocation(10,300);
  c.add(lb3);

  tf2=new JTextField();
  tf2.setForeground(Color.black);
  tf2.setSize(250,25);
  tf2.setLocation(120,300);
  c.add(tf2);
  command2=new JButton("发送信息");
  command2.setFont(new Font("宋体",Font.BOLD,16));
  command2.setSize(110,25);
  command2.setLocation(380,300);
  command2.addActionListener(this);
  command2.setEnabled(false);
  c.add(command2);

  JLabel lb4=new JLabel("发送信息");
  lb4.setFont(new Font("宋体",Font.BOLD,16));
  lb4.setForeground(Color.black);
  lb4.setSize(120,25);
  lb4.setLocation(180,27);
  c.add(lb4);

//  tf3=new JTextField("离线");
//  tf3.setForeground(Color.black);
//  tf3.setSize(120,25);
//  tf3.setLocation(270,27);
//  c.add(tf3);
  command3=new JButton("结束连接");
  command3.setFont(new Font("宋体",Font.BOLD,16));
  command3.setSize(110,20);
  command3.setLocation(380,85);
  command3.addActionListener(this);
  command3.setEnabled(false);
  c.add(command3);
  //建立server和client线程
  t1=new ml625_server();
  t2=new ml625_client();
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setSize(500,400);
  setVisible(true);
  }
  public void actionPerformed(ActionEvent e)
  {
   if(e.getSource()==command)
      {
       try{
           if(rb[0].isSelected()==true)
              {
               inbuf="";
               tf2.setText("");
               //启动server线程
               t1.start();
              }
            else
              {
               inbuf="";
               tf2.setText("");
               //启动client线程
               t2.start();
              }
           }
        catch(Exception e2)
           {
//            tf3.setText("发生错误");
           }
      }
   if(e.getSource()==command2)
      {
       //发送信息
       out1.write(tf2.getText()+"\n");
       out1.flush();
       tf2.setText("");
      }
   if(e.getSource()==command3)
      {
       try{
           if(rb[0].isSelected()==true)
              {
               //关闭server端socket
               insocket1.close();
//               tf3.setText("离线!");
               command2.setEnabled(false);
               command3.setEnabled(false);
               }
             else
               {
                //关闭client端socket
                socket2.close();
 //               tf3.setText("离线!");
                command2.setEnabled(false);
                command3.setEnabled(false);
               }
          }
        catch(Exception e2)
          {
//           tf3.setText("发生错误");
          }
      }
  }
  class ml625_server extends Thread
  {
   public ml625_server(){}
   public void run()
     {
      try
        {
         command.setEnabled(false);
//         tf3.setText("正在等待连接!");
         tf1.setText(InetAddress.getLocalHost().getHostAddress());
         socket1=new ServerSocket(4500);
         insocket1=socket1.accept();
         in1=new BufferedReader (new InputStreamReader(insocket1.getInputStream()));
         out1=new PrintWriter (insocket1.getOutputStream(),true);

         while(true)
         {
          if(socket1.isBound()==true)
            {
//             tf3.setText("正在连接!");
             command2.setEnabled(true);
             command3.setEnabled(true);
             break;
             }
         }
         while(true)
         {
          inbuf=in1.readLine();
          if(inbuf.length()>0)
            {
             tal.append(inbuf);
             tal.append("\n");
            }
         }
        }
        catch(Exception e){}
     }
  }
  class ml625_client extends Thread
  {
   public ml625_client(){}
   public void run()
     {
      try
        {
         command.setEnabled(false);
 //        tf3.setText("正在等待连接!");
         socket2=new Socket();
         socket2.connect(new InetSocketAddress(tf1.getText(),4500),5000);
         in1=new BufferedReader (new InputStreamReader(socket2.getInputStream()));
         out1=new PrintWriter (socket2.getOutputStream(),true);
         while(true)
           {
            if(socket2.isConnected()==true)
              {
 //              tf3.setText("正在连接!");
               command2.setEnabled(true);
               command3.setEnabled(true);
               tf2.setText("1234");
               break;
              }
           }
          inbuf="";
          while(true)
            {
             inbuf=in1.readLine();
             if(inbuf.length()>0)
               {
                tal.append(inbuf);
                tal.append("\n");
               }
            }
          }
        catch (Exception e) {}
     }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -