-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
156 lines (141 loc) · 3.33 KB
/
Copy pathServer.java
File metadata and controls
156 lines (141 loc) · 3.33 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Server
{
public static final int Port = 2737;
public static void main(String[] args) throws Exception
{
Scanner in = new Scanner(System.in);
System.out.println("Input -1 to STOP SERVER");
while(true)
{
System.out.println("Enter Integer n and FileName");
int n;
String fileName;
try
{
n = in.nextInt();
if(n==-1)
break;
fileName = in.next();
}
catch(Exception e)
{
System.out.println("Invalid input format.");
in.nextLine();
continue;
}
File file = new File(fileName);
if(file.exists())
{
System.out.println("This file exists in the system. Choose a different Name.");
continue;
}
file.createNewFile();
Socket cs = null;
try
{
cs = new Socket("localhost",Port);
}
catch (Exception ex)
{
System.out.println("The connection to client could not be established.");
Thread.sleep(3000);
continue;
}
Thread new_thread = new generateRequest(cs,fileName,n);
new_thread.start();
try
{
new_thread.join();
}
catch (InterruptedException ex)
{
System.out.println("Interrupt occured while waiting for the thread");
}
}
}
}
//to generate request and receive file
class generateRequest extends Thread
{
private Socket sock;
private String fileName;
private int n;
public generateRequest(Socket sock,String file,int n)
{
this.sock = sock;
this.n = n;
this.fileName = file;
}
@Override
public void run()
{
String request = fileName+" "+n+"\n";
try
{
sock.getOutputStream().write(request.getBytes("UTF-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String response = reader.readLine();
System.out.println(response);
int count = 1;
if(response.startsWith("Ok"))
{
File file = new File(fileName);
FileOutputStream fis = new FileOutputStream(file);
int size = Integer.parseInt(reader.readLine());
System.out.println(size);
DataInputStream bis = new DataInputStream(sock.getInputStream());
while(count <= size)
{
byte data = bis.readByte();
fis.write(data);
count++;
}
fis.close();
}
else
{
System.out.println(response);
return ;
}
}
catch(Exception ex)
{
System.out.println(ex);
return ;
}
System.out.println("File recieved.");
handleAcknowledgment ack = new handleAcknowledgment(sock);
ack.start();
try
{
ack.join();
Thread.sleep(3000);
}
catch (InterruptedException ex)
{
System.out.println(ex);
}
}
}
// this thread handles the acknowledgment from the Client
class handleAcknowledgment extends Thread
{
private Socket sock;
public handleAcknowledgment(Socket sock)
{
this.sock = sock;
}
@Override
public void run()
{
String response = "SUCCESS\n";
try {
sock.getOutputStream().write(response.getBytes("UTF-8"));
} catch (IOException ex) {
System.out.println(ex);
}
}
}