Java代写,java作业代写:100%原创代码
当前位置:以往案例 > >Java Client-Server with Sockets
2018-05-19

Preamble

Some of these questions will be must be handed in for assessment        by the end of April 30th. Detailed submission        instructions will be added in due course.

Please check back here for last minute updates before        submitting your work

There is now a module        forum. You need your usual UoL username password to        connect; you can post anonymously, or login within the forum        using your informatics linux username/password.  All posts are        moderated, so might not show up immediately.

You should review Lecture        15 for discussion of         Socket programming in java and basic client/server architecture.

The deadline for this work is Monday 30th April.

In this page (and in other pages for this module):

Pale yellow boxes indicate code or other material that should be the contents of a file — you will generally use an editor, such as Emacs, Scite or GEdit to enter/view the content.

Pale blue boxes indicate a place where you should be entering a command, e.g. at a command prompt in a terminal window.

Pale pink boxes indicate interactive use, with the input you should        type in RED; other text is expected        output from a program.

Hints and asides are written in pink italics.

Question 1 — Number Square       Service (Unassessed)

Your task is to write a pair of programs, client and server,        to implement a number squaring service. This initally be        similar to the TimeServer/Client        example discussed in lectures.

You will need to choose a port to run your service on. To get a unique port number, try using your numeric        uid. "getent passwd" will tell you        your uid (and your gid).

Client

The client uses a Socket to make a connection to the          server. Once connected, the client prompts the user for a          number (in the console; no need for a GUI), and sends it to          the server. The server responds with the square of the          number, which the client will print on the screen.

Keep going until the user submits the special number          999. The client should send the special number, then shut          down.

Server

Server listens for connections. When a connection is made, it        should create a handler Thread which waits for numbers to be        sent from the client. After receiving a number, it should        calculate the square and send it back to the client.

Repeat until the client sends the special number 999.

Extra features

  • What happens if the client tries to connect and no server is            running?

  • What happens if one end just stops in the middle (client            never sends another number; server never responds to a            request)?

  • Test it over two machines. Try running more than            one client against one server.

  • Test your client with another person's server; and vice            versa.

Unassessed — model solution here


Question 2 — Online Voting (assessed)

There is now a module forum. You need your usual UoL username password to connect;      you can post anonymously, or login within the forum using your informatics linux username/password.      All posts are moderated, so might not show up immediately.


The worksheet is presented in several phases to lead you        towards a working solution. If you do not make it to the final phase, you should hand in as far as you        have managed. If you do make it to the final        phase, you only need to hand that in.

The theme of the exercise is to produce a simple client/server        on-line voting system. The server will:

  1. be setup with a fixed candidate list (the BallotPaper) and        a list of eligible voters (the VoterList);

  2. accept connections from clients (voters); send the voter a        copy of the list of candidates and then record their vote.

It is important that you write your code in a java package called        "CO2017.exercise3. This will simplify the marking process. To        achieve this, you need to start each code file with a line        like this (replaceabc123with your own username).

package CO2017.exercise3.abc123;

If you are using eclipse or a similar IDE, it should have        tools available to make it simple to write your code in a        specific package, and/or to move code from one package to        another.

Phase 0

Download the jar file  containing the packageCO2017.exercise3.supportof basic classes for representing Ballots,  Ballot boxes and lists of voters. UPDATED (28th          March) so it can be imported as library in          eclipse. This code forms the packageCO2017.exercise3.support.

See javadoc documentation for the          classes included. This describes the public interface provided          by the classes. You do not need to implement these          yourself.

See test code area for some basic  test programs. You can use these to explore the behaviour of          the providedCO2017.exercise3.supportpackage,          and verify that you have installed them properly.

See also junit test code for some basic          junit test programs. Provided for completeness only — you do          not need to use these. Working out how to          compile and run these junit test cases is left as an          exercise for the reader…


Phase 1

In this phase, you will produce simple client and server  programs to implement a protocol for online  voting. The client and server will make use of the basic  data structure classes from Phase 0.

Note that 70% of the CW3 assessment marks are for phase 1.

Protocol

This section describes the protocol that the client and server use    to communicate. Note that the messages passed between            client and server using the protocol are not necessarily            the same as the messages that will be displayed on-screen            for the user of the system.

In all cases

  • vid,bnumare decimal            integers (in String form) representing            unique voter and ballot identifiers;

  • voteis a single character vote;

  • BALLOT,OK,END, etc are literal Strings values;

  • similarly,Xvid,Xbnum,XvoteandXXXXare values that do not match the expected ones (used in            the transaction so far);

  • values on a single line are separated with colons            (':');

  • lines are to be terminated where shown;

  • data should be flushed after each message;

  • {close connection}indicates that the            socket connection should be closed

Here are some typical protocol transactions between an individual            client and the server.

  • A typical (no error) vote transaction:

  • CLIENT SERVER Comment
    vid => client requests a ballot
    <=
    vid:bnum:BALLOT
    A:Candidate 1
    B:Candidate 2
    0:END
    server sends back thevid, a new & unique ballot                  id (bnumand the string                  "BALLOT"; and then, starting on a new line, the                   ballot paper, one candidate per line, terminated                  with "0:END" on a line of its own.
    vid:bnum:vote => client sends back the vid, the ballot id, plus the vote
    <=
    vid:bnum:vote:OK
    {close connection}
    server sends back the vid, a ballot id, the                  received vote, plus "OK"; then closes the connection
    {close connection}
    client closes the connection
  • Client attempts duplicate vote:

  • CLIENT SERVER Comment
    vid
    => client sends a duplicate voter id (who has                  already voted)
    <=
    vid:0:DUPLICATE
    {close connection}
    server sends back thevid, the                  special ballot                  id0(zero) and the string                  "DUPLICATE"; and then closes the connection
    {close connection}
    client displays an error message and closes the connection
  • Vote closing transaction:

  • CLIENT SERVER Comment
    0
    {close connection}
    => client sends special zero value and closes the connection
    {close connection}
    server closes connection and closes the ballot
  • Incorrect initial response from server:

  • CLIENT SERVER Comment
    vid => client requests a ballot
    <=
    Xvid:Xbnum:XXXX
    server sends back a different voter id OR ballot id OR a string                  other thanBALLOT
    {close connection} client closes the connection (and therefore, so will                  server) — no vote will be recorded
  • Incorrect 2nd response from client:

  • CLIENT SERVER Comment
    vid => client requests a ballot
    <=
    vid:bnum:BALLOT
    A:Candidate 1
    B:Candidate 2
    0:END
    server sends back the correct vid, a ballot id, plus the                  ballot paper, terminated with "0:END"
    Xvid:Xbnum:vote => client sends back the something other than the                  expected voter id OR ballot id OR a vote that is not                  possible then the vote must be discarded
    <= vid:bnum:vote:ABORT {close connection} server aborts the vote and closes the connection
    {close connection}
    client displays an error message and closes the connection
  • Incorrect 2nd response from server:

  • CLIENT SERVER Comment
    vid => client requests a ballot
    <=
    vid:bnum:BALLOT
    A:Candidate 1
    B:Candidate 2
    0:END
    server sends back the correct vid, a new & unique ballot                  id; then, starting on a new line, the                  ballot paper, terminated with "0:END"
    vid:bnum:vote => client sends back the vid, the ballot id, plus the vote
    <=
    Xvid:Xbnum:Xvote:XXXX
    {close connection}
    server records the vote, but then (for some                  reason) sends back an incorrect response (one or more                  of voter id, ballot number, vote or the "OK" message are                  wrong) and closes the connection
    {close connection}
    display a warning to voter about the incorrect                  response from the server and then close the connection
I/O streams

To avoid confusion, all data should be sent asStringvalues. Do not useDatareaders/writers to send numbers. You            should use something very like this (on both ends of the            connection):

BufferedReader rdr = new
  BufferedReader(new InputStreamReader(server.getInputStream(),                                       "UTF-8"));Writer out = new OutputStreamWriter(server.getOutputStream());

Make sure that strings you write are terminated            (with"\r\n"or, if using formatted strings,            with"%n").

Make sure youflushthe output            buffer after finishing each transaction.

You can then usereadLineto read in            complete lines on the other end of the connection.

Phase 1A: Simple client —Voter.java

This is a simple interactive client, for use in a command            line terminal. For example, here are several successive            invocations of the simple client program:

$ java CO2017.exercise3.gtl1.Voter localhost 8111Connected to localhost/127.0.0.1
Enter voter id: 12341234: 1
A:E.Smith (Apple Affiliates)
B:B.Jones (Android Alliance)
C:A.Brown (Microsoft Party)
D:D.Green (Blackberry Bunch)
E:C.Black (Penguinistas)
Enter vote: EVote processed OK
$ java CO2017.exercise3.gtl1.Voter localhost 8111Connected to localhost/127.0.0.1
Enter voter id: 12351235: 2
A:E.Smith (Apple Affiliates)
B:B.Jones (Android Alliance)
C:A.Brown (Microsoft Party)
D:D.Green (Blackberry Bunch)
E:C.Black (Penguinistas)
Enter vote: DVote processed OK
$ java CO2017.exercise3.gtl1.Voter localhost 8111Connected to localhost/127.0.0.1
Enter voter id: 14321432: 3
A:E.Smith (Apple Affiliates)
B:B.Jones (Android Alliance)
C:A.Brown (Microsoft Party)
D:D.Green (Blackberry Bunch)
E:C.Black (Penguinistas)
Enter vote: EVote processed OK
$ java CO2017.exercise3.gtl1.Voter localhost 8111Connected to localhost/127.0.0.1
Enter voter id: 1432      

在线提交订单