Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
server side program


Java
import java.net.*; 
import java.io.*; 
import java.util.Scanner;
public class Server 
{ 
    public static void main (String [] args ) throws IOException 
    { 
    int filesize=1022386; 
    int bytesRead; int currentTot = 0; 
      try
    {  
    ServerSocket serverSocket = new ServerSocket(9999); 
    Socket socket = serverSocket.accept(); 
    System.out.println("Accepted connection : " + socket);
    BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));    
    String temp=br.readLine();
    System.out.println("The mesage from client is::"+temp);
    PrintStream  pr=new PrintStream(socket.getOutputStream()); 
                String  str = "acknowledgement from server";
                pr.println(str);
    Scanner scanner = new Scanner(System.in);  
    String path_save = scanner.nextLine();
             
    byte [] bytearray = new byte [filesize]; 
    InputStream is = socket.getInputStream(); 
    FileOutputStream fos = new FileOutputStream(path_save); 
    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();
    }
    catch(Exception ex)
        
    {
        System.err.println(ex.getClass().getName()+":"+ex.getMessage());
        System.exit(0);
        
    }
    }
}


client side program

Java
import java.net.*; 
import java.io.*; 
import java.util.Scanner;
public class client 
{ 
    public static void main (String [] args ) throws IOException 
    { 
    int filesize=1022386; 
    int bytesRead; 
    int currentTot = 0; 
    try
    {
    Socket socket = new Socket("10.176.24.63",9999);
    Scanner scanner = new Scanner(System.in);  
    String path = scanner.nextLine();
    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(); 
        PrintStream  pr=new PrintStream(socket.getOutputStream()); 
    
                    System.out.println("Enter the file name,type,path");
                    BufferedReader  br=new  BufferedReader(new InputStreamReader(System.in));  
                    String temp = br.readLine();
                    pr.println(temp);	
                    BufferedReader  ed=new  BufferedReader(new InputStreamReader(socket.getInputStream())); 
                    String  st = ed.readLine();
                    System.out.println(st);
        System.out.println("Sending Files..."); 
        os.write(bytearray,0,bytearray.length); 
        os.flush();
        socket.close(); 
        System.out.println("File transfer complete");
    }
        catch(Exception ex)
        {
            System.err.println(ex.getClass().getName()+":"+ex.getMessage());
            System.exit(0);
        }

    }
}
Posted
Updated 31-Jul-14 20:38pm
v2
Comments
Richard MacCutchan 1-Aug-14 2:47am    
What is the content of the jar file, which program are you trying to run, what is the exact text of the error message?
Member 10946695 1-Aug-14 2:49am    
Exception in thread main Class not found this was the error.. iam trying to run the jar file of above "server.java" program in cmd using "java -jar server.jar"
Richard MacCutchan 1-Aug-14 4:56am    
Did you add a manifest to your jar file to identify the main class?
Member 10946695 1-Aug-14 4:58am    
manifest file there in the project folder by default
Richard MacCutchan 1-Aug-14 5:49am    
That's not good enough, you need to add a proper mainfest. I just tried it and it does not work with the default.

1 solution

PLEASE use an IDE for development. The IDE would tell you right away that you did miss to import the class Exception.

Eclipse[^]

Netbeans[^] (java SE Version is perfectly fine)

Both are free, both will guide you through the process of developing.
Nobody is developing by foot any more. There is no reason for not using the given tools.
 
Share this answer
 
v2
Comments
Richard MacCutchan 1-Aug-14 4:22am    
Does that mean I must stop writing code using Notepad?
TorstenH. 1-Aug-14 4:57am    
Maybe you should light a bonfire in the center of the office while you starve like the other dinosaurs.

Serious: I'd like my colleagues to focus more on the design and the way they write their stuff than to think about the syntax of command xy.
Member 10946695 1-Aug-14 4:23am    
i am working in netbeans ide, both server and client program works smoothly with in netbeans ,this happens when moving to executable jar file
TorstenH. 1-Aug-14 5:00am    
Then there is something missing in the jar.
PLease try following:
get rid of the general imports

import java.net.*;
import java.io.*;

and let netbeans do that for you.
For the deployment take a look here: Deployment with Netbeans

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