Network file transfer



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



.



Relevant Pages

  • return from server only works for built-in types
    ... But when I call _r.GetStatisticsthe client fails with the exception shown ... GetStatistics fails when client and server is on different networks. ... Win32 Version: 2.0.50727.832 ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: How do I stop a Winsock from buffering characters?
    ... Sorry all my server code did not make it in the post here is is. ... The fist character does not show up the rest do ... first character of the client send. ... public static void Main ...
    (microsoft.public.windowsce.embedded)
  • Re: How to detect if client or server are not responding
    ... you are asking if raising an exception could cause a COM call not ... responds to the server crashing. ... client implemented in an ActiveX plugin DLL running in a browser. ... If a COM service crashes in the middle of a call, it may take up to 6 ...
    (microsoft.public.win32.programmer.ole)
  • Re: JAX-WS: Wie Exceptions behandeln?
    ... SoapFault gemacht aber die Exception auch von "Java" ins Logfile ... Der Server oder der Client? ... Exceptions werden doch nur vom Server erzeugt. ...
    (de.comp.lang.java)
  • WCF communication error - whats the cause?
    ... I have a very simple client - server application using the WCF. ... Communications exception thrown whilst trying to write to remote service. ...
    (microsoft.public.dotnet.framework.webservices)