-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathUDPSendText.java
More file actions
102 lines (86 loc) · 3.38 KB
/
Copy pathUDPSendText.java
File metadata and controls
102 lines (86 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
public class UDPSendText extends JFrame {
static public final long serialVersionUID = 1L;
InetSocketAddress destination_address;
static MessagePanel message;
public static void main( String[] args ) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new UDPSendText();
}
});
}
public UDPSendText() {
super("Send Text via UDP");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content = new JPanel( );
content.setLayout( new BoxLayout( content, BoxLayout.Y_AXIS) );
content.add( new SocketAddressPanel() );
content.add( new MessagePanel() );
this.setContentPane(content);
this.pack();
this.setVisible(true);
}
static class SocketAddressPanel extends JPanel {
public static final long serialVersionUID = 1L;
static public JTextField ip;
static private JTextField port;
static public InetSocketAddress getSocketAddress() {
return new InetSocketAddress(
ip.getText(),
Integer.parseInt( port.getText() )
);
}
public SocketAddressPanel() {
super( new FlowLayout(FlowLayout.LEFT, 5, 0) );
setBorder( BorderFactory.createTitledBorder("Internet Socket Address") );
add( new JLabel("IP:") );
ip = new JTextField("192.168.", 12);
add(ip);
add( new JLabel(" port:") );
port = new JTextField("65000",5);
add(port);
}
static public void transmit(String message) {
try{
DatagramSocket network = new DatagramSocket();
byte[] payload = message.getBytes();
DatagramPacket datagram = new DatagramPacket(
payload, payload.length,
getSocketAddress()
);
network.send(datagram);
}catch(Exception e){}
}
}
class MessagePanel extends JPanel implements ActionListener {
public static final long serialVersionUID = 1L;
JButton action;
JTextField message;
public MessagePanel() {
this.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
this.setBorder( BorderFactory.createTitledBorder("Message") );
message = new JTextField("Put some message here to send",32);
this.add(message);
message.addActionListener(this);
action = new JButton("send");
Dimension s = message.getPreferredSize();
Dimension b = action.getPreferredSize();
b.setSize( b.getWidth() , s.getHeight() );
action.setPreferredSize(b);
action.addActionListener(this);
this.add(action);
}
public void actionPerformed(ActionEvent e) {
try {
UDPSendText.SocketAddressPanel.transmit( message.getText() );
} catch (Exception exception ) {
}
}
}
}