-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendQuery.java
73 lines (67 loc) · 2.7 KB
/
sendQuery.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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
class sendQuery implements Runnable
{ /**********************/
int sockPort = 7005 ;
/*********************/
sendQuery()
{
// Red args if any
}
@Override
public void run()
{
try
{
//Creating a client socket to send query requests
Socket socketConnection = new Socket("localhost", sockPort) ;
// Files for input queries and responses
String inputfile = "./Input/" + Thread.currentThread().getName() + "_input.txt" ;
String outputfile = "./Output/" +Thread.currentThread().getName() + "_output.txt" ;
//-----Initialising the Input & ouput file-streams and buffers-------
OutputStreamWriter outputStream = new OutputStreamWriter(socketConnection
.getOutputStream());
BufferedWriter bufferedOutput = new BufferedWriter(outputStream);
InputStreamReader inputStream = new InputStreamReader(socketConnection
.getInputStream());
BufferedReader bufferedInput = new BufferedReader(inputStream);
PrintWriter printWriter = new PrintWriter(bufferedOutput,true);
File queries = new File(inputfile);
File output = new File(outputfile);
FileWriter filewriter = new FileWriter(output);
Scanner queryScanner = new Scanner(queries);
String query = "";
//--------------------------------------------------------------------
// Read input queries and write to the output stream
while(queryScanner.hasNextLine())
{
query = queryScanner.nextLine();
printWriter.println(query);
}
//System.out.println("Query sent from " + Thread.currentThread().getName());
// Get query responses from the input end of the socket of client
String result;
while( (result = bufferedInput.readLine()) != null)
{
filewriter.write(result + "\n");
}
// close the buffers and socket
filewriter.close();
queryScanner.close();
printWriter.close();
socketConnection.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}