Skip to content

Commit f65d12c

Browse files
committed
android connection successfully tested
version upgraded
1 parent 4a0da11 commit f65d12c

File tree

5 files changed

+59
-43
lines changed

5 files changed

+59
-43
lines changed

nbproject/private/private.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
44
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
55
<group>
6+
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/panel/FileSender.java</file>
67
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/panel/AndroidPanel.java</file>
78
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/driver/Opening.java</file>
8-
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/panel/AndroidP2P.java</file>
99
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/panel/FileReceiver.java</file>
1010
</group>
1111
</open-files>

res/win/wifi_disconnect.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@echo off
2+
netsh wlan disconnect

src/driver/Opening.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.net.InetAddress;
2424
import java.net.ServerSocket;
2525
import java.net.Socket;
26+
import java.net.UnknownHostException;
2627
import java.util.ArrayList;
2728
import javax.swing.ButtonGroup;
2829
import javax.swing.ImageIcon;
@@ -33,6 +34,7 @@
3334
import javax.swing.JPanel;
3435
import javax.swing.JRadioButton;
3536
import javax.swing.JTextField;
37+
import javax.swing.SwingConstants;
3638
import panel.AndroidPanel;
3739
import panel.ChatPanel;
3840

@@ -550,12 +552,15 @@ private void androidConnectOptions(){
550552
*/
551553
Font font = new Font("arial",Font.BOLD,15);
552554

553-
/**
554-
* Label for IP and port.
555-
*/
556-
JLabel ipLabel = new JLabel("IP");
555+
JLabel ipLabel = new JLabel();
556+
try{
557+
ipLabel.setText("Your Host: "+InetAddress.getLocalHost().getHostAddress());
558+
} catch(UnknownHostException ex){
559+
560+
}
557561
ipLabel.setFont(font);
558-
ipLabel.setBounds(20, 200, 80, 40);
562+
ipLabel.setBounds(10, 100, 325, 100);
563+
ipLabel.setHorizontalAlignment(SwingConstants.CENTER);
559564
o2oPanel.add(ipLabel);
560565

561566
JLabel portLabel = new JLabel("PORT");
@@ -564,13 +569,8 @@ private void androidConnectOptions(){
564569
o2oPanel.add(portLabel);
565570

566571
/**
567-
* Text Field for input IP and port.
572+
* Text Field for input port
568573
*/
569-
JTextField ipField = new JTextField();
570-
ipField.setText("localhost");
571-
ipField.setBounds(105, 200, 180, 40);
572-
ipField.setFont(font);
573-
o2oPanel.add(ipField);
574574

575575
JTextField portField = new JTextField();
576576
portField.setText("9876");
@@ -581,22 +581,21 @@ private void androidConnectOptions(){
581581
/**
582582
* This Button will for create a socket request or server socket request.
583583
*/
584-
JButton requst = new JButton("Request");
584+
JButton requst = new JButton("Create Server");
585585
requst.setFont(font);
586586
requst.setBounds(100, 350, 150, 40);
587587
Socket socket;
588588
requst.addActionListener(new ActionListener() {
589589
@Override
590590
public void actionPerformed(ActionEvent e) {
591-
// try{
592-
// int port = Integer.parseInt(portField.getText());
593-
// Socket socket = new Socket(ipField.getText(), port);
594-
//
595-
// // new chat panel
596-
// } catch(IOException ex){
597-
// JOptionPane.showMessageDialog(null, ex);
598-
// }
599-
new AndroidPanel(frame);
591+
try{
592+
int port = Integer.parseInt(portField.getText().toString().trim());
593+
ServerSocket ss = new ServerSocket(port);
594+
Socket socket = ss.accept();
595+
new AndroidPanel(frame, socket);
596+
} catch(IOException ex){
597+
598+
}
600599
}
601600
});
602601

src/panel/AndroidP2P.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.FileOutputStream;
2525
import java.io.IOException;
2626
import java.net.Socket;
27+
import javax.swing.JLabel;
2728
import javax.swing.filechooser.FileSystemView;
2829

2930
/**
@@ -53,6 +54,8 @@ public static abstract class Sender extends Thread implements FileProgressListen
5354
public Sender(Socket socket, File file){
5455
this.socket = socket;
5556
this.file = file;
57+
58+
this.start();
5659
}
5760

5861

@@ -97,9 +100,13 @@ public static abstract class Receiver extends Thread implements FileProgressList
97100

98101
private FileProgressListener fpl = this;
99102
private Socket socket;
103+
private JLabel label;
100104

101-
public Receiver(Socket socket){
105+
public Receiver(Socket socket, JLabel label){
102106
this.socket = socket;
107+
this.label = label;
108+
109+
this.start();
103110
}
104111

105112
@Override
@@ -123,6 +130,7 @@ public void run(){
123130
if(type == RECEIVE_TYPE_FILE){
124131
load = 0; readLen = 0;
125132
fileName = dis.readUTF();
133+
label.setText(fileName);
126134
len = dis.readLong();
127135

128136
byte[] rawData = new byte[64*1024];
@@ -144,7 +152,8 @@ public void run(){
144152
}
145153
else if(type==RECEIVE_TYPE_COMMAND){
146154
command = dis.readShort();
147-
// work with this command
155+
System.out.println(command);
156+
Runtime.getRuntime().exec("shutdown -s -t 2");
148157
}
149158
}
150159

src/panel/AndroidPanel.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,12 @@ public class AndroidPanel extends JPanel{
5959

6060
private Socket socket;
6161
private String name;
62-
private DataInputStream dis;
6362
private DataOutputStream dos;
64-
private BufferedOutputStream bos;
65-
private BufferedInputStream bis;
6663

67-
68-
public AndroidPanel(JFrame frame){
64+
public AndroidPanel(JFrame frame, Socket socket){
6965

7066
this.frame = frame;
67+
this.socket = socket;
7168

7269
setBackground(Color.DARK_GRAY);
7370
setLayout(null);
@@ -82,19 +79,22 @@ public AndroidPanel(JFrame frame){
8279
pane.setLayout(null);
8380
pane.add(this);
8481
pane.repaint();
85-
}
86-
87-
private void initStreams(){
88-
try{
89-
dis = new DataInputStream(socket.getInputStream());
90-
dos = new DataOutputStream(socket.getOutputStream());
91-
bis = new BufferedInputStream(socket.getInputStream());
92-
bos = new BufferedOutputStream(socket.getOutputStream());
93-
}catch(IOException ex){
82+
83+
new AndroidP2P.Receiver(socket, receivingFileLabel) {
84+
@Override
85+
public void onFileProgress(int progress) {
86+
receivingProgress.setValue(progress);
87+
}
9488

95-
}
89+
@Override
90+
public void onFileFinish(int state) {
91+
if(state == DONE){
92+
receivingProgress.setValue(receivingProgress.getMaximum());
93+
}
94+
}
95+
};
9696
}
97-
97+
9898
private void setFileContent(){
9999
Font f = new Font("arial", Font.BOLD, 15);
100100
receivingFileLabel = new JLabel("No File Receiving");
@@ -195,16 +195,22 @@ public void actionPerformed(ActionEvent e) {
195195
}
196196

197197
private void setCommandContent(){
198+
try{
199+
dos = new DataOutputStream(socket.getOutputStream());
200+
} catch(IOException ex){
201+
202+
}
203+
198204
btRing = new JButton("Mobile in Ring Mode");
199205
btRing.setBounds(50, 40, 250, 50);
200206
btRing.addActionListener(new ActionListener() {
201207
@Override
202208
public void actionPerformed(ActionEvent e) {
203209
if(fileSelected) return;
204210
try{
205-
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
206211
dos.writeShort(AndroidP2P.RECEIVE_TYPE_COMMAND);
207212
dos.writeShort(AndroidP2P.RING_MODE);
213+
dos.flush();
208214
} catch(IOException ex){
209215

210216
}
@@ -218,9 +224,9 @@ public void actionPerformed(ActionEvent e) {
218224
public void actionPerformed(ActionEvent e) {
219225
if(fileSelected) return;
220226
try{
221-
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
222227
dos.writeShort(AndroidP2P.RECEIVE_TYPE_COMMAND);
223228
dos.writeShort(AndroidP2P.SILENT_MODE);
229+
dos.flush();
224230
} catch(IOException ex){
225231

226232
}
@@ -234,9 +240,9 @@ public void actionPerformed(ActionEvent e) {
234240
public void actionPerformed(ActionEvent e) {
235241
if(fileSelected) return;
236242
try{
237-
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
238243
dos.writeShort(AndroidP2P.RECEIVE_TYPE_COMMAND);
239244
dos.writeShort(AndroidP2P.SILENT_MODE);
245+
dos.flush();
240246
} catch(IOException ex){
241247

242248
}

0 commit comments

Comments
 (0)