Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-07-2008, 09:41 PM
Member
 
Join Date: Jul 2008
Posts: 2
qwerty is on a distinguished road
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 help me please Im appreciate your help
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-01-2008, 09:17 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-01-2008, 03:40 PM
Member
 
Join Date: Jul 2008
Posts: 31
skaspersen is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sending file over network qwerty Networking 3 07-25-2008 07:01 PM
database+network hidar Database 4 06-30-2008 11:04 AM
best Java Network API to use? San_Andreas Networking 1 04-30-2008 10:42 PM
How to get URL from network machine Mir Networking 1 04-02-2008 02:08 AM
Non Blocking Network mathias Networking 1 08-07-2007 08:49 AM


All times are GMT +3. The time now is 09:41 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org