Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
client code:
C#
public class Client {
	     ServerSocket sendServer = null;
         // Socket soc;
         FileDetails details;    
	  /**
	   * @param args
	   */   
	public Socket send(Socket soc) {
		   // TODO Auto-generated method stub
		   byte data[];
           try {
	        File file = new File("D:\\Untitled.wma");
	        // Getting file name and size
	        if (file.length() > Integer.MAX_VALUE) {
	            System.out.println("File size exceeds 2 GB");
	        } else {	           
	        System.out.println("Waiting for Server...");	          
	        // File Object for accesing file Details
	        System.out.println("Connected to Server...");
	        data = new byte[2048]; // Here you can increase the size also which will send it faster
            details = new FileDetails();
	        details.setDetails(file.getName(), file.length());
	        // Sending file details to the client
	        System.out.println("Sending file details...");
	        ObjectOutputStream sendDetails = new ObjectOutputStream(soc.getOutputStream());
	        sendDetails.writeObject(details);
	        sendDetails.flush();
	        // Sending File Data 
	        System.out.println("Sending file data...");
	        FileInputStream fileStream = new FileInputStream(file);
	        BufferedInputStream fileBuffer = new BufferedInputStream(fileStream);
	        OutputStream out = soc.getOutputStream();
	        int count;
	        while ((count = fileBuffer.read(data)) > 0) {
	        System.out.println("Data Sent : " + count);
	        out.write(data, 0, count);
	        out.flush();
	        }
       //    out.close();
         //   fileBuffer.close();
        //   fileStream.close();
	        }
	        } catch (Exception e) {
	    	e.printStackTrace();
	        }        
            return soc;
	        }    
	
   public void Receive(Socket soc) {
		    try
	     	{	
			System.out.println("Getting details from Server...");
			System.out.println(soc);

	        ObjectInputStream getDetails = new ObjectInputStream(soc.getInputStream());
	        FileDetails details = (FileDetails) getDetails.readObject();
	        System.out.println("Now receiving file...");
	        // Storing file name and sizes	
	        String fileName = details.getName();
	        System.out.println("File Name : " + fileName);
	        byte data[] = new byte[2048]; // Here you can increase the size also which will receive it faster
	        FileOutputStream fileOut = new FileOutputStream("E:\\" + fileName);
	        BufferedOutputStream fileBuffer = new BufferedOutputStream(fileOut);
	        InputStream fileIn = soc.getInputStream();	        
	        int count;
	        int sum = 0;
	        while ((count = fileIn.read(data)) > 0) {
	        sum += count;
	        fileBuffer.write(data, 0, count);
	        System.out.println("Data received : " + sum);
	        fileBuffer.flush();
	        }
	        System.out.println("File Received...");	        
	 //      fileIn.close();
	        fileBuffer.close();
	        fileOut.close();	   
	        } catch (Exception e) {
	    	e.printStackTrace();
	        }	
	        }
	public void connClose(Socket soc)
	        {
		    try {
			soc.close();
			sendServer.close();
		    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		    }
	        }
        	}


Server code:
C#
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {

	/**
	 * @param args
	 */
	ServerSocket ser = null;
	Socket soc;
	FileDetails details;
	public Socket getConnection()
	    {
		try {
			ser = new ServerSocket(1111);
			System.out.println("ready accept Client req" );
			soc = ser.accept();
			System.out.println("accepted" );
			System.out.println(soc);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return soc;
	}
	public void send(Socket soc, String out_file) {
		// TODO Auto-generated method stub
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		out_file="F:\\Untitled.wma";
		byte data[];
		try {
			if(soc != null){
				System.out.println("Socket is estalished");
			}else{
				Server s = new Server();				
				soc=s.getConnection();
				send(soc, out_file);
			}
			File file = new File(out_file);
			// Getting file name and size
			// File Object for accesing file Details
			System.out.println("Connected to Client...");
			data = new byte[2048]; // Here you can increase the size also which will send it faster
			details = new FileDetails();
			details.setDetails(file.getName(), file.length());
	        // Sending file details to the client
	        System.out.println(soc);
	        OutputStream os= soc.getOutputStream();
	        ObjectOutputStream sendDetails = new ObjectOutputStream(os);
	        System.out.println("Sending file details...");
	        sendDetails.writeObject(details);
	        sendDetails.flush();
	        // Sending File Data 
	        System.out.println("Sending file data...");
	        FileInputStream fileStream = new FileInputStream(file);
	        BufferedInputStream fileBuffer = new BufferedInputStream(fileStream);
	        OutputStream out = soc.getOutputStream();
	        int count;
	        while ((count = fileBuffer.read(data)) > 0) {
	            System.out.println("Data Sent : " + count);
	            out.write(data, 0, count);
	            out.flush();
	        }

//	        out.close();
	        fileBuffer.close();
	        fileStream.close();
		 } catch (Exception e) {
			 e.printStackTrace();
		 }
	}
	
	public Socket recieve(Socket soc)
	{
		try{
	        ObjectInputStream getDetails = new ObjectInputStream(soc.getInputStream());
	        FileDetails details = (FileDetails) getDetails.readObject();
	        System.out.println("Now receiving file...");
	        // Storing file name and sizes

	        String fileName = details.getName();
	        System.out.println("File Name : " + fileName);
			byte data[] = new byte[2048]; // Here you can increase the size also which will receive it faster
	        FileOutputStream fileOut = new FileOutputStream("F:\\" + fileName);
	        InputStream fileIn = soc.getInputStream();
	        System.out.println(soc);
	        BufferedOutputStream fileBuffer = new BufferedOutputStream(fileOut);
	        System.out.println(soc);
	        int count;
	        int sum = 0;
	        while ((count = fileIn.read(data)) > 0) {
	            sum += count;
	            fileBuffer.write(data, 0, count);
	            System.out.println("Data received : " + sum);
	            fileBuffer.flush();
	        }
	        System.out.println("File Received...");
	        fileBuffer.close();
	        fileOut.close();
	        fileIn.close();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return soc;
	}
	public void connClose()
	{
		try {
			soc.close();
			ser.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}


C#
File details:

public class FileDetails implements Serializable  {

            /**
            * @param args
            */
           String name;
           long size;
           public void setDetails(String name, long size) {
               this.name = name;
               this.size = size;
           }
           public String getName() {
               return name;
           }
           public long getSize() {
               return size;
           }
           }


the connection is establishing and the client send the files to server up to that the code working fine but after that server can't sending the files to client.telling that socket write error.

thank you sir
Posted
Updated 20-Feb-15 19:08pm
v2
Comments
Richard MacCutchan 21-Feb-15 4:35am    
Then you will need to do some debugging to find out exactly where, and why. Please do not just dump all your code and expect other people to debug it for you.

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