?
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
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

770 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