P
Phillip D Ferguson
Hello,
i am relatively new to java, but i have a large amount of experience in C++
so the transition hasn't been too bad.
I am currently trying to build an application bit by bit so i am learning as
much as i can.
In the end i want an application that transfers XML files (via and HTTP
which will be good for debugging), but for the moment i am trying to build
one that just sends any file.
I have set up the appropriate code on each machine, sending a file from a
client to a server. However i am having problems with sockets.
I can send the file successfully but i would like the server to return some
acknowledgement, then i can figure out how to send another file etc.
I have tried sending a string but it doesn't work... can someone help?
Eventually i would like to send multiple files sequentially so i do need
some sort of acknowledgement even thought i am using tcp.
Im also having trouble with the naming of a file. If i send a file from the
client and want to save the file anywhere on the server, i need the file
name from the client. Can i extract it from the input stream or do i need to
request it separately?
My code for both sections is below:
Client code
-------------------------------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
public class TCPClientfile
{
public static void main(String[] args) throws Exception
{
Socket clientSocket = new Socket(host address and port);
OutputStream dataOutput = clientSocket.getOutputStream();
File inputFile = new File("readme_eclipse.html"); // example file to
send
FileInputStream in = new FileInputStream(inputFile);
byte[] buffer = new byte[2048];
int numread;
while ((numread = in.read(buffer))>=0)
{
dataOutput.write(buffer,0,numread);
System.out.println("sending..." +numread); // console confirmation
of transfer
}
// if i de-comment the next section things dont work
/* BufferedReader inFromServer =new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
*/
inFromServer.close();
in.close();
dataOutput.close();
clientSocket.close();
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Server code
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
public class TCPServerfile
{
public static void main(String[] args) throws Exception
{
ServerSocket welcomeSocket = new ServerSocket(port number);
System.out.println("listening");
Socket connectionSocket = welcomeSocket.accept();
System.out.println("socket accepted");
InputStream dataInput = connectionSocket.getInputStream();
File outputfile = new File ("readme_eclipse2.html"); // renamed
file from client, but how to get the original name?
FileOutputStream out = new FileOutputStream(outputfile);
byte[] buffer = new byte[2048];
int numbread;
while((numbread = dataInput.read(buffer))>=0)
{
out.write(buffer, 0, numbread);
System.out.println("Receiving..." + numbread);
}
// code will hang here if i try to send string as commented
below
/* DataOutputStream outToClient = new
DataOutputStream(connectionSocket.getOutputStream());
outToClient.writeBytes(Response);
System.out.println(Response);
*/
outToClient.close();
out.close();
dataInput.close();
connectionSocket.close();
welcomeSocket.close();
}
}
-------------------------------------------------------------------------------------------------------------------------------------
Can anyone offer me any corrections, advice.... good texts to read that
would help me at all?
Cheers!!
Phillip Ferguson
MEng student strathclyde university
i am relatively new to java, but i have a large amount of experience in C++
so the transition hasn't been too bad.
I am currently trying to build an application bit by bit so i am learning as
much as i can.
In the end i want an application that transfers XML files (via and HTTP
which will be good for debugging), but for the moment i am trying to build
one that just sends any file.
I have set up the appropriate code on each machine, sending a file from a
client to a server. However i am having problems with sockets.
I can send the file successfully but i would like the server to return some
acknowledgement, then i can figure out how to send another file etc.
I have tried sending a string but it doesn't work... can someone help?
Eventually i would like to send multiple files sequentially so i do need
some sort of acknowledgement even thought i am using tcp.
Im also having trouble with the naming of a file. If i send a file from the
client and want to save the file anywhere on the server, i need the file
name from the client. Can i extract it from the input stream or do i need to
request it separately?
My code for both sections is below:
Client code
-------------------------------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
public class TCPClientfile
{
public static void main(String[] args) throws Exception
{
Socket clientSocket = new Socket(host address and port);
OutputStream dataOutput = clientSocket.getOutputStream();
File inputFile = new File("readme_eclipse.html"); // example file to
send
FileInputStream in = new FileInputStream(inputFile);
byte[] buffer = new byte[2048];
int numread;
while ((numread = in.read(buffer))>=0)
{
dataOutput.write(buffer,0,numread);
System.out.println("sending..." +numread); // console confirmation
of transfer
}
// if i de-comment the next section things dont work
/* BufferedReader inFromServer =new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
*/
inFromServer.close();
in.close();
dataOutput.close();
clientSocket.close();
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Server code
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
public class TCPServerfile
{
public static void main(String[] args) throws Exception
{
ServerSocket welcomeSocket = new ServerSocket(port number);
System.out.println("listening");
Socket connectionSocket = welcomeSocket.accept();
System.out.println("socket accepted");
InputStream dataInput = connectionSocket.getInputStream();
File outputfile = new File ("readme_eclipse2.html"); // renamed
file from client, but how to get the original name?
FileOutputStream out = new FileOutputStream(outputfile);
byte[] buffer = new byte[2048];
int numbread;
while((numbread = dataInput.read(buffer))>=0)
{
out.write(buffer, 0, numbread);
System.out.println("Receiving..." + numbread);
}
// code will hang here if i try to send string as commented
below
/* DataOutputStream outToClient = new
DataOutputStream(connectionSocket.getOutputStream());
outToClient.writeBytes(Response);
System.out.println(Response);
*/
outToClient.close();
out.close();
dataInput.close();
connectionSocket.close();
welcomeSocket.close();
}
}
-------------------------------------------------------------------------------------------------------------------------------------
Can anyone offer me any corrections, advice.... good texts to read that
would help me at all?
Cheers!!
Phillip Ferguson
MEng student strathclyde university