Who knows networking with Java

In summary: You'll need to use a ServerSocket on one computer to create a server which can be connected to by the other computer. The other computer will use a normal Socket.You will have to use FileInputStream and FileOutputStream to read and write the files from the disk.
  • #1
ramollari
437
1
Hi,
Does anyone know a way how to transfer a file from one computer to the other with Java language? Should I serialize it as an object?
 
Technology news on Phys.org
  • #2
Are u working on lan or internet?
If its on lan , it is easy and no i don't think u need to serialize it. Use ObjectInputStream and ObjectOutputStream which does the job for u.

-- AI
 
  • #3
Any Java object must be serialized in order to leave the virtual machine in which it resides. But, as Tenali mentions, it's not something you invoke explicitly.
 
  • #4
So you want to copy a file from your disk, across the network to another computer, and put the file on its disk?

You'll need to use a ServerSocket on one computer to create a server which can be connected to by the other computer. The other computer will use a normal Socket.

You will have to use FileInputStream and FileOutputStream to read and write the files from the disk.

This is a potentially complicated program, because you'll have to develop some kind of a protocol in which the computers can tell each other the names of the files, etc. If you want to be able to transfer files in both directions, you'll have to work out a protocol in which one computer can tell the other what to do, etc.

You might want to consider implementing a subset of the File Transfer Protocol, http://www.w3.org/Protocols/rfc959/. If you do this, you'll be able to test and use your program with a large variety of FTP clients already available for free.

- Warren
 
  • #5
No Warren I don't intend to build something as complicated as the FTP. I just thought I could transfer file objects with the ObjectOutputStream and ObjectInputStream from one machine to the other. Regarding the reading and writing of files I believe I can use the built in FileChooserDialog.
 
  • #6
ramollari said:
Regarding the reading and writing of files I believe I can use the built in FileChooserDialog.
Err he didnt mean that.
For example when u have transferred a file to the other side, say for example the client would like to store it with the same name as on the server side , then u would have to pass the filename to the other side (we had to do this when we had implemented a small teaching tool at our college).

-- AI
 
  • #7
ramollari said:
No Warren I don't intend to build something as complicated as the FTP. I just thought I could transfer file objects with the ObjectOutputStream and ObjectInputStream from one machine to the other. Regarding the reading and writing of files I believe I can use the built in FileChooserDialog.
This is not correct. You will need to involve actual networking classes, like Socket and ServerSocket, to accomplish moving files across a network.

- Warren
 
  • #8
If it is on the LAN, and you can mount network drives, they its as easy as writing the file to the directory.

If it is over the Internet however, you will need to user network sockets, and you will need a simple protocol. It shouldn't be too bad, however.
 
  • #9
What I mean is that I can define a much simpler protocol than the FTP, whithout the 'get', 'put', 'list', etc. etc. commands. Anyway I get your points.
 
  • #10
Actually another alternative is to use RMI, but that would probably more complicated than its worth.
 
  • #11
No, RMI doesn't work for file transfer.
 
  • #12
Found some code that might be useful as a starting point:

http://forums.devshed.com/archive/t-8632/file-transfer-in-servlets
 
  • #13
lyapunov said:
Found some code that might be useful as a starting point:

http://forums.devshed.com/archive/t-8632/file-transfer-in-servlets

Servlets require a webserver(or a webserver-lite like Tomcat), which is probably more than he needs.

ramollari said:
No, RMI doesn't work for file transfer.

Why couldn't you invoke a remote method on the host that will write out to a local file? I am not saying its the easiest way or anything(you have to set up an RMI registry on the host for one), but I don't see any reason why its not possible..
 

Related to Who knows networking with Java

1. What is networking with Java?

Networking with Java is the ability to communicate and exchange data between different devices or systems using the Java programming language. This includes establishing connections, sending and receiving data, and managing network resources.

2. Why is networking important in Java?

Networking is important in Java because it allows for the development of applications that can communicate with other devices or systems over a network. This enables the creation of more dynamic and interactive programs, such as web browsers, online games, and chat applications.

3. What are the key components of networking in Java?

The key components of networking in Java include the java.net package, which provides classes and interfaces for networking operations, and the java.io package, which allows for the reading and writing of data over a network. Other important components include sockets, URLs, and protocols such as TCP and UDP.

4. How do you establish a network connection in Java?

To establish a network connection in Java, you can use the Socket class from the java.net package. This class allows you to create a socket, which represents an endpoint for communication between two devices. You can then use methods such as connect() and getOutputStream() to establish a connection and send data.

5. What are some common challenges in networking with Java?

Some common challenges in networking with Java include handling errors and exceptions, managing resources efficiently, and dealing with different protocols and network configurations. It is important to have a good understanding of networking principles and to use proper error handling techniques to overcome these challenges.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
769
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
10
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
Back
Top