Page 3 of 3 FirstFirst 123
Results 41 to 60 of 60
Like Tree1Likes

Thread: A simple connection between 2 computers

  1. #41
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    Then how the txt files has the same info in it?
    So what you reccomand to do?

    edit: here is the code if you want to test it,

    Client:
    Java Code:
    public class Client
    {
    	public static void main(String[]args) throws Exception
    	{
    		Socket socket = null;
    		try{
    			socket = new Socket("127.0.0.1",2000);		
    		}
    		catch (Exception e) {
    			System.out.println(e);
    		}
    		
    
    		DataInputStream dis = new DataInputStream(socket.getInputStream());
    		byte[]ar = new byte[dis.readInt()];
    		while(dis.read(ar) != -1);
    
    		FileOutputStream fos = new FileOutputStream(new File("tictactoe2.png"));
    		fos.write(ar);
    		fos.close();
    		
    		dis.close();
    		socket.close();
    	}
    	
    }
    Server:
    Java Code:
    public static void main (String[]args)throws IOException 
    	{
    		ServerSocket serverSocket = null;
    		Socket clientSocket = null;
    		ObjectOutputStream out = null;
    		PrintWriter pw;
    		
    		try
    		{
    			serverSocket = new ServerSocket(2000);
    			clientSocket = serverSocket.accept();
    		}
    		catch (Exception e) {
    			System.out.println(e);
    		}
    		DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
    		byte[]ar = getBytesFromFile(new File("pic.png"));
    		dos.writeInt(ar.length);
    		dos.write(ar);
    		dos.close();
    
    		
    	}
    getBytesFromFile:
    Java Code:
    public static byte[] getBytesFromFile(File file) throws IOException 
    	{
    	    InputStream is = new FileInputStream(file);
    
    	    // Get the size of the file
    	    long length = file.length();
    
    	    // You cannot create an array using a long type.
    	    // It needs to be an int type.
    	    // Before converting to an int type, check
    	    // to ensure that file is not larger than Integer.MAX_VALUE.
    	    if (length > Integer.MAX_VALUE) {
    	        // File is too large
    	    }
    	    
    	    // Create the byte array to hold the data
    	    byte[] bytes = new byte[(int)length];
    
    	    // Read in the bytes
    	    int offset = 0;
    	    int numRead = 0;
    	    while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) 
    	    {
    	        offset += numRead;
    	    }
    
    	    // Ensure all the bytes have been read in
    	    if (offset < bytes.length) {
    	        throw new IOException("Could not completely read file "+file.getName());
    	    }
    
    	    // Close the input stream and return bytes
    	    is.close();
    	    return bytes;
    	}
    Last edited by tnrh1; 01-28-2012 at 05:57 PM.

  2. #42
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    txt files contain char values. Your classes and methods work with char data.

    You need to use classes and methods that work with bytes.

    Did you try File... Stream and Data...Stream for example?

  3. #43
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    But the write method of the data output stream should work with bytes "Writes b.length bytes to this output stream. "
    Then there must be something wrong in the way I send the data.
    When I printed the first picture byte and the new picture bytes only the first half was the same and then the rest was zeros.

  4. #44
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    Can you compare the input file to the output file, byte by byte to see where the difference is?
    You can write a very simple compare program that reads a byte at a time from each file and compares them.

  5. #45
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    I expanded my tests and looks like I tryed to copy an damaged image.
    I downloaded a new image and it copy it perfectlly.
    Now I tried it with an exe (again) and the bytes amount was the same as usual but when I tried to open it a dos window said: "program to big to fit in memory".

  6. #46
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    You need to compare the input file with the output file to see if all the bytes' contents are the same.

  7. #47
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    Ok I'm working on one now, just to make a boolean method that checks if all the bytes are the same?

  8. #48
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    It should print out the location and the contents of bytes that are different.

  9. #49
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    Since the bytes are stored in a file, the location will be a counter ok?

  10. #50
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    Yes, you would count the bytes as you read them.

  11. #51
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    Hmm maybe this 2 pictures can demonstrate the diffrence between the files.
    tictactoe.jar is the original jar.
    A simple connection between 2 computers-1.png
    A simple connection between 2 computers-2.png
    Put both of the pictures infront of your eyes and you will see that the files are not the same.

  12. #52
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    What is the difference? What byte(s) are different? What is different between the bytes?

  13. #53
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    Can't you see that it diffrent?the first picture starts with "p" while the other one with a square.
    If the copy done well it means it is the same file, so the text in the picture should be the same and it's not.

  14. #54
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    You know that the files are different. Now you need to figure out why.
    What is the difference between the bytes in one file as compared to the other?
    If one has a 'p' what does the other one have? What is the hex value of the first bytes in each file?

    Then go to your program and see what it reads for the first byte. Is it the same char as in the file?
    If so, then why doesn't it get written out. If it is written out, go to the receiver of the file's contents and see if the first byte that is read has the same value as the first byte that was read from the file.

  15. #55
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    Here take a look, it's a gist:
    originial:
    80 75 3 4 20 0 8 0 8 0 -37 -104 -119 63 0 0 0 0 0 0 0 0 0 0 0 0 20 0 4 0 77 69 8
    4 65 45 73 78 70 47 77 65 78 73 70 69 83 84 46 77 70 -2 -54 0 0 -13 77 -52 -53 7
    6 75 45 46 -47 13 75 45 42 -50 -52 -49 -77 82 48 -44 51 -32 -27 -14 77 -52 -52 -
    45 117 -50 73 44 46 -74 82 112 79 -52 77 -27 -27 -30 -27 2 0 80 etc..
    new:
    0 20 0 8 0 8 0 -59 -104 -119 63 18 118 -88 18 -87 1 0 0 -103 2 0 0 12 0 0 0 0 0
    0 0 0 0 0 0 0 0 -30 5 0 0 71 97 109 101 36 50 46 99 108 97 115 115 80 75 1 2 20
    0 20 0 8 0 8 0 -59 -104 -119 63 -21 12 94 23 -86 1 0 0 -103 2 0 0 12 0 0 0 0 0 0
    0 0 0 0 0 0 0 -59 7 0 0 71
    Why the hell it can send a byte array of a picture and not a byte array of a jar, after all they are all arrays !!!!!!!!

  16. #56
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    Looking at the images, the second one looks like it is missing the first few bytes that are in the first one.

  17. #57
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    It's not only few bytes in the start, it's in the start at the middle and in the end (many times).
    How come it sends perfectlly x array (which is an image/txt file) and the y array not?after all I send bytes and where they came from is not relevant.

  18. #58
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    How come it ...
    You will have to debug your code and find out why.
    Is it related to the size of the file?
    Create a file with simple file with known binary, byte data and use it for testing so that you can easily see where bits of the file go wrong.

  19. #59
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: A simple connection between 2 computers

    Finaly It's working !!!
    I will share it with you guys, it's a piece of code:

    Server:
    Java Code:
    ServerSocket servsock = new ServerSocket(2000);
    			while (!this.finish) 
    			{
    			    System.out.println("Waiting...");
    			    Socket sock = servsock.accept();
    			    System.out.println("Accepted connection : " + sock);
    			    
    			    
    			    // sendfile
    			    File myFile = new File ("tictactoe.jar");
    			    byte [] mybytearray  = new byte [(int)myFile.length()];
    			    FileInputStream fis = new FileInputStream(myFile);
    			    BufferedInputStream bis = new BufferedInputStream(fis);
    			    
    			    //Reads bytes from this byte-input stream into the specified byte array
    			    bis.read(mybytearray,0,mybytearray.length);
    			    OutputStream os = sock.getOutputStream();
    			    System.out.println("Sending...");
    			    os.write(mybytearray,0,mybytearray.length);
    			    os.flush();
    			    sock.close();
    			}
    Client:
    Java Code:
    int filesize=6022386; // filesize temporary hardcoded
    		
    		    long start = System.currentTimeMillis();
    		    int bytesRead;
    		    int current = 0;
    		    
    		    // localhost for testing
    		    Socket sock = new Socket("127.0.0.1",2000);
    		    System.out.println("Connecting...");
    		
    		    // receive file
    		    byte [] mybytearray  = new byte [filesize];
    		    InputStream is = sock.getInputStream();
    		    FileOutputStream fos = new FileOutputStream("tictactoe2.jar");
    		    BufferedOutputStream bos = new BufferedOutputStream(fos);
    		    bytesRead = is.read(mybytearray,0,mybytearray.length);
    		    current = bytesRead;
    		
    		
    		    do 
    		    {
    		       bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
    		       if(bytesRead >= 0) 
    		    	   current += bytesRead;
    		    } 
    		    while(bytesRead > -1);
    		
    		    bos.write(mybytearray, 0 , current);
    		    bos.flush();
    		    long end = System.currentTimeMillis();
    		    System.out.println(end-start);
    		    bos.close();
    		    sock.close();

  20. #60
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: A simple connection between 2 computers

    The trick appears to be to consider that the receiver/client may get the bytes in separate blocks.
    You need to keep track of what was received by using the current variable to advance in the buffer past what has already been received and to decrement the number of bytes to be read by the value in current. See lines 17, 22 and 24

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Playing sounds on other computers
    By jeffpaulwilson in forum AWT / Swing
    Replies: 0
    Last Post: 09-14-2011, 09:25 PM
  2. Help needed!!!!! choosing network computers
    By apoorv in forum Networking
    Replies: 6
    Last Post: 03-22-2011, 05:33 PM
  3. Need to have multiple computers run same java program
    By thehighlander12 in forum Networking
    Replies: 11
    Last Post: 07-07-2010, 02:30 PM
  4. Career Paths in Computers
    By Lil_Aziz1 in forum New To Java
    Replies: 26
    Last Post: 06-30-2010, 01:59 PM
  5. Sharing a game instance over two computers
    By keiys in forum Java Gaming
    Replies: 0
    Last Post: 04-21-2010, 02:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •