Repository navigation
Expand file tree
/
Copy pathRecieveConnection.java
More file actions
114 lines (88 loc) · 3.28 KB
/
Copy pathRecieveConnection.java
File metadata and controls
114 lines (88 loc) · 3.28 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
/*
* 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 sharefile;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import static sharefile.Send.din;
/**
*
* @author Muskan
*/
public class RecieveConnection extends Thread {
static Socket clientsocket;
DataOutputStream dout;
DataInputStream din;
String uname="abcd"; //yeh hatana h
File f;
public RecieveConnection(Socket s)
{
clientsocket = s;
}
public void run()
{
String str;
try{
din = new DataInputStream(clientsocket.getInputStream());
while(true){
if(din.available()>0) {
str=(String)din.readUTF();
break;
}
}
System.out.println(str);
if(str.compareTo("accept conn")==0){
dout = new DataOutputStream(clientsocket.getOutputStream());
dout.writeUTF(uname);
dout.flush();
sleep(5);
clientsocket.close();
}
else if(str.contains("recieve file")==true){
// else if(str.compareTo("recieve file")==0){
Thread t=new Thread(new ReceiveFile());
String s = str.substring(11);
f = new File("E:"+File.separator+"ShareFiles"+File.separator+s.substring(s.lastIndexOf(File.separator)+1));
System.out.println("download lcatin E:"+File.separator+"ShareFiles"+File.separator+s.substring(s.lastIndexOf(File.separator)+1));
//File f = new File("E://");
t.start();
}
}
catch(Exception e){
e.printStackTrace();
}
}
class ReceiveFile extends Thread{
public void run(){
FileOutputStream fout = null;
try{
//Receive file karne wala code
fout = new FileOutputStream(f);
byte[] bytes = new byte[16*1024];
int count;
try{
while ((count = din.read(bytes)) > 0) {
fout.write(bytes, 0, count);
}
fout.close();
System.out.println("recieved file");
//updating the database
//String query= ""
}
catch(Exception e){}
}
catch(FileNotFoundException ex){Logger.getLogger(RecieveConnection.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fout.close();
} catch (IOException ex) {
Logger.getLogger(RecieveConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}