Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 08-09-2007, 03:00 AM
Member
 
Join Date: Aug 2007
Posts: 2
spasavvas is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-13-2007, 07:49 PM
Member
 
Join Date: Aug 2007
Posts: 30
dmacvittie is on a distinguished road
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 -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-14-2007, 12:03 PM
Member
 
Join Date: Aug 2007
Posts: 2
spasavvas is on a distinguished road
send file via client - server model
when i do it the client didn't send the data

thanks
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-14-2007, 08:27 PM
Member
 
Join Date: Aug 2007
Posts: 30
dmacvittie is on a distinguished road
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 -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-29-2008, 10:36 PM
Member
 
Join Date: May 2008
Posts: 3
iliana is on a distinguished road
how to play an mp3 file?
Hello ! I have a problem ...Can anybody help me? 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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-29-2008, 10:37 PM
Member
 
Join Date: May 2008
Posts: 3
iliana is on a distinguished road
Sorry the right title was how to play an mp3 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
To transfer a file from client to server phani Networking 1 04-16-2008 02:39 AM
Copy a .swf file from server side to client using signed applet Imoracle Java Applets 0 01-28-2008 01:39 PM
send/read int in a client/server app dim_ath New To Java 2 01-03-2008 02:03 PM
how to send .jar files client to server gobinathm Networking 1 12-25-2007 05:05 AM
Server socket - send image to client Hinty Networking 0 11-30-2007 09:15 PM


All times are GMT +3. The time now is 12:21 AM.


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