Link to home
Start Free TrialLog in
Avatar of Beardsley_Lan
Beardsley_Lan

asked on

Java network programming. Creating a server and client program to allow client to connect and download files from server

Server program functionality:
allow user to enter port number for server to listen to
allow multiple clients to connect to it

Client functionality:
allow user to enter ip address and port number of server to connect to
allow user to enter file to download

These are two separate java programs.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

So?
Avatar of pronane
pronane

nice repsonse cehj, is this a programming assignment for college if not I will post the code to do this as I have done this several times.
/*
  c paul ronane
multithreadedserver program that allows multiple connections to the server takes in the port number to run from the command line, can accept up to at least 50 clients havent tried anymore, sets the default port number to 80 if not set on the command line
*/


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

public class ThreadServer
{  
     
     public static void main(String[] args)
     {  
         
          int portnum;

          if (args.length != 1)
          {
               portnum = 80;
          }
          else
          {
               portnum = Integer.parseInt(args[0]);
          }

          try
          {  
               ServerSocket s = new ServerSocket(portnum);
               int thrnum = 1;
         
               for (;;)
               {  
                    Socket client = s.accept( );
                    System.out.println("Spawning " + thrnum);
                    new ThreadHandler(client, thrnum).start();
                    thrnum++;
               }
          }
          catch (Exception e)
          {  
               System.out.println(e);
          }
     }
}

class ThreadHandler extends Thread
{  
     private Socket incoming;
     private int counter;
     private BufferedReader inp;
     private PrintWriter outp;
     private int tn;
     private static Vector handlers = new Vector();

     public ThreadHandler(Socket inc, int tn) throws IOException
     {
          incoming = inc;
          counter = tn;

          inp = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
          outp = new PrintWriter(incoming.getOutputStream(), true);
     }

     public void run()
     {  
          try
          {  
               handlers.addElement(this);
               String username = inp.readLine();
               outp.println( "Hello " + username + " ! Welcome to the Chat Room." );
               broadcast(" has joined the discussion.", username);
               
               boolean done = false;
               while (!done)
               {  
                    String str = inp.readLine();
                    broadcast(str, username);
                    System.out.println(handlers.toString());
                    if (str.trim().equals("BYE"))
                    {
                         broadcast(" has left the discussion.", username);
                         remove(incoming);
                         dispose();
                         done = true;
                    }

               }
               incoming.close();
          }
          catch (IOException e)
          {  
               System.out.println(e);
          }
     }
     
     public synchronized void broadcast(String message, String username)
     {
          Enumeration e = handlers.elements();
          int i = 1;
          while (e.hasMoreElements())
          {
               ThreadHandler th = (ThreadHandler) e.nextElement();
               try
               {
                    //th.outp.println("User" + c + ": " + message);
                    th.outp.println(username + ": " + message);
                    th.outp.flush();
                    i++;
               }
               catch (Exception ex)
               {
                    th.outp.close();
               }
          }
     }

     static synchronized void remove(Socket s)
     {
          handlers.remove(s);

     
    }    
}


to compile type
javac ThreadServer.java
to run
java ThreadServer <portnumber>
ASKER CERTIFIED SOLUTION
Avatar of pronane
pronane

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>is this a programming assignment for college

What do you think pronane? Beware - if so, your (double) posting is against the terms of this site.
ya IM not sure but i checked his profile
pronane,

What does your posted 'chat room demo' code have to do with this question?  

I would think your FileServerReader and FileClientReader code would be much more appropriate.

I think this is what he meant, Beardsley_Lan,
https://www.experts-exchange.com/questions/20545602/File-Transfer-Java-Server-Program-and-a-sample-client-application-that-tests-the-server.html

>connect to.  copyright paul ronane
although it is nice to finally see you taking credit for your work ( emphasis applied on whatever words make it work for you ).  ;)



pronane,

It's a violation of the membership guidelines to engage in "academic dishonesty" -- both on the part of someone asking Experts to do his homework, and on the part of someone who actually does it.

Since the profile of the asker doesn't contain anything that indicates one way or the other, it's my advice that you err on the side of caution.

Beardsley_Lan,

Please explain what the genesis of your question is; otherwise, it will be closed and your points deducted.

Netminder
Site Administrator
Avatar of Beardsley_Lan

ASKER

Netminder,
This is NOT my academic project.
I am currently on probation for my job.
Thanks to pronane, i can work out the rest myself.
Thanks again, you have started me off.
Beardsley_Lan,

Thanks for posting; please understand that the Experts are just being cautious, because of the prohibition against doing homework for students.

Netminder
EE Admin