-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaFileServer.java
95 lines (81 loc) · 3.64 KB
/
JavaFileServer.java
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
//package Server;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
import java.net.ServerSocket;
importjava.net.Socket;
importjava.util.Scanner;
class Server
{
public static void main(String args[])throws Exception{
String filename;
System.out.println (" ------------------------------------------------------------------------------------------------------------------------");
System.out.println (" FILE TRANSFER");
System.out.println (" ------------------------------------------------------------------------------------------------------------------------");
System.out.println (" This is the Server side of the program...");
System.out.println (" __________________________________________");
System.out.println(" |Enter the File Name: |");
System.out.print (" |");
Scanner sc=new Scanner(System.in);
filename=sc.nextLine();
System.out.println (" |__________________________________________|");
sc.close();
while(true)
{
//create server socket on port 5000
ServerSocketss=new ServerSocket(5000);
System.out.println (" |server is Waiting for request |");
System.out.println (" |__________________________________________|");
Socket s=ss.accept();
System.out.println (" |Connected With IP adress"+s.getInetAddress().toString());
System.out.println (" |__________________________________________|");
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStreamdout=new DataOutputStream(s.getOutputStream());
try
{
String str="";
str=din.readUTF();
if(!str.equals("stop"))
{
System.out.println(" |Sending File from Server : "+filename);
dout.writeUTF(filename);
dout.flush();
File f=new File(filename);
FileInputStream fin=new FileInputStream(f);
longsz=(int) f.length();
byte b[]=new byte [1024];
int read;
dout.writeUTF(Long.toString(sz));
dout.flush();
System.out.println (" |__________________________________________|");
System.out.println (" |Size of file: "+sz+" MB");
System.out.println (" |__________________________________________|");
System.out.println (" |Buffer size: "+ss.getReceiveBufferSize());
System.out.println (" |__________________________________________|");
while((read = fin.read(b)) != -1)
{
dout.write(b, 0, read);
dout.flush();
}
fin.close();
System.out.println(" |On the process... |");
dout.flush();
}
dout.writeUTF("stop");
System.out.println (" |__________________________________________|");
System.out.println(" |Sending file Completed |");
System.out.println (" |__________________________________________|");
dout.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("An error occured.....");
}
din.close();
s.close();
ss.close();
}
}
}