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.
Related
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) {
...
}
I have completed client server program to download and upload a video(media) file, but I am unable to figure out how to calculate the upload, download time separately and make it run repeatedly to measure improvement in time? I will be running client at one IP and server at different IP.
Source code of the client;
package wdc;
import java.io.*;
import java.io.ByteArrayOutputStream;
import java.net.*;
class TCPClient {
public static void main(String args[]) {
byte[] aByte = new byte[1];
int bytesRead;
Socket clientSocket = null;
InputStream is = null;
try {
clientSocket = new Socket("127.0.0.1", 3248);
is = clientSocket.getInputStream();
} catch (IOException ex) {
// Do exception handling
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (is != null) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream("C:\\file1.avi");
bos = new BufferedOutputStream(fos);
bytesRead = is.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesRead = is.read(aByte);
} while (bytesRead != -1);
bos.write(baos.toByteArray());
bos.flush();
bos.close();
clientSocket.close();
} catch (IOException ex) {
// Do exception handling
}
}
}
}
And the source code of the server side;
package wds;
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String args[]) {
while (true) {
ServerSocket welcomeSocket = null;
Socket connectionSocket = null;
BufferedOutputStream outToClient = null;
try {
welcomeSocket = new ServerSocket(3248);
connectionSocket = welcomeSocket.accept();
outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
} catch (IOException ex) {
// Do exception handling
}
if (outToClient != null) {
File myFile = new File("C:\\file2.avi");
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(myFile);
} catch (FileNotFoundException ex) {
// Do exception handling
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(mybytearray, 0, mybytearray.length);
outToClient.write(mybytearray, 0, mybytearray.length);
outToClient.flush();
outToClient.close();
connectionSocket.close();
// File sent, exit the main method
return;
} catch (IOException ex) {
// Do exception handling
}
}
}
}
}
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.
I'm trying to send large files using socket programming in java for a p2p file sharing application. This code is sending 200-300 mb files without any problems, but for large files around 1 gb it is giving the error:-
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105)
at FileSender.main(FileSender.java:28)
I'm already sending file in small chunks as suggested in many answers I got. What should I do to send 1 gb files. I'm programming on Windows.
Here are my codes:
Server
public class FileSender {
public static void main(String...s)
{
BufferedOutputStream bos;
String file="D:\\filename.mp4";
try {
ServerSocket sock=new ServerSocket(12345);
while(true)
{
System.out.println("waiting");
Socket soc=sock.accept();
bos= new BufferedOutputStream(soc.getOutputStream());
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int n=-1;
byte[] buffer = new byte[8192];
while((n = bis.read(buffer))>-1)
{
bos.write(buffer,0,n);
System.out.println("bytes="+n);
bos.flush();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Reciever
public class FileReciev {
public static void main(String...s)
{
try
{
Socket sock=new Socket("127.0.0.1",12345);
File file=new File("D:\\newfilename.mp4");
BufferedInputStream bis=new BufferedInputStream(sock.getInputStream());
FileOutputStream fos = new FileOutputStream(file);
int n;
byte[] buffer = new byte[8192];
System.out.println("Connected");
while ((n = bis.read(buffer)) > -1) {
System.out.println("bytes="+n);
fos.write(buffer, 0, n);
if(n<(8192)){
fos.close();
bis.close();
break;
}
fos.flush();
}
System.out.println("recieved");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
if(n<(8192)){
fos.close();
bis.close();
What this means is if you ever get less than the number of bytes that you expected, you'll shut down your socket. There's no reason to do this.
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();
}
}