我正在尝试使用套接字但使用计时器发送byte []数组。 我想每1000毫秒发送一次byte []。与定时器一起,我有一个按钮,单击按钮时必须发送byte []。
当我运行我的代码时如果我点击按钮数据没有被发送如果我在我的代码中使用Timer并且只发送从定时器发送的但是没有收到。如果没有使用定时器,则按钮点击工作。
我的发送代码是
channel1=new JLabel("Channel 1");
channel1.setBounds(80, 140, 80, 30);
channel1.setFont(new Font("arial",Font.BOLD,15));
contentPane.add(channel1);
channel1Field=new JTextField();
channel1Field.setBounds(55, 200, 120, 30);
channel1Field.setEnabled(true);
contentPane.add(channel1Field);
channel1send=new JButton("Send 1");
channel1send.setBounds(65, 270, 100, 30);
channel1send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
if(!(channel1Field.getText().equals("")))
{
// TODO Auto-generated method stub
if(channel1send.isEnabled())
{
new Thread(new Channel1()).start();
}
}
else
{
JOptionPane optionPane = new JOptionPane("Field cannot be
empty", JOptionPane.ERROR_MESSAGE);
JDialog dialog = optionPane.createDialog("FAILURE");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
}
}
});
contentPane.add(channel1send);
new Thread(new timer()).start();
}
class timer implements Runnable
{
@Override
public void run()
{
// TODO Auto-generated method stub
Timer timer = new Timer();
timer.schedule(new SayHello(), 0, 4000);
}
}
class SayHello extends TimerTask {
public void run() {
// System.out.println("Hello World!");
try
{
byte[] com=new byte[]{0x01,(byte)0xFE};
socket=new Socket("192.168.1.115",8002);
DataOutputStream dw=new DataOutputStream(socket.getOutputStream());
dw.writeInt(com.length);
dw.write(com);
StringBuilder sb=new StringBuilder();
// System.out.println(Arrays.toString(com));
for(byte b:com)
{
sb.append(String.format("%02X ", b));
}
sentmessage.append("Timer Sent - " + sb.toString() + "\n");
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
if(socket==null)
{
try {
// socket.close();
socket=new Socket("192.168.1.115",8002);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println(e1.getMessage());
}
}
}
}
}
class Channel1 implements Runnable
{
@Override
public void run()
{
// TODO Auto-generated method stub
try {
String message1=channel1Field.getText();
int msg1=Integer.parseInt(message1);
command=new byte[]{(byte)0xFE,0x01,0x02,(byte)msg1,0x00,0x00,(byte)0xFD};
DataOutputStream dw=new DataOutputStream(socket.getOutputStream());
dw.writeInt(command.length);
dw.write(command);
StringBuilder sb=new StringBuilder();
// System.out.println(Arrays.toString(command));
for(byte b:command)
{
sb.append(String.format("%02X ", b));
}
sentmessage.append("Client Sent to Channel 2 - " + sb.toString() + "\n");
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
sentmessage.append("channel 2 on failed");
}
finally
{
if(socket==null)
{
try {
// socket.close();
socket=new Socket("192.168.1.115",8002);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println(e1.getMessage());
}
}
}
}
}
我的接收代码是
DataInputStream dw= new DataInputStream(s.getInputStream());
while(true)
{
int len = dw.readInt();
System.out.println(len);
byte[] bytes = new byte[len];
System.out.println(bytes);
dw.readFully(bytes);
System.out.println("Client sent: " + Arrays.toString(bytes));
StringBuilder sb=new StringBuilder();
for(byte b:bytes)
{
sb.append(String.format("%02X ", b));
}
text1.append("Server Received : " + sb.toString() + "\n");
}
}
catch(IOException e)
{
e.printStackTrace();
System.out.println("connection failed");
con.setText("CONNECTION FAILED");
}
}
我想接收定时器和按钮点击发送的数据。我将如何实现这一点。热烈帮助。我确实在互联网上获得了解决方案。