📄 mf.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.lang.*;
import java.io.ByteArrayInputStream;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//接受短信息转换MessageContent = new String(bt,"ISO-10646-UCS-2").trim();
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class MF extends JFrame{
Socket socket=null;//全局套接字
BufferedReader in=null;//全局读
DataOutputStream out=null;//全局写
public MF() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
jTextArea1.setText("jTextArea1");
this.getContentPane().setLayout(xYLayout1);
jButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
jButton1.setText("开始服务器连接");
jButton1.addActionListener(new MF_jButton1_actionAdapter(this));
jButton2.setText("jButton2");
this.getContentPane().add(jTextArea1, new XYConstraints(0, 0, 400, 174));
this.getContentPane().add(jButton1, new XYConstraints(21, 216, 176, 32));
this.getContentPane().add(jButton2, new XYConstraints(245, 214, -1, -1));
xYLayout1.setWidth(400);
xYLayout1.setHeight(465);
}
JTextArea jTextArea1 = new JTextArea();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
public static void main(String[]args){
MF MFTest=new MF();
MFTest.setSize(300,300);
MFTest.setVisible(true);
}
public void jButton1_actionPerformed(ActionEvent e) {
try{
getContent(1, "test", "test", socket);
}catch(Exception exp){
this. jTextArea1.append(exp.toString()+"\n");
}
}
public byte[] getContent(int Type,String Username,String SpPassword,Socket socket) throws IOException
{ socket=new Socket("130.75.150.25",8801);
//消息头
byte[] bm = new byte[4];
byte[] bc = new byte[4];
byte[] bs1 = new byte[4];
byte[] bs2= new byte[4];
byte[] bs3 = new byte[4];
if (("1212".getBytes()).length == 4) {
System.out.println("Right");
}
if (("123411202032".getBytes()).length == 12) {
System.out.println("Right");
}
if (("test".getBytes()).length == 16) {
System.out.println("Right");
}
bm = IntToByte(1212); //Message Length 自己编的不知道对不对
bc = IntToByte(1234); //Command ID 自己编的不知道对不对
bs1 = IntToByte(1234); //Sequence Number 自己编的不知道对不对
bs2 = IntToByte(1120);
bs3 = IntToByte(2032);
//消息体
byte[] test = IntToByte(1); //登录类型。自定以方法
byte[] U = "test".getBytes(); //用户名
byte[] P = "test".getBytes(); //密码
byte[] zw = new byte[8]; //Reserve,保留扩展用
ByteArrayOutputStream bo = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bo);
//将消息头和消息体写入字节流--组合消息头和消息体
dout.write(bm);
dout.write(bc);
dout.write(bs1);
dout.write(bs2,0,4);
dout.write(bs3,0,4);
dout.write(test,0,4);
dout.write(U);
dout.write(P);
dout.write(zw);
byte[] bytes; //组合消息头和消息体后的命令
bytes = bo.toByteArray();
PrintStream PrintSocket = new PrintStream(socket.getOutputStream());
PrintSocket.write(bytes); //发送到服务器端
// PrintSocket.write('a');
PrintSocket.flush(); //请空缓存
// BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//System.out.println(in.readLine());
// PrintSocket.close();
int len=bytes.length;
for(int i=0;i<len;i++)
System.out.print(bytes[i]);
return bytes;
}
//将整数转化为网络字节顺序的byte数组
public static byte[] IntToByte(int data) {
byte[] bx = new byte[4];
try {
DataOutputStream fin;
ByteArrayOutputStream b = new ByteArrayOutputStream(4);
fin = new DataOutputStream(b);
fin.writeInt(data);
fin.flush();
fin.close();
fin = null;
bx = b.toByteArray();
if (bx.length!=4)
System.out.println("错误数组转换失败");
} catch (Exception e) {
System.out.println("将整数转化为网络字节顺序的byte数组时发生错误: " + e.toString());
}
System.out.print(data+"转化为网络字节顺序的byte数组后为:");
for(int i=0;i<4;i++)
System.out.print(bx[i]);
System.out.println();
return bx;
}
//将字节数组转化为整数
public static int byte4toInt(byte[] Byte4Size) {
//byte []byteInt=Byte4Size;//参数为本地数据时
//参数为网路数据时
byte[] byteInt = new byte[4];
for (int i = 0; i < 4; i++)
byteInt[i] = Byte4Size[3 - i];
int integer = 0;
for (int i = 0; i < 4; i++) {
int int_temp = 0;
int_temp = int_temp | ((int) byteInt[i] & 0xff);
integer = integer | (int_temp << (i * 8));
}
return integer;
}
}
class MF_jButton1_actionAdapter implements ActionListener {
private MF adaptee;
MF_jButton1_actionAdapter(MF adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -