473,757 Members | 10,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file transfer via socket

2 New Member
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;

BufferedInputSt ream bis;
BufferedOutputS tream bos;
int in;
byte[] byteArray;

try{
Socket client = new Socket("127.0.0 .1", 8585);

input = new DataInputStream (client.getInpu tStream() );

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

bis = new BufferedInputSt ream(new FileInputStream ("encryptAtmMsg .txt"));
bos = new BufferedOutputS tream(client.ge tOutputStream() );
byteArray = new byte[8192];
while ((in = bis.read(byteAr ray)) != -1){
bos.write(byteA rray,0,in);
}
bis.close();
bos.close();

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

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

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

ServerSocket server;
Socket connection;

DataOutputStrea m output;

BufferedInputSt ream bis;
BufferedOutputS tream bos;

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

output = new DataOutputStrea m (connection.get OutputStream() );

output.writeUTF ( " ack 1" );

receivedData = new byte[1024];
bis = new BufferedInputSt ream(connection .getInputStream ());
bos = new BufferedOutputS tream(new FileOutputStrea m("sss.txt")) ;
while ((in = bis.read(receiv edData)) != -1){
bos.write(recei vedData,0,in);
}
bos.close();
output.writeUTF ( " ack 2" );
}
}
catch (IOException e ) {
System.err.prin tln(e);
}
}
}
[/PHP]
I have this error: java.net.Socket Exception: socket closed
Any ideas pls...
Aug 14 '07 #1
3 10264
JosAH
11,448 Recognized Expert MVP
I have this error: java.net.Socket Exception: socket closed
Any ideas pls...
On which side? The client or the server?

kind regards,

Jos
Aug 14 '07 #2
spasavvas
2 New Member
Client give this error
Aug 14 '07 #3
JosAH
11,448 Recognized Expert MVP
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
2050
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 wrong. The number of bytes getting transfered
4
6475
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 details about the transaction. Here is an output-example: ******************************************** E:\Studium\Multi\Projekt>java transfer size: 225308 HTTP/1.1 200 OK
1
2698
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() function if I run it won't open the filetypes I just get the error message from tkMessageBox but its the right filetype and the final problem the Variable FILE how can I get it to interact with GUI once I select an image to send how can I use the...
11
6638
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 transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
1
5959
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
11193
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
5708
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 files (something like ftp server/client). I implemented all the commands which the server executes from scratch meaning I don't use fork and exec. It was very educational but my problem is that when I try to "get <filename>" from server to the...
10
10625
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 example to follow for using a tcp socket to transfer files between client/server, server/client. Both server and client are my program so I'm not looking for how to implement an FTP client, or how to download a file from a web server via http...
5
8240
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 common process of connecting and of the server and client... here is the code i am trying to implement. this code snippet is for message transfer only for multiple clients. /////////////////////////////file receive method when sent from...
1
21416
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 managed to do the client part. i read the content of a file, "hi.txt", and i send it to the server. The problem is in the server part, i do not know how to write the file in the server. Thanks in advance for your help Here is my code for the...
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.