Results 1 to 3 of 3
Thread: Transferring Over Network
- 07-07-2008, 07:41 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 2
- Rep Power
- 0
Transferring Over Network
Hello every one
This is my first participation ,Im new in java , I have question , How I can send a file over network in a client- server paradigm , server send and client receive . the following is trying code
//The client code Client.java:
import java.net.*;
import java.io.*;
public class Client {
ObjectInputStream Sinput;
ObjectOutputStream Soutput;
Socket socket;
Client(int port) {
try {
socket = new Socket("localhost", port);
FileInputStream test=new FileInputStream("file name");
Soutput.writeObject(test);
Soutput.flush();
}
catch(Exception e) {
System.out.println("Error connectiong to server:" + e);
return;
}
try{
Sinput.close();
Soutput.close();
}
catch(Exception e) {}
}
public static void main(String[] arg) {
new Client(1500);
}
}
//The server code Server.java:
import java.io.*;
import java.net.*;
/
public class Server {
private ServerSocket serverSocket;
Server(int port) {
try
{
serverSocket = new ServerSocket(port);
System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());
while(true)
{
Socket socket = serverSocket.accept();
System.out.println("New client asked for a connection");
TcpThread t = new TcpThread(socket);
System.out.println("Starting a thread for a new Client");
t.start();
}
}
catch (IOException e) {
System.out.println("Exception on new ServerSocket: " + e);
}
}
public static void main(String[] arg) {
new Server(1500);
}
class TcpThread extends Thread implements Serializable {
Socket socket;
ObjectInputStream Sinput;
ObjectOutputStream Soutput;
TcpThread(Socket socket) {
this.socket = socket;
}
public void run() {
System.out.println("Thread trying to create Object Input/Output Streams");
try
{
Soutput = new ObjectOutputStream(socket.getOutputStream());
Soutput.flush();
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
System.out.println("Thread waiting for a String from the Client");
try {
byte[] msgArray=null;
File file = (File)Sinput.readObject();
FileInputStream fisSrc=new FileInputStream(file);
FileOutputStream fosDes=new FileOutputStream("ClientRceivedTempFile");
int n;
while ((n = fisSrc.available()) > 0) {
byte[] b = new byte[n];
int result = fisSrc.read(b);
if (result == -1) break;
fosDes.write( b );}
fisSrc.close();
fosDes.close();
}
catch (IOException e) {
System.out.println("Exception reading/writing Streams: " + e);
return;
}
catch (ClassNotFoundException o) {
}
finally {
try {
Soutput.close();
Sinput.close();
}
catch (Exception e) {
}
}
}
}
}
Im lost:o help me please :confused:Im appreciate your help :p
- 08-01-2008, 07:17 AM #2
Please note that the Tutorials section is currently off limits for questions specifically unrelated to the tutorial. I've taken the liberty of creating a thread in the proper place so your issue can be addressed.
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 08-01-2008, 01:40 PM #3
Member
- Join Date
- Jul 2008
- Posts
- 31
- Rep Power
- 0
You cannot send the FileInputStream object over the network you need to send the data that is in the file.
Look at it this way
Sending Side:
1)Open File for reading
2)Read a chunk of data from file
3)Write chunk of data to port
4)Repeat from 2 until whole file has been sent
5)Close file
Receiving Side:
1)Open file for writing
2)Read chunk of data from port
3)Write chunk of data to file
4)Repeat from 2 until all data has been received
5)Close file
Similar Threads
-
sending file over network
By qwerty in forum NetworkingReplies: 6Last Post: 04-25-2009, 01:55 AM -
database+network
By hidar in forum JDBCReplies: 4Last Post: 06-30-2008, 09:04 AM -
best Java Network API to use?
By San_Andreas in forum NetworkingReplies: 1Last Post: 04-30-2008, 08:42 PM -
How to get URL from network machine
By Mir in forum NetworkingReplies: 1Last Post: 04-02-2008, 12:08 AM -
Non Blocking Network
By mathias in forum NetworkingReplies: 1Last Post: 08-07-2007, 06:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks