Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have code for source and client it works well in same machine but i want to do communication between two machines so i just want to know , is my code work for this task? and please go through code once, if any part of code you want to change to perform this task please do. thanks in advance

client program

Java
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
 
 class Main {
 
    public static void main(String[] args)
    {
        try
        {
        Socket sock = new Socket("127.0.0.1",8089);      
        byte[] mybytearray = new byte[1024];
        InputStream is =sock.getInputStream();
        String filepath = "F:\\"+"p"+".csv";
        FileOutputStream fos = new FileOutputStream(filepath);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int byteRead = is.read(mybytearray , 0 , mybytearray.length);
        bos.write(mybytearray,0,byteRead);
        System.out.println("File sent to server");
        bos.close();
        sock.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
     }
}


server program

Java
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
class Main
{
    public static void main(String[] args)
    {
        try
        {
        ServerSocket s = new ServerSocket(8089);
        String filepath = "E:\\"+"p"+".csv";
        File myFile = new File(filepath);
 
        while (true)
        {
            Socket sock = s.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);
            System.out.println("file transfferd");
            os.flush();
            sock.close();
        }
        }
 
        catch(Exception e)
        {
            System.out.println(e);
        }
         }
      }
Posted
Comments
CPallini 12-May-15 3:13am    
Before asking here, I would actually try the code on a actual network.
Member 11543226 12-May-15 3:27am    
is it working or not? i want to use this for my product barcode scanner which has ipaddress.
Richard MacCutchan 12-May-15 3:37am    
Install the server and client on different machines, with the appropriate IP address. Run some tests and see what happens.
Mohibur Rashid 12-May-15 3:56am    
The Op is asking if provided code works or not. It's not even a question.
Suggestion to the OP. First try yourself then ask where are you failing. Don't ask public to study your code and tell you whether it will work or not.
Richard MacCutchan 12-May-15 4:11am    
Exactly so, hence my suggestion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900