I am developing an application that transfer files amoung peers, client/server , using socket. The requerement for the project is to send request to other peer using xml file. Several things are happening that's driving me crazy. Here are the scenerios:
1. When I connect to my own peer (setup my program on two different machines) everything is good
a. I able connect
b. It takes less than a second to parse the requested
c. receive files
d.download files.
2. When my classmate try to connect to my peer
a. they are able to connect
c. it takes between 1 to 2 minutes to parse their request
b. I can't upload songs to their machine
d. I can download file from their machine.
3. When the other group connect amoung themselves
Everthing works fine.
So, something must be wrong with my codes. I started debugging and did a couple printtrace, I find the error. Here is my code and the print stack trace error. Please help.
Function1 : After receiving a connection, I get the Input Stream, dump it to a file, then I parse the file ...
try {
InputStream in = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
if (!flagCache) {
Date now = new Date();
String pattern = "yyyyMMdd'T'hhmmssSSSZ";
SimpleDateFormat formatDate= new SimpleDateFormat(pattern, Locale.US);
String time = formatDate.format(now);
dump = new File(".\\log\\" + time + ".xml"
if (!dump.exists())
dump.createNewFile();
}
BufferedWriter tempwriter = new BufferedWriter(new FileWriter(dump));
PrintWriter wr = new PrintWriter(tempwriter,true);
String reply;
do {
reply = reader.readLine();//.read();
if (reply != null)
wr.write(reply,0,reply.length());
}
while (reply != null);
//tempwriter.flush();
wr.close();
}
catch (IOException e ) {
outputMessage.append("\nError occur while initialing the parser"
}
Function 2: Parsing the file using Dom document
try {
setInputSource();
//input = new InputSource(socket.getInputStream());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document;
if (dump.isFile() ){
document = docBuilder.parse(dump);
outputMessage.append("\nFinish Parsing the xml query"
}
catch (SAXException e) { e.printstrace()); }
After parsing, i retreive the file requested, and I try to send the song ... this where the problem coming from.
Funciton 3:
try {
File requestedFile = new File(directory + File.separatorChar + track);
BufferedOutputStream output = new BufferedOutputStream(socket.getOutputStream());
FileInputStream fin = new FileInputStream(requestedFile);
System.err.println(requestedFile.getName());
int bytesRead = -1;
byte[] streamData = new byte[8000];
while ((bytesRead = fin.read(streamData, 0, 8000)) != -1) {
output.write(streamData, 0, bytesRead);
sleep(1);
}
output.flush();
output.close();
}
catch (InterruptedException z ) {
System.err.println("Error sleeping"
}
catch (IOException e ) {
e.printStackTrace();
}
This is the error from printstackTrace :
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
1. When I connect to my own peer (setup my program on two different machines) everything is good
a. I able connect
b. It takes less than a second to parse the requested
c. receive files
d.download files.
2. When my classmate try to connect to my peer
a. they are able to connect
c. it takes between 1 to 2 minutes to parse their request
b. I can't upload songs to their machine
d. I can download file from their machine.
3. When the other group connect amoung themselves
Everthing works fine.
So, something must be wrong with my codes. I started debugging and did a couple printtrace, I find the error. Here is my code and the print stack trace error. Please help.
Function1 : After receiving a connection, I get the Input Stream, dump it to a file, then I parse the file ...
try {
InputStream in = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
if (!flagCache) {
Date now = new Date();
String pattern = "yyyyMMdd'T'hhmmssSSSZ";
SimpleDateFormat formatDate= new SimpleDateFormat(pattern, Locale.US);
String time = formatDate.format(now);
dump = new File(".\\log\\" + time + ".xml"
if (!dump.exists())
dump.createNewFile();
}
BufferedWriter tempwriter = new BufferedWriter(new FileWriter(dump));
PrintWriter wr = new PrintWriter(tempwriter,true);
String reply;
do {
reply = reader.readLine();//.read();
if (reply != null)
wr.write(reply,0,reply.length());
}
while (reply != null);
//tempwriter.flush();
wr.close();
}
catch (IOException e ) {
outputMessage.append("\nError occur while initialing the parser"
}
Function 2: Parsing the file using Dom document
try {
setInputSource();
//input = new InputSource(socket.getInputStream());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document;
if (dump.isFile() ){
document = docBuilder.parse(dump);
outputMessage.append("\nFinish Parsing the xml query"
}
catch (SAXException e) { e.printstrace()); }
After parsing, i retreive the file requested, and I try to send the song ... this where the problem coming from.
Funciton 3:
try {
File requestedFile = new File(directory + File.separatorChar + track);
BufferedOutputStream output = new BufferedOutputStream(socket.getOutputStream());
FileInputStream fin = new FileInputStream(requestedFile);
System.err.println(requestedFile.getName());
int bytesRead = -1;
byte[] streamData = new byte[8000];
while ((bytesRead = fin.read(streamData, 0, 8000)) != -1) {
output.write(streamData, 0, bytesRead);
sleep(1);
}
output.flush();
output.close();
}
catch (InterruptedException z ) {
System.err.println("Error sleeping"
}
catch (IOException e ) {
e.printStackTrace();
}
This is the error from printstackTrace :
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)