Skip to content

Commit 4a0da11

Browse files
committed
added android connection
building connection protocol with android app P250
1 parent dc69dd9 commit 4a0da11

File tree

8 files changed

+509
-8
lines changed

8 files changed

+509
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
### Licensed under Apache License 2.0
1414

15-
Copyright 2018 Rafiul Islam
15+
Copyright 2019 Rafiul Islam
1616

1717
Licensed under the Apache License, Version 2.0 (the "License");
1818
you may not use this file except in compliance with the License.

nbproject/private/private.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
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/ChatPanel.java</file>
6+
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/panel/AndroidPanel.java</file>
7+
<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>
9+
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/panel/FileReceiver.java</file>
710
</group>
811
</open-files>
912
</project-private>

src/driver/Opening.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import javax.swing.JPanel;
3434
import javax.swing.JRadioButton;
3535
import javax.swing.JTextField;
36+
import panel.AndroidPanel;
3637
import panel.ChatPanel;
3738

3839
/**
@@ -125,6 +126,20 @@ public void actionPerformed(ActionEvent e) {
125126
}
126127
});
127128
contentPanel.add(manyToMany);
129+
130+
/**
131+
* Create a button on content panel for android-pc connection choose.
132+
*/
133+
JButton androidPC = new JButton();
134+
androidPC.setText("Android");
135+
androidPC.setBounds(100, 250, 150, 40);
136+
androidPC.addActionListener(new ActionListener() {
137+
@Override
138+
public void actionPerformed(ActionEvent e) {
139+
androidConnectOptions();
140+
}
141+
});
142+
contentPanel.add(androidPC);
128143
}
129144

130145
private void singleChatOptions(){
@@ -521,6 +536,82 @@ public void run(){
521536
repaint();
522537
}
523538

539+
private void androidConnectOptions(){
540+
/**
541+
* create a new panel that will show the one-one connection stuffs.
542+
*/
543+
JPanel o2oPanel = new JPanel();
544+
o2oPanel.setLayout(null);
545+
o2oPanel.setBackground(Color.LIGHT_GRAY);
546+
o2oPanel.setBounds(0, 100, 350, 420);
547+
548+
/**
549+
* This font will display on every label
550+
*/
551+
Font font = new Font("arial",Font.BOLD,15);
552+
553+
/**
554+
* Label for IP and port.
555+
*/
556+
JLabel ipLabel = new JLabel("IP");
557+
ipLabel.setFont(font);
558+
ipLabel.setBounds(20, 200, 80, 40);
559+
o2oPanel.add(ipLabel);
560+
561+
JLabel portLabel = new JLabel("PORT");
562+
portLabel.setFont(font);
563+
portLabel.setBounds(20, 250, 100, 40);
564+
o2oPanel.add(portLabel);
565+
566+
/**
567+
* Text Field for input IP and port.
568+
*/
569+
JTextField ipField = new JTextField();
570+
ipField.setText("localhost");
571+
ipField.setBounds(105, 200, 180, 40);
572+
ipField.setFont(font);
573+
o2oPanel.add(ipField);
574+
575+
JTextField portField = new JTextField();
576+
portField.setText("9876");
577+
portField.setBounds(105, 250, 60, 40);
578+
portField.setFont(font);
579+
o2oPanel.add(portField);
580+
581+
/**
582+
* This Button will for create a socket request or server socket request.
583+
*/
584+
JButton requst = new JButton("Request");
585+
requst.setFont(font);
586+
requst.setBounds(100, 350, 150, 40);
587+
Socket socket;
588+
requst.addActionListener(new ActionListener() {
589+
@Override
590+
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);
600+
}
601+
});
602+
603+
/**
604+
* add this one-one panel on main panel after remove the previous one and repaint
605+
* the main panel.
606+
*/
607+
o2oPanel.add(requst);
608+
609+
remove(contentPanel);
610+
add(o2oPanel);
611+
612+
repaint();
613+
}
614+
524615
private boolean isAValidName(String name){
525616
if(name.length() < 4){
526617
// name at least 4 length

src/panel/AndroidP2P.java

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright 2019 rafiul islam.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package panel;
17+
18+
import java.io.BufferedInputStream;
19+
import java.io.BufferedOutputStream;
20+
import java.io.DataInputStream;
21+
import java.io.DataOutputStream;
22+
import java.io.File;
23+
import java.io.FileInputStream;
24+
import java.io.FileOutputStream;
25+
import java.io.IOException;
26+
import java.net.Socket;
27+
import javax.swing.filechooser.FileSystemView;
28+
29+
/**
30+
*
31+
* @author rafiul islam
32+
*/
33+
public class AndroidP2P {
34+
public static final short RECEIVE_TYPE_FILE = 1;
35+
public static final short RECEIVE_TYPE_COMMAND = 2;
36+
37+
// mobile commands
38+
public static final short RING_MODE = 21;
39+
public static final short SILENT_MODE = 22;
40+
public static final short VIBRATE_MODE = 23;
41+
42+
// pc commands
43+
public static final short PC_SHUTDOWN = 51;
44+
45+
46+
public static abstract class Sender extends Thread implements FileProgressListener{
47+
48+
private FileProgressListener fpl = this;
49+
50+
private Socket socket;
51+
private File file = null;
52+
private short command = -1;
53+
public Sender(Socket socket, File file){
54+
this.socket = socket;
55+
this.file = file;
56+
}
57+
58+
59+
@Override
60+
public void run(){
61+
62+
try{
63+
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
64+
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
65+
66+
//send the protocol
67+
dos.writeShort(RECEIVE_TYPE_FILE);
68+
dos.writeUTF(file.getName());
69+
dos.writeLong(file.length());
70+
71+
FileInputStream fis = new FileInputStream(file);
72+
byte[] kb64 = new byte[1024*64];
73+
int readLen; long available = file.length();
74+
75+
while((readLen = fis.read(kb64,0,kb64.length))>0){
76+
available -= readLen;
77+
bos.write(kb64,0,readLen);
78+
fpl.onFileProgress(FileProgress.getProgress(file.length(),file.length()-available));
79+
if(available <=0){
80+
bos.flush();
81+
fis.close();
82+
break;
83+
}
84+
}
85+
fpl.onFileFinish(DONE);
86+
87+
88+
89+
} catch(IOException ex){
90+
91+
}
92+
}
93+
}
94+
95+
96+
public static abstract class Receiver extends Thread implements FileProgressListener{
97+
98+
private FileProgressListener fpl = this;
99+
private Socket socket;
100+
101+
public Receiver(Socket socket){
102+
this.socket = socket;
103+
}
104+
105+
@Override
106+
public void run(){
107+
String docDir = FileSystemView.getFileSystemView().getDefaultDirectory().getPath();
108+
File file = new File(docDir, "IP_Messenger");
109+
if(! file.exists()){
110+
file.mkdir();
111+
}
112+
try{
113+
114+
DataInputStream dis = new DataInputStream(socket.getInputStream());
115+
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
116+
117+
short type, command;
118+
String fileName;
119+
long len, load; int readLen;
120+
121+
while(socket!=null || !socket.isClosed()){
122+
type = dis.readShort();
123+
if(type == RECEIVE_TYPE_FILE){
124+
load = 0; readLen = 0;
125+
fileName = dis.readUTF();
126+
len = dis.readLong();
127+
128+
byte[] rawData = new byte[64*1024];
129+
file = new File(file.getAbsolutePath()+"\\"+fileName);
130+
FileOutputStream fos = new FileOutputStream(file);
131+
132+
while((readLen = bis.read(rawData,0,rawData.length))!=-1){
133+
load += readLen;
134+
fpl.onFileProgress(FileProgress.getProgress(len, load));
135+
fos.write(rawData,0, readLen);
136+
if(load == len){
137+
break;
138+
}
139+
}
140+
fos.flush();
141+
fos.close();
142+
fpl.onFileFinish(DONE);
143+
144+
}
145+
else if(type==RECEIVE_TYPE_COMMAND){
146+
command = dis.readShort();
147+
// work with this command
148+
}
149+
}
150+
151+
} catch(IOException ex){
152+
153+
}
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)