-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient_UDP.java
More file actions
173 lines (150 loc) · 6.57 KB
/
Client_UDP.java
File metadata and controls
173 lines (150 loc) · 6.57 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dos;
import com.sun.media.sound.DLSModulator;
import static dos.UDPClient.data;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
*
* @author rsrah
*/
public class Client_UDP extends javax.swing.JFrame {
Scanner sc = new Scanner(System.in);
DatagramSocket ds;
DatagramSocket ds1;
DatagramPacket DpReceive = null;
byte[] receive = new byte[65535];
InetAddress ip;
byte buf[] = null;
public Client_UDP() throws UnknownHostException, SocketException {
this.ip = InetAddress.getLocalHost();
this.ds1 = new DatagramSocket(1233);
this.ds = new DatagramSocket();
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
conversationWindow = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
sendClientText = new javax.swing.JTextField();
sendToServerBtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Client");
conversationWindow.setColumns(20);
conversationWindow.setRows(5);
jScrollPane1.setViewportView(conversationWindow);
jLabel1.setText("Client Message: ");
sendToServerBtn.setText("Send");
sendToServerBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendToServerBtnActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sendClientText, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(sendToServerBtn)
.addGap(21, 21, 21))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(sendClientText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sendToServerBtn))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void sendToServerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendToServerBtnActionPerformed
if (evt.getSource() == sendToServerBtn){
try{
String inp = sendClientText.getText();
sendClientText.setText("");
conversationWindow.append("Client:-" + inp + "\n");
if (inp.equals("bye")){
conversationWindow.append("Client sent bye.....EXITING\n");
setVisible(false); //you can't see me!
dispose(); //Destroy the JFrame object
}
//receive = new byte[65535];
buf = inp.getBytes();
DatagramPacket DpSend = new DatagramPacket(buf, buf.length, ip, 1234);
ds.send(DpSend);
}catch(IOException e){}
}
}//GEN-LAST:event_sendToServerBtnActionPerformed
public static StringBuilder data(byte[] a)
{
if (a == null)
return null;
StringBuilder ret = new StringBuilder();
int i = 0;
while (a[i] != 0)
{
ret.append((char) a[i]);
i++;
}
return ret;
}
public static void main(String args[]) throws IOException {
Client_UDP cliudp = new Client_UDP();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
cliudp.setVisible(true);
}
});
while (true) {
cliudp.DpReceive = new DatagramPacket(cliudp.receive, cliudp.receive.length);
cliudp.ds1.receive(cliudp.DpReceive);
if (data(cliudp.receive).toString().equals("bye")) {
cliudp.conversationWindow.append("Client sent bye.....EXITING\n");
//cliudp.setVisible(false); //you can't see me!
cliudp.dispose(); //Destroy the JFrame object
break;
}
else
cliudp.conversationWindow.append("Server:-" + data(cliudp.receive) + "\n");
cliudp.receive = new byte[65535];
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea conversationWindow;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField sendClientText;
private javax.swing.JButton sendToServerBtn;
// End of variables declaration//GEN-END:variables
}