Skip to content

Commit

Permalink
android connection successfully tested
Browse files Browse the repository at this point in the history
version upgraded
  • Loading branch information
rafiulgits committed Jan 15, 2019
1 parent 4a0da11 commit f65d12c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 43 deletions.
2 changes: 1 addition & 1 deletion nbproject/private/private.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/panel/FileSender.java</file>
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/panel/AndroidPanel.java</file>
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/driver/Opening.java</file>
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/panel/AndroidP2P.java</file>
<file>file:/D:/Programming/My%20Program%20Code/Java%20Language/IP-Messenger/src/panel/FileReceiver.java</file>
</group>
</open-files>
Expand Down
2 changes: 2 additions & 0 deletions res/win/wifi_disconnect.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
netsh wlan disconnect
41 changes: 20 additions & 21 deletions src/driver/Opening.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
Expand All @@ -33,6 +34,7 @@
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import panel.AndroidPanel;
import panel.ChatPanel;

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

/**
* Label for IP and port.
*/
JLabel ipLabel = new JLabel("IP");
JLabel ipLabel = new JLabel();
try{
ipLabel.setText("Your Host: "+InetAddress.getLocalHost().getHostAddress());
} catch(UnknownHostException ex){

}
ipLabel.setFont(font);
ipLabel.setBounds(20, 200, 80, 40);
ipLabel.setBounds(10, 100, 325, 100);
ipLabel.setHorizontalAlignment(SwingConstants.CENTER);
o2oPanel.add(ipLabel);

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

/**
* Text Field for input IP and port.
* Text Field for input port
*/
JTextField ipField = new JTextField();
ipField.setText("localhost");
ipField.setBounds(105, 200, 180, 40);
ipField.setFont(font);
o2oPanel.add(ipField);

JTextField portField = new JTextField();
portField.setText("9876");
Expand All @@ -581,22 +581,21 @@ private void androidConnectOptions(){
/**
* This Button will for create a socket request or server socket request.
*/
JButton requst = new JButton("Request");
JButton requst = new JButton("Create Server");
requst.setFont(font);
requst.setBounds(100, 350, 150, 40);
Socket socket;
requst.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// try{
// int port = Integer.parseInt(portField.getText());
// Socket socket = new Socket(ipField.getText(), port);
//
// // new chat panel
// } catch(IOException ex){
// JOptionPane.showMessageDialog(null, ex);
// }
new AndroidPanel(frame);
try{
int port = Integer.parseInt(portField.getText().toString().trim());
ServerSocket ss = new ServerSocket(port);
Socket socket = ss.accept();
new AndroidPanel(frame, socket);
} catch(IOException ex){

}
}
});

Expand Down
13 changes: 11 additions & 2 deletions src/panel/AndroidP2P.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JLabel;
import javax.swing.filechooser.FileSystemView;

/**
Expand Down Expand Up @@ -53,6 +54,8 @@ public static abstract class Sender extends Thread implements FileProgressListen
public Sender(Socket socket, File file){
this.socket = socket;
this.file = file;

this.start();
}


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

private FileProgressListener fpl = this;
private Socket socket;
private JLabel label;

public Receiver(Socket socket){
public Receiver(Socket socket, JLabel label){
this.socket = socket;
this.label = label;

this.start();
}

@Override
Expand All @@ -123,6 +130,7 @@ public void run(){
if(type == RECEIVE_TYPE_FILE){
load = 0; readLen = 0;
fileName = dis.readUTF();
label.setText(fileName);
len = dis.readLong();

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

Expand Down
44 changes: 25 additions & 19 deletions src/panel/AndroidPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,12 @@ public class AndroidPanel extends JPanel{

private Socket socket;
private String name;
private DataInputStream dis;
private DataOutputStream dos;
private BufferedOutputStream bos;
private BufferedInputStream bis;


public AndroidPanel(JFrame frame){
public AndroidPanel(JFrame frame, Socket socket){

this.frame = frame;
this.socket = socket;

setBackground(Color.DARK_GRAY);
setLayout(null);
Expand All @@ -82,19 +79,22 @@ public AndroidPanel(JFrame frame){
pane.setLayout(null);
pane.add(this);
pane.repaint();
}

private void initStreams(){
try{
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
bis = new BufferedInputStream(socket.getInputStream());
bos = new BufferedOutputStream(socket.getOutputStream());
}catch(IOException ex){

new AndroidP2P.Receiver(socket, receivingFileLabel) {
@Override
public void onFileProgress(int progress) {
receivingProgress.setValue(progress);
}

}
@Override
public void onFileFinish(int state) {
if(state == DONE){
receivingProgress.setValue(receivingProgress.getMaximum());
}
}
};
}

private void setFileContent(){
Font f = new Font("arial", Font.BOLD, 15);
receivingFileLabel = new JLabel("No File Receiving");
Expand Down Expand Up @@ -195,16 +195,22 @@ public void actionPerformed(ActionEvent e) {
}

private void setCommandContent(){
try{
dos = new DataOutputStream(socket.getOutputStream());
} catch(IOException ex){

}

btRing = new JButton("Mobile in Ring Mode");
btRing.setBounds(50, 40, 250, 50);
btRing.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(fileSelected) return;
try{
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeShort(AndroidP2P.RECEIVE_TYPE_COMMAND);
dos.writeShort(AndroidP2P.RING_MODE);
dos.flush();
} catch(IOException ex){

}
Expand All @@ -218,9 +224,9 @@ public void actionPerformed(ActionEvent e) {
public void actionPerformed(ActionEvent e) {
if(fileSelected) return;
try{
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeShort(AndroidP2P.RECEIVE_TYPE_COMMAND);
dos.writeShort(AndroidP2P.SILENT_MODE);
dos.flush();
} catch(IOException ex){

}
Expand All @@ -234,9 +240,9 @@ public void actionPerformed(ActionEvent e) {
public void actionPerformed(ActionEvent e) {
if(fileSelected) return;
try{
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeShort(AndroidP2P.RECEIVE_TYPE_COMMAND);
dos.writeShort(AndroidP2P.SILENT_MODE);
dos.flush();
} catch(IOException ex){

}
Expand Down

0 comments on commit f65d12c

Please sign in to comment.