connection reset in socket file transfer - java

i just want to run a sample code for file transfer using sockets , in eclipse! and the client gives me this error:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at Client.main(Client.java:13)
i don't think that it is caused by the other side closing the socket!
here's the code of server and client:
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket servsock = new ServerSocket(1000);
File myFile = new File("s.pdf");
while (true) {
Socket sock = servsock.accept();
byte[] mybytearray = new byte[(int) myFile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
sock.close();
}
}
}
public class Client {
public static void main(String[] argv) throws Exception {
Socket sock = new Socket("127.0.0.1", 1000);
byte[] mybytearray = new byte[1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("s.pdf");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
sock.close();
}
}

Related

Java Connection Reset when sending .jpg or images over socket

I have no problems sending large text documents, or other files over sockets with my java program, but when I try to send a .jpg file or other image I get java.net.SocketException: Connection reset.
I have not run into any trouble when sending a 229 KB text document over the socket, but when I try to send the 89 KB image, I get the error. I use a while loop to read and write the file.
This is the part of the server class in question (it is named EasyDataSend):
public EasyDataSend() throws IOException
{
port = 8080;
server = new ServerSocket(port);
socket = server.accept();
dataOutputStream = new DataOutputStream(socket.getOutputStream());
}
public void sendFile(String path) throws IOException
{
File file = new File(path);
InputStream fileInputStream = new FileInputStream(file);
OutputStream fileOutputStream = socket.getOutputStream();
byte[] bytes = new byte[16 * 1024];
int count;
while ((count = fileInputStream.read(bytes)) > 0)
{
fileOutputStream.write(bytes, 0, count);
}
fileOutputStream.close();
fileInputStream.close();
socket.close();
server.close();
}
And this is the part of the client class (name EasyDataReceive):
public EasyDataReceive() throws UnknownHostException, IOException
{
ip = "127.0.0.1";
port = 8080;
socket = new Socket(ip,port);
}
public void receiveFile(String path) throws IOException, SocketException
{
File file = new File(path);
InputStream fileInputStream = socket.getInputStream();
OutputStream fileOutputStream = new FileOutputStream(file);
byte[] bytes = new byte[16*1024];
int count;
while ((count = fileInputStream.read(bytes)) > 0)
{
fileOutputStream.write(bytes, 0, count);
}
fileOutputStream.close();
fileInputStream.close();
socket.close();
}
This is the error I get:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at EasyDataReceive.receiveFile(EasyDataReceive.java:111)
at TesterClient.main(TesterClient.java:23)
Also, the line 111 is really just the start of the while loop in the client class. I did not want to paste the whole class into the post. The line 23 is just part of the testing class where I construct the EasyDataReceive object.
Your socket should be inside the methods: receiveFile() and sendFile().
For example,
public static void sendFile(String path) throws IOException {
try {
socket = new Socket("127.0.0.1", 8080);
System.out.println("Connected");
File file = new File(path);
InputStream fileInputStream = new FileInputStream(file);
OutputStream fileOutputStream = socket.getOutputStream();
byte[] bytes = new byte[16 * 1024];
int count;
while ((count = fileInputStream.read(bytes)) > 0) {
fileOutputStream.write(bytes, 0, count);
}
fileOutputStream.close();
fileInputStream.close();
} catch (UnknownHostException u) {
System.out.println(u);
} catch (IOException i) {
System.out.println(i);
} finally {
try {
socket.close();
} catch (IOException i) {
System.out.println(i);
}
}
}
Or you should pass the socket client to your method, i.e.
private void receiveFile (Socket client) {
...
}

Connection reset by peer: socket write error, when Transferring Large File

I'm making a program which transfers a file. In the process of this method, I'm getting the written error:
"Connection reset by peer: socket write error"
The file's size which I'm transferring is 96MB.
(clientSocket is SSLSocket)
Client:
Activity_1 Class:
clientSocket = (SSLSocket) sslContext.getSocketFactory().createSocket();
clientSocket.connect(...);
clientSocket.setEnabledCipherSuites(clientSocket.getSupportedCipherSuites());
clientSocket.startHandshake();
Activity_2 Class:
SSLSocket clientSocket = Activity_1.clientSocket;
final DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
int n = 0;
final byte[] buf = new byte[8192];
fos = new new FileOutputStream....
while ((n = dis.read(buf)) != -1) {
counter += n;
fos.write(buf, 0, n);
runOnUiThread(new Runnable() {
#Override
public void run() {
...
}
});
}
if (counter >= file_size) {
System.out.println("CLOSED");
fos.flush();
fos.close();
clientSocket.close();
}
Server:
FileInputStream fis = new FileInputStream...
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
byte[] mybytearray = new byte[8192];
OutputStream os;
try {
os = clientSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
int read = -1;
while ((read = dis.read(mybytearray)) > -1) {
dos.write(mybytearray, 0, read);
}
System.out.println("CLOSED");
dos.flush();
dos.close();
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
What could cause this problem to occur?
Thanks in advanced.

Image file not fully copied

I am using following program to get file from server:
public static void main(String[] argv) throws Exception {
Socket sock = new Socket("127.0.0.1", 12345);
byte[] mybytearray = new byte[1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("E://cy.jpg");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
sock.close();
}
This is creating the file but only a few contents of the image are being copied.
Can someone please explain what is wrong with this code and how to fix it?
You are reading only 1024 bytes from InputStream. You need wrap reading by while loop:
int bytesRead ;
while ((bytesRead = is.read(mybytearray)) != -1) {
bos.write(mybytearray, 0, bytesRead);
}
Try this one.
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
import org.apache.commons.io.IOUtils;
public class Test
{
public static void main(String[] argv) throws Exception {
Socket sock = new Socket("127.0.0.1", 12345);
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("E://cy.jpg");
IOUtils.copy(is, fos);
IOUtils.closeQuietly(fos);
sock.close();
}
}
This code uses IOUtils from Commons IO

java.net.ConnectException: Connection refused in TCP socket?

I have created a program to send file from client to server but when i am running that file in localhost it works good and i can send the file but when i am sending that file from another PC to my PC it shows "java.net.ConnectException: Connection refused".
Port number at both side same and firewall is also off. Even port is listening when client request to connect server socket is also accepting the socket.
Thank you in advance
Code of my program is :
public class ProgramSocket {
public ProgramSocket(ServerSocket servsock) throws IOException {
System.out.print("server started");
while (true) {
final Socket sock;
try {
sock = servsock.accept();
} catch (SocketException e) {
break;
}
new Thread(new Runnable() {
#Override
public void run() {
try {
connectToNewClient(sock);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
System.out.print("server stopped");
}
void connectToNewClient(Socket sock) throws IOException,
InterruptedException {
FileOutputStream fos = new FileOutputStream("C:/abc.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
sock.close();
}
}
Client side :
public String uploadFile(String filePath) throws Exception {
Socket sock = new Socket(ip, 24999);
// select file
File myFile = new File(filePath);
// send the program file
byte[] mybytearray = new byte[(int) myFile.length()];
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(myFile));
OutputStream os = sock.getOutputStream();
bis.read(mybytearray, 0, mybytearray.length);
os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
bis.close();
sock.close();
return message;
}
omitted some code for brevity.

File sending from multiple client to Single server in Java Socket Programming

hi i am trying to create a program in java for sending files from multiple client to single server. is this is possible?
i tried this could for multiple client and single server and also it is from server to client sending program.
Client Program
public class Client {
public Client(String ip,String path,int i) throws IOException {
int filesize=2022386;
int bytesRead;
int currentTot = 0;
File file=new File(path);
File dir=new File("c:/PAMR/Dest"+i+"/");
String name=file.getName();
if(!dir.exists())
{
dir.mkdir();
}
Socket socket = new Socket(ip,9100);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(dir+"/"+name);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(bytearray,0,bytearray.length);
currentTot = bytesRead;
do {
bytesRead =
is.read(bytearray, currentTot, (bytearray.length-currentTot));
if(bytesRead >= 0) currentTot += bytesRead;
} while(bytesRead > -1);
bos.write(bytearray, 0 , currentTot);
bos.flush();
bos.close();
socket.close();
}
}
for Server
public class Source {
public Source(String path) throws IOException {
ServerSocket serverSocket = new ServerSocket(9100);
System.out.println("Server is in Listening Mode");
Socket socket = serverSocket.accept();
File transferFile = new File (path);
byte [] bytearray = new byte [(int)transferFile.length()];
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray,0,bytearray.length);
OutputStream os = socket.getOutputStream();
os.write(bytearray,0,bytearray.length);
bin.close();
os.close();
os.flush();
socket.close();
serverSocket.close();
}
}
it is a part of code for my project i am doing.
Any body pls help.
You want something like this:
boolean working = true;
ServerSocket ss = new ServerSocket(9100);
while(working) {
Socket s = ss.accept();
MyThread myThread = new MyThread(s);
myTread.start();
}
where MyThread is a class of yours that extends the Thread class, accepts the connection to the client that just connected and then accepts any file or data sent to it.

Categories

Resources