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.
Beardsley_LanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
So?
0
pronaneCommented:
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.
0
pronaneCommented:
/*
  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>
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

pronaneCommented:
/*  this is a client program that can be called form the main of another program, takes in its main method runclient two parameters a string ip and a portnumber, so that a user can enter in the ip address and portnumber they want to connect to.  copyright paul ronane
*/


class Client
{
     protected Socket sock;
     private BufferedReader in;
     private PrintWriter out;
     private DataInputStream dis;
     
     public void runclient(String ip, int portnum)
     {
          try
          {
               sock = new Socket(ip, portnum);
               dis = new DataInputStream(sock.getInputStream());
               in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
               out = new PrintWriter(sock.getOutputStream(), true);
         
          }

          catch (IOException e)
          {
               System.out.println(e);
               System.exit(1);
          }
         
     }

     public void sendText(String s)
     {
          String text = s;
          out.println(text);
          out.flush();
     }
     
     public boolean hasText()
     {
          boolean paul = false;
          try
          {
               paul = (dis.available() > 0);
          }
         
          catch (IOException e)
          {
               System.out.println(e);
               System.exit(1);
          }
          return paul;
     }
   
    public String gettext()
     {
          String paul = new String();

          try
          {
               paul = in.readLine();
          }
          catch (IOException e)
          {
               System.out.println(e);
               System.exit(1);
          }
          return paul;
     }
           
     public void closesoc()
     {
          try
          {
               this.sock.close();
          }
          catch (IOException e)
          {
               System.out.println(e);
               System.exit(1);
          }
     }
     
}

to compile type;
javac Client.java

to run you must call from another class like:

Client p = new CLient();
p.runclient(ip, portnumber);

such as p.runclient(136.2.4.67, 80);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
>>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.
0
pronaneCommented:
ya IM not sure but i checked his profile
0
functionpointerCommented:
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,
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20545602.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 ).  ;)



0
NetminderCommented:
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
0
Beardsley_LanAuthor Commented:
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.
0
Beardsley_LanAuthor Commented:
Thanks again, you have started me off.
0
NetminderCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.