一个关于UTP协议的问题
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class MyQQ4
{
public static void main(String[] args)
{
SimpleFrame frame = new SimpleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
}
class SimpleFrame extends JFrame implements ActionListener
{
public static final int DEFAULT_WIDTH = 500;
public static final int DEFAULT_HEIGHT = 600;
public String name = null;
private MenuBar menuBar = new MenuBar();
private Menu Menu = new Menu("系统");
private MenuItem exit = new MenuItem("退出");
private MenuItem setItem = new MenuItem("设置");
private JFileChooser file = new JFileChooser(".");
MyPanel panel = new MyPanel();
MyDialog dialog = new MyDialog();
public SimpleFrame()
{
setTitle("MyQQ");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLocation(400, 20);
menuBar.add(Menu);
Menu.setFont(new Font("宋体", 1, 20));
Menu.add(setItem);
Menu.add(exit);
setMenuBar(menuBar);
setItem.addActionListener(this);
exit.addActionListener(this);
panel.sendMessage.addActionListener(this);
panel.sendFile.addActionListener(this);
panel.receiveFile.addActionListener(this);
add(panel);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == setItem)
{
dialog.setVisible(true);
} else if (e.getSource() == exit)
{
System.exit(0);
}
else if (e.getSource() == panel.sendMessage)
{
String nickname = null;
String aPieceOfMessage = null;
nickname = panel.text.getText();
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date nowdate = new Date();
String s = format.format(nowdate);
aPieceOfMessage = nickname + " " + "(" + s + ")" + ":" + "\n"
+ panel.Send.getText();
byte[] buf = aPieceOfMessage.trim().getBytes();
panel.Send.setText(null);
try {
InetAddress address = InetAddress.getByName(dialog.temp3);
DatagramPacket dp = new DatagramPacket(buf, buf.length,
address, dialog.targetPort);
DatagramSocket ds = new DatagramSocket();
ds.send(dp);
panel.Show.append(aPieceOfMessage + "\n");
} catch (Exception a)
{
a.printStackTrace();
}
}
else if (e.getSource() == panel.sendFile)
{
file.setFileSelectionMode(JFileChooser.FILES_ONLY);
file.setDialogTitle("请选择文件路径");
String path = null;
if (JFileChooser.APPROVE_OPTION == file.showOpenDialog(null))
{
path = file.getSelectedFile().getAbsolutePath();
File passFile = new File(path);
name = passFile.getName();
int length = (int) passFile.length();
//System.out.println(length);
byte[] buf = new byte[length];
if ((dialog.temp1.length() != 0)
&& (dialog.temp2.length() != 0)
&& (dialog.temp3.length() != 0)
&& (dialog.temp4.length() != 0)
&& (dialog.temp5.length() != 0))
{
try {
RandomAccessFile in=new RandomAccessFile(path,"r");
in.read(buf,0,length);
in.close();
InetAddress address = InetAddress
.getByName(dialog.temp3);
DatagramPacket dp = new DatagramPacket(buf, buf.length,
address, dialog.targetFilePort);
DatagramSocket ds = new DatagramSocket();
ds.send(dp);
ds.close();
if (path != null)
{
String nick = panel.text.getText();
JOptionPane.showMessageDialog(this,"发送成功!");
panel.receiveFile.setEnabled(true);
panel.Show.append("您的好友"+nick+"向您发送"+name + "\n");
} else {
}
} catch (Exception a)
{
a.printStackTrace();
}
}
}
}
else if (e.getSource() == panel.receiveFile)
{
try
{
File file = new File("D:\\",name);
RandomAccessFile toWrite = new RandomAccessFile(file, "rw");
toWrite.write(dialog.pass);
toWrite.close();
panel.receiveFile.setEnabled(false);
}
catch (IOException a)
{
a.printStackTrace();
}
}
}
}
class MyPanel extends JPanel
{
public static JTextArea Send = new JTextArea();
public static JTextArea Show = new JTextArea();
public static JTextField text = new JTextField(100);
public JLabel NickName = new JLabel("昵称:");
public JButton sendFile = new JButton("发送文件");
public JButton receiveFile = new JButton("接收文件");
public JButton sendMessage = new JButton("发送");
private JScrollPane ShowScrollPane = new JScrollPane(Show,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
private JScrollPane SendScrollPane = new JScrollPane(Send,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public MyPanel()
{
setLayout(null);
ShowScrollPane.setViewportView(Show);
NickName.setFont(new Font("宋体", 1, 20));
NickName.setBounds(10, 300, 100, 50);
sendFile.setBounds(30, 500, 100, 40);
receiveFile.setBounds(170, 500, 100, 40);
sendMessage.setBounds(350, 500, 100, 40);
text.setBounds(90, 300, 390, 40);
ShowScrollPane.setBounds(10, 70, 470, 230);
SendScrollPane.setBounds(10, 350, 470, 130);
//Show.setBorder(BorderFactory.createLoweredBevelBorder());
//Send.setBorder(BorderFactory.createLoweredBevelBorder());
//receiveFile.setEnabled(false);
add(text);
add(NickName);
add(sendFile);
add(receiveFile);
add(sendMessage);
add(ShowScrollPane);
add(SendScrollPane);
}
}
class MyDialog extends JDialog implements ActionListener
{
DialogPanel aPanel = new DialogPanel();
public DatagramSocket ds1 = null;
public DatagramPacket dp1= null;
public DatagramSocket ds2 = null;
public DatagramPacket dp2= null;
public String temp1 = null;
public String temp2 = null;
public String temp3 = null;
public String temp4 = null;
public String temp5 = null;
public int targetPort = 0;
public int receivePort = 0;
public int targetFilePort = 0;
public int receiveFilePort = 0;
byte[] pass = null ;
public MyDialog()
{
this.setLocation(300, 100);
this.setSize(400, 450);
this.setTitle("设置");
this.setResizable(false);
aPanel.Ok.addActionListener(this);
aPanel.Cancel.addActionListener(this);
add(aPanel);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == aPanel.Ok)
{
temp1 = aPanel.jf1.getText();
temp2 = aPanel.jf2.getText();
temp3 = aPanel.jf3.getText();
temp4 = aPanel.jf4.getText();
temp5 = aPanel.jf5.getText();
if ((temp1.length() != 0) && (temp2.length() != 0)
&& (temp3.length() != 0) && (temp4.length() != 0)
&& (temp5.length() != 0))
{
receivePort = Integer.parseInt(temp1);
receiveFilePort = Integer.parseInt(temp2);
targetPort = Integer.parseInt(temp4);
targetFilePort = Integer.parseInt(temp5);
try {
FileOutputStream fos = new FileOutputStream("setting.txt",
false);
PrintWriter pw = new PrintWriter(fos);
pw.println(temp1);
pw.println(temp2);
pw.println(temp3);
pw.println(temp4);
pw.println(temp5);
pw.close();
fos.close();
JOptionPane.showMessageDialog(this, "保存成功!");
this.setVisible(false);
} catch (IOException a)
{
a.printStackTrace();
}
Receive rec = new Receive();
rec.start();
ReceiveFile re = new ReceiveFile();
re.start();
}
else
{
JOptionPane.showMessageDialog(this, "信息不全!");
this.setVisible(true);
}
}
else if(e.getSource() == aPanel.Cancel)
{
this.setVisible(false);
}
}
class Receive extends Thread
{
public void run()
{
byte[] buf = new byte[1024];
try
{
dp1 = new DatagramPacket(buf, buf.length);
ds1 = new DatagramSocket(receivePort);
} catch (Exception a)
{
a.printStackTrace();
}
while (true)
{
try
{
ds1.receive(dp1);
int length = dp1.getLength();
//System.out.println(length);
//InetAddress address = dp1.getAddress();
//int port = dp.getPort();
String message = new String(dp1.getData(), 0, length);
MyPanel.Show.append(message + "\n");
} catch (Exception a)
{
a.printStackTrace();
}
}
}
}
class ReceiveFile extends Thread
{
public void run()
{
byte[] buf = new byte[10*1024*1024];
try
{
dp2 = new DatagramPacket(buf, buf.length);
ds2 = new DatagramSocket(receiveFilePort);
}
catch (Exception a)
{
a.printStackTrace();
}
while (true)
{
try
{
ds2.receive(dp2);
int length1 = dp2.getLength();
//System.out.println(length1);
pass = new byte[length1];
pass = dp2.getData();
}
catch (Exception a)
{
a.printStackTrace();
}
}
}
}
}
class DialogPanel extends JPanel
{
private JLabel messageRec = new JLabel("文本接收端口:");
public JTextField jf1 = new JTextField();
private JLabel fileRec = new JLabel("文件接收端口:");
public JTextField jf2 = new JTextField();
private JLabel addressTar = new JLabel("目标接收地址:");
public JTextField jf3 = new JTextField();
private JLabel messageTar = new JLabel("目标文本端口:");
public JTextField jf4 = new JTextField();
private JLabel fileTar = new JLabel("目标文件端口:");
public JTextField jf5 = new JTextField();
public JButton Ok = new JButton("确定");
public JButton Cancel = new JButton("取消");
public DialogPanel()
{
setLayout(null);
messageRec.setFont(new Font("宋体", 1, 20));
fileRec.setFont(new Font("宋体", 1, 20));
addressTar.setFont(new Font("宋体", 1, 20));
messageTar.setFont(new Font("宋体", 1, 20));
fileTar.setFont(new Font("宋体", 1, 20));
Ok.setFont(new Font("宋体", 1, 20));
Cancel.setFont(new Font("宋体", 1, 20));
messageRec.setBounds(10, 30, 140, 40);
jf1.setBounds(170, 30, 200, 40);
fileRec.setBounds(10, 90, 140, 40);
jf2.setBounds(170, 90, 200, 40);
addressTar.setBounds(10, 150, 140, 40);
jf3.setBounds(170, 150, 200, 40);
messageTar.setBounds(10, 210, 140, 40);
jf4.setBounds(170, 210, 200, 40);
fileTar.setBounds(10, 270, 140, 40);
jf5.setBounds(170, 270, 200, 40);
Ok.setBounds(80, 350, 100, 40);
Cancel.setBounds(200, 350, 100, 40);
add(messageRec);
add(jf1);
add(fileRec);
add(jf2);
add(addressTar);
add(jf3);
add(messageTar);
add(jf4);
add(fileTar);
add(jf5);
add(Ok);
add(Cancel);
}
}
//我这个代码,聊天还行,可就是不能传送文件,不知道是什么原因,运行时要打开两个界面,点击设置才能用
kk193
11 years, 9 months ago
Answers
首先纠正一点这个协议应该是UDP不是UTP,第二这种通信传输是以报文交换的方式传输的,也就是说不论你发消息还是传文件,接收或者发送的数据一定是byte[],所以传文件的话,就需要把文件的内容变成byte[]数组,同时需要注意,文件可能存在10GB大小的,所以算法上要修改。当然在传文件时还需要传递一些信息,如果你实在想不到有什么好办法包裹信息的话建议你用XML节点传递例如:
<File 携带的信息><!CDATA[--文件内容--]></File>
然后把该节点的所有内容变成String,以UTF-8格式编码成byte[]传递
麦田裸奔者
answered 11 years, 9 months ago