Results 1 to 7 of 7
Thread: sending file over network
- 07-07-2008, 07:43 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 2
- Rep Power
- 0
sending file 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 :(Im appreciate your help :D
- 07-10-2008, 03:29 AM #2
I'd suggest that you do a search for sample code on the various forums to see how its down.
You code and your comments don't agree.
The client won't receive by using a writeObject() method.
The server appears to be reading instead of sending
Your code would be easier to read if you put in the CODE tags. Put CODE inside of [ ] and end with [/ ]
- 07-10-2008, 07:30 AM #3
Start with what @norm said.
In general, over a network you send bytes, not objects.
And your OP says you want to send a file. In nearly all cases, a file is a stream of bytes. So sending a file is well suited to the usual pattern of sending bytes.
Do some more work, and ask again
- 07-25-2008, 05:01 PM #4
Member
- Join Date
- Jul 2008
- Posts
- 12
- Rep Power
- 0
Client should write on the server socket,but u didn't do that..
That is why server read nothingParimal
java heart
- 04-15-2009, 03:07 PM #5
Member
- Join Date
- Mar 2009
- Location
- US
- Posts
- 4
- Rep Power
- 0
Hi
Hi Guys,
Long time lurker thought i would eventually join up :) sorry if this is the wrong section mods!
Jen
- 04-24-2009, 10:46 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
Worked for me
- 04-25-2009, 01:55 AM #7
Stop raising zombies with your spam
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
Similar Threads
-
best Java Network API to use?
By San_Andreas in forum NetworkingReplies: 1Last Post: 04-30-2008, 08:42 PM -
Servlet sending the file (setting header)
By Java Tip in forum Java TipReplies: 0Last Post: 01-27-2008, 08:14 PM -
sending image file from JSP to Servlet
By ravian in forum Advanced JavaReplies: 2Last Post: 01-10-2008, 02:34 PM -
Problems sending file throught TCP sockets
By Nite in forum Advanced JavaReplies: 2Last Post: 08-04-2007, 09:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks