Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-14
11
Medium Priority
?
208 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:Beardsley_Lan
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8137360
So?
0
 
LVL 1

Expert Comment

by:pronane
ID: 8137366
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
 
LVL 1

Expert Comment

by:pronane
ID: 8137393
/*
  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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Accepted Solution

by:
pronane earned 150 total points
ID: 8137435
/*  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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8137591
>>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
 
LVL 1

Expert Comment

by:pronane
ID: 8137614
ya IM not sure but i checked his profile
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8140814
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
 
LVL 5

Expert Comment

by:Netminder
ID: 8147099
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
 

Author Comment

by:Beardsley_Lan
ID: 8149992
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
 

Author Comment

by:Beardsley_Lan
ID: 8150002
Thanks again, you have started me off.
0
 
LVL 5

Expert Comment

by:Netminder
ID: 8154830
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question