473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

file transfer via socket

Hi
I am trying to create a server – client model where client send a file and when server receive the file send back an ack msg.
My code is:
[PHP]
import java.net.*;
import java.io.*;

class Client{
public static void main (String[] args){

DataInputStream input;

BufferedInputStream bis;
BufferedOutputStream bos;
int in;
byte[] byteArray;

try{
Socket client = new Socket("127.0.0.1", 8585);

input = new DataInputStream (client.getInputStream() );

System.out.println("Server message: " +input.readUTF() );

bis = new BufferedInputStream(new FileInputStream("encryptAtmMsg.txt"));
bos = new BufferedOutputStream(client.getOutputStream());
byteArray = new byte[8192];
while ((in = bis.read(byteArray)) != -1){
bos.write(byteArray,0,in);
}
bis.close();
bos.close();

System.out.println("Server message: " +input.readUTF() );
}
catch ( Exception e ) {
System.err.println(e);
}
}

}
[/PHP]
and
[PHP]
import java.net.*;
import java.io.*;

class Server{
public static void main (String[] args){

ServerSocket server;
Socket connection;

DataOutputStream output;

BufferedInputStream bis;
BufferedOutputStream bos;

byte[] receivedData;
int in;
try{
server = new ServerSocket( 8585 );
while ( true ) {
connection = server.accept();

output = new DataOutputStream (connection.getOutputStream() );

output.writeUTF( " ack 1" );

receivedData = new byte[1024];
bis = new BufferedInputStream(connection.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream("sss.txt"));
while ((in = bis.read(receivedData)) != -1){
bos.write(receivedData,0,in);
}
bos.close();
output.writeUTF( " ack 2" );
}
}
catch (IOException e ) {
System.err.println(e);
}
}
}
[/PHP]
I have this error: java.net.SocketException: socket closed
Any ideas pls...
Aug 14 '07 #1
3 10227
JosAH
11,448 Expert 8TB
I have this error: java.net.SocketException: socket closed
Any ideas pls...
On which side? The client or the server?

kind regards,

Jos
Aug 14 '07 #2
Client give this error
Aug 14 '07 #3
JosAH
11,448 Expert 8TB
Client give this error
Try flushing the buffered streams before you close anything. The rest of the logic
in your code seems ok.

kind regards,

Jos
Aug 14 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Sriram | last post by:
Hi, I have wriiten a program to transfer a binary file. After transfer. The file contents in the destination machine is getting changed. Please Help. Please advice if i am doing any...
4
by: Christian Galbavy | last post by:
Hello! My friend and I are working on a little program. It sends a local file to a server (php), we use the rfc1867 protocoll for this. When the server gets the data, it replies with some...
1
by: ADE | last post by:
Hi everyone well from my last post I found what I am looking for I have some code now that transfers files I have added a GUI to it and need some help with two things one my loadtemplate()...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
1
by: Nguyen Thanh Danh | last post by:
I'm a student, i try search on Internet but i can't find any sample how to send file throught socket, between 2 computer! Please help me! Thanks!
2
by: Satish | last post by:
Hello...plz provide sample code on heo top transfer multiple files over a Local network VIA TCP/IP in .net
4
by: pradqdo | last post by:
Hi folks, I have a very strange problem when I try to port my client/server program to cygwin. It is a simple shell program where the server executes client's commands + it can send and receive...
10
by: David | last post by:
I have googled to no avail on getting specifically what I'm looking for. I have found plenty of full blown apps that implement some type of file transfer but what I'm specifcally looking for is an...
5
by: raghubr | last post by:
Hi all, Can any one pls guide me through..I need to transfer the file from server to client and client to server using sockets in an Asynchronous mode so this file transfer doesn't hinder the...
1
by: diegoblin | last post by:
Hi, i kind of new to java and i want to transfer a file between a server and a client. I know i have to use InputStream and OutputStream, but i don't know how to do it properly. So far i think i've...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.