Results 1 to 16 of 16
- 08-09-2007, 02:00 AM #1
Member
- Join Date
- Aug 2007
- Posts
- 2
- Rep Power
- 0
send file via client - server model
Itry to send a file from client to server.
I do it, but when the server receive the file send an ack msg, then the client give error:
java.net.SocketException: socket closed
i don't know why.
my code is
client:
import java.net.*;
import java.io.*;
class Client{
Socket clientSocket;
byte[] byteArray;
BufferedInputStream bis;
BufferedOutputStream bos;
int in;
BufferedReader inm = null;
PrintWriter outm = null;
public Client(){
try{
clientSocket = new Socket("localhost", 9632);
System.out.println("i am client & connect");
inm = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outm = new PrintWriter(clientSocket.getOutputStream(), true);
System.out.println("------1--------" +clientSocket.isClosed());
outm.println("msg 1: hi");
System.out.println("from server__ " +inm.readLine());
outm.println("msg 2: now i will send you a file");
System.out.println("from server__ " +inm.readLine());
sendFile();
System.out.println("-------2-------" +clientSocket.isClosed());
System.out.println("from server__ " +inm.readLine());
System.out.println("-------3------nothing-" );
}
catch(IOException e){
e.printStackTrace();
}
}
public void sendFile(){
try{
bis = new BufferedInputStream(new FileInputStream("encryptAtmMsg.txt"));
bos = new BufferedOutputStream(clientSocket.getOutputStream( ));
byteArray = new byte[8192];
while ((in = bis.read(byteArray)) != -1){
bos.write(byteArray,0,in);
}
bis.close();
bos.close();
}
catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){
new Client();
}
}
server:
import java.net.*;
import java.io.*;
class Server{
BufferedInputStream bis;
BufferedOutputStream bos;
byte[] data;
Socket socket;
ServerSocket serverSocket;
int in;
BufferedReader inm = null;
PrintWriter outm = null;
public Server(){
try{
serverSocket = new ServerSocket(9632);
System.out.println("i am server & listening...");
socket = serverSocket.accept();
System.out.println("a client connect");
System.out.println("------1--------" +socket.isClosed());
inm = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outm = new PrintWriter(socket.getOutputStream(), true);
System.out.println("from client: " +inm.readLine());
outm.println("ack 1: hi....");
System.out.println("from client: " +inm.readLine());
outm.println("ack 2: ok....");
receiveFile();
System.out.println("------2--------" +socket.isClosed());
outm.println("ack 3: take the file");
}
catch (IOException e){
e.printStackTrace();
}
}
public void receiveFile(){
try{
byte[] receivedData = new byte[8192];
bis = new BufferedInputStream(socket.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream("encryptAtmMsg22.txt"));
while ((in = bis.read(receivedData)) != -1){
bos.write(receivedData,0,in);
}
bos.close();
}
catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){
new Server();
}
}
any idea pls
- 08-13-2007, 06:49 PM #2
Member
- Join Date
- Aug 2007
- Posts
- 30
- Rep Power
- 0
In the client:
bis.close();
bos.close();
bos.close() closes the underlying stream, not just the BufferedOutputStream. Comment that line out, and things should work for you.
Don.Don MacVittie F5 Networks - DevCentral
- 08-14-2007, 11:03 AM #3
Member
- Join Date
- Aug 2007
- Posts
- 2
- Rep Power
- 0
send file via client - server model
when i do it the client didn't send the data
thanks
- 08-14-2007, 07:27 PM #4
Member
- Join Date
- Aug 2007
- Posts
- 30
- Rep Power
- 0
Sorry, change it from bos.close() to bos.flush() and see if that takes care of it for you (you will eventually have to close bos though, or you'll slowly leak memory, but this will get you started to the solution).
Don.Don MacVittie F5 Networks - DevCentral
- 05-29-2008, 09:36 PM #5
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
how to play an mp3 file?
Hello ! I have a problem ...Can anybody help me?:confused: plz?
I want to play an mp3 file and the code in eclipse is something like the following ... I see the interface but it can't be played...
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class AudioFilee extends JFrame {
public AudioFilee() {
setTitle("Music File");
setSize(300, 400);
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JButton s= new JButton("STOP");
JButton p= new JButton("PLAY");
JLabel j1 = new JLabel("MusicFile");
String[] musicfiles={"workspace/eclipse/Audio/asdf.mp3","asd.mp3"};
JComboBox musiclist=new JComboBox(musicfiles);
musiclist.setSelectedIndex(1);
jp1.add(j1);
jp1.add(musiclist);
jp2.add(s);
jp2.add(p);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
true, jp1, jp2);
splitPane.setOneTouchExpandable(true);
getContentPane().add(splitPane);
}
public static void main(String[] args) {
AudioFilee sp = new AudioFilee();
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sp.setVisible(true);
}
}
Do you have any suggestion? Thanks
- 05-29-2008, 09:37 PM #6
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
Sorry the right title was how to play an mp3 file...
- 02-14-2009, 05:37 PM #7
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
How Send File Using Java Socket.
-in the site computersciencenotes.net section FAQ->Programming-> "send file with Java" explain how send a File using java socket.
- in section Download->Java->JavaProgramming there is a Project divided in 2 part: client and Server, that you can use for send a file.
Developed By: Enrico Chiacchiari and Paolo Loberto.
www[dot]computersciencenotes[dot]net[slash]index.php?option=com_phocadownload&view=category&i d=13:java-programming&Itemid=2
:)
- 02-14-2009, 10:21 PM #8
a post from the living dead
Zombie alert... the post your answering is over 8 months old.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-14-2009, 10:26 PM #9
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
- 02-14-2009, 10:42 PM #10
I'm sure that your intentions are good, you just have to make sure that your answering a current post... so that the suggestion has benefits.
huh... not sure what that meant... but welcome to the Java forums !!!ok... but now dont be shy
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
Have to second Cristobol's suggestion that we keep our posts relevant here.
- 12-01-2009, 01:48 PM #12
Member
- Join Date
- Dec 2009
- Location
- AddisAbaba
- Posts
- 10
- Rep Power
- 0
hey can some body help me? i was trying to up load file from my laptop which i used it as a client to my desktop which is used as a server and save this file to my server but i donn know the code for doing this. please get me out of this mess!
-
Belete, welcome to the forum, but I ask you to please ask a new question in a new thread. Thanks for your cooperation.
- 12-02-2009, 08:54 AM #14
Member
- Join Date
- Dec 2009
- Location
- AddisAbaba
- Posts
- 10
- Rep Power
- 0
can some body please me make me familiar with java.I tried to send file from client to server and more or less it worked but i want to save this file on my server, so please send me a sample code for doing so.
- 08-13-2010, 07:23 AM #15
Member
- Join Date
- Aug 2010
- Posts
- 1
- Rep Power
- 0
Hi spasavvas,
___________________ deleted by mod ____________________Last edited by Fubarable; 08-13-2010 at 11:44 AM.
-
RohitBhat: please read comments in the threads above before posting. If you have a question, start a new thread. Don't hijack an old one. Locking.
Similar Threads
-
To transfer a file from client to server
By phani in forum NetworkingReplies: 4Last Post: 10-12-2010, 06:15 PM -
Server socket - send image to client
By Hinty in forum NetworkingReplies: 2Last Post: 03-14-2009, 07:39 AM -
Copy a .swf file from server side to client using signed applet
By Imoracle in forum Java AppletsReplies: 2Last Post: 10-05-2008, 06:13 PM -
send/read int in a client/server app
By dim_ath in forum New To JavaReplies: 2Last Post: 01-03-2008, 01:03 PM -
how to send .jar files client to server
By gobinathm in forum NetworkingReplies: 1Last Post: 12-25-2007, 04:05 AM


LinkBack URL
About LinkBacks


Bookmarks