首页
编程语言
数据库
网络开发
Algorithm算法
移动开发
系统相关
金融统计
人工智能
其他
首页
>
> 详细
TCP留学生程序辅导、Java程序设计调试、Java编程语言辅导 解析R语言编程|解析Haskell程序
Exercise 4
The goal of this exercise is to create a TCP server for serving requests to clients. In the first part, we are going to
create a server and test it with telnet, a program for connecting to a TCP server and send virtual text terminal.
Don’t forget to read also the general instructions for the assignment
(https://moodle.njit.edu.cn/moodle/mod/resource/view.php?id=2158).
Remember that while it is acceptable to discuss with the other students, the solutions submitted must be
individual and plagiarism is not acceptable. If you reuse small snippets of code that you didn’t write yourself,
remember to mention in English where they come from, using code comments inside your code. However,
remember that you should still be the main author of the solutions you provide.
Telnet
First make sure that you can use telnet on your computer. If you are using Windows 10, telnet is disabled by
default but can be enabled like this:
https://social.technet.microsoft.com/wiki/contents/articles/38433.windows-10-enabling-telnet-client.aspx
Another (and possibly better) alternative for Windows is to use PuTTY which an open source SSH and Telnet
client for Windows. If you don’t want to install PuTTY, you can download a single executable that contains only
the Telnet client:
• 32-bit version: https://the.earth.li/~sgtatham/putty/latest/w32/puttytel.exe
• 64-bit version: https://the.earth.li/~sgtatham/putty/latest/w64/puttytel.exe
(These files are also available on Moodle)
If you are using MacOS, recent versions do not include telnet by default anymore and will need to install it (see
for example: https://osxdaily.com/2018/07/18/get-telnet-macos/)
If you are using Linux, it is very like you can install telnet with your package manager (e.g. “sudo apt install
telnet” on Ubuntu or Debian)
(On Linux and MacOS, an alternative is also to use nc (netcat), see for example:
https://www.igorkromin.net/index.php/2018/07/12/macos-has-a-much-better-tool-than-telnet-for-testingremote-server-connectivity/)
The instructions in this exercise will assume that you use Windows with the PuTTY Telnet client.
TCP Server
Create a new package programming3.chatsys.tcp in which you will put all your classes relating to the TCP
server and client.
First, we will create a basic HTTP server. In Java, we can create a TCP server with a java.net.ServerSocket.
Create a class TCPChatServer that has the following attributes:
• An integer with the port on which the server will listen
• An integer with the timeout for the TCP session
• A Database object
• A Boolean that is true if the server is running
• A ServerSocket
Create a constructor to initialize the attributes and add two public method start and stop.
The method start is used to start the server (i.e. initialize the ServerSocket) and should, in a loop, accept
connections by calling the method accept on the ServerSocket. The method “accept” returns a Socket object.
Set the timeout of the Socket using the method setSoTimeout, so that the socket is automatically closed if it
doesn’t receive anything from the client after a certain amount of time. Then we will handle the connection
with the client with a new Thread and a TCPChatServerSession (see below).
The stop method simply sets the boolean of the server to false to close the loop and also call the method
“close” on the ServerSocket.
TCPChatServerSession
A TCPChatServerSession is a class that implements Runnable. It has two attributes that are initialized in the
constructor:
• A Database object (the same as the one used by the TCPChatServer)
• A Socket object (the one obtained when the server accepts a connection with a client)
As the class is a Runnable, it needs to implement the method “run()”. The method does the following:
• Create a BufferedWriter from the OutputStream of the Socket (obtained with the method
getOutputStream)
• Create a BufferedReader from the InputStream of the Socket (obtained with the method
getInputStream)
• Read a line from the BufferedReader
• Parse the line as defined by the protocol of ChatSys 4.0
• Take some action based on the protocol messaged received from the client.
Protocol
The server can receive the following messages from the client.
• “OK\r\n”: OK messages can be sent by the client to keep the server alive (i.e. prevent the connection
from being closed because of timeout). These messages are simply ignored by the server.
• “GET recent messages
\r\n”: a client sends this message to obtain the N most recent messages.
The server responds with a list of N messages which includes the username of the author, the
timestamp and the message. It sends the following messages back to the client
o “MESSAGES
\r\n” to tell the clients how many messages to read. N is usually the same as
N2, but if the number of messages in the database is smaller than N, than N2 is the number of
messages in the database.
o “MESSAGE
\r\n” for each message to send.
• “REGISTER
\r\n”: a client sends this message to register as a user.
The server replies with:
o “ERROR username already taken\r\n” if the username already exists.
o “OK\r\n” if the registration was successful.
• “LOGIN
\r\n”: a client sends this message to authenticate itself, the server
responds with:
o “OK\r\n” if the username exists and the password match the one in the Database.
o “ERROR
\r\n” if the username or passwords are invalid.
• “POST
\r\n”: a client sends this message and the server replies with
o “OK\r\n” if the user is authenticated.
o “ERROR user unauthenticated\r\n” otherwise.
• “GET unread messages\r\n”: a client sends this message and the server sends back the list of messages
since the last time this message was sent by the authenticated user. The server responds with:
o If the user is authenticated, a similar answer as for “GET recent messages
” but with N2 the
number of unread messages. (Don’t forget that the server also needs to update the user’s
lastReadId in the Database!)
▪ “MESSAGES
\r\n” to tell the clients how many messages to read. N3 is the
number of unread messages.
▪ “MESSAGE
\r\n” for each message to send.
o If the user is not authenticated, “ERROR unauthenticated user\r\n”.
• When receiving an invalid message that cannot be parsed, the server will reply with “ERROR
message>\r\n”. Every time the server sends an ERROR message, it closes the connection with the
client.
Start by implementing the server to handle the “GET recent messages
” messages.
Testing the server
Before implementing the other messages of the protocol, you should test that it works properly. There are
different ways of testing your server:
• Using unit tests. Ideally, you should split the logic of the server in different methods. Then you can
create individual unit tests with Junit to make sure that small pieces (i.e. units) of your program work
properly. For example, it is easier to write unit tests if you have a method for:
o Determining the type of message of a given line
o Parsing each type of protocol message
o Generating the response message(s) for each request
• Testing manually with Telnet
• Testing by implementing a TCP Client
Ideally, you should use these three techniques to make sure that your server behave as expected when it
receives a correct message, but can also handle incorrect messages. Remember that in the end, your server
must be able to keep serving clients if it receives an incorrect message!
Testing with Telnet
Telnet can be used to connect to your server. Assuming your server is running on port 42, you can connect to it
like this:
Notice how the connection type is set to “Raw” and not to “Telnet” by default. This is important as our server
is not a real Telnet server.
Once we click on “Open”, we get a terminal that allows us to send text to the server, and get an anser.
For example, we can type “GET recent messages 10”:
And once we press enter, we get as reply:
To end the connection with the server, we can press Ctrl+D. If we don’t do anything for a certain amount of
time, the connection will be closed by the server based on the timeout set to the Socket when the server
accepted the connection (by calling setSoTimeout).
Testing by implementing a TCP Client
While testing with telnet is relatively simple, it has several disadvantages. First, we need to type the protocol
messages manually. If we make typos, we might make mistakes. Also, we must verify manually that the output
is correct and cannot rerun the same test easily. In addition, the timestamp is shown as a number rather than a
formatted date, which makes it is not very user friendly.
To solve those problems, we can implement a client in Java instead. Moreover, if we wanted to provide a
graphical user interface, we would also need for the GUI to act as a client with our server.
Once you are done testing your client with telnet, you should start implementing a client in Java. For that,
create a new class (e.g. you can call it TCPChatClient). Creating a TCP client in Java is easier than a server: all we
need to do is create a new Socket. For example, in the example above with Telnet, the server is running locally
(i.e. localhost is the address of the server which points to our computer) and on the port 42. We can create a
TCP (client) socket in Java to the server like this: Socket socket = new Socket(“localhost”, 42);
In the same way as for the server, we can obtain from this socket an InputStream and an OutputSteam for
creating a BufferedReader and a BufferedWriter so we can exchange messages with the server. Implement a
client that ask the server for the 10 most recent messages and print them. Your client should provide an output
that is more user friendly. For example, the timestamp of the message should be displayed as a formatted date
and time rather than as a number.
Mid-term assignment - Implement the full protocol
Now you should have a working client and server for receiving the most recent messages in the database. You
should now implement the missing protocol messages defined above in order to get a fully functional chat
server based on TCP. Make sure to test your server, with Telnet, unit tests and a client written in Java that your
program behave correctly and can handle incorrect messages.
Once you are done, make sure your code fulfills the requirements for the mid-term assignment
(https://moodle.njit.edu.cn/moodle/mod/resource/view.php?id=2158).
Bonus: SSL connection
There is one issue with our TCP client: when a user register or authenticate, it sends its password over the
network. Thus, a hacker could intercept the messages sent over the network and get access to the user’s
password!
In a real-world scenario, to prevent this from happening we would use secure connections that are encrypted.
It is very common to do this using SSL/TLS which provides encryption on top of a TCP session.
As a bonus, implement SSL as part of your server (and client) to secure the connection between them.
联系我们
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-21:00
微信:codinghelp
热点文章
更多
讲解 econ1202 – quantitativ...
2024-11-22
辅导 msds 490: healthcare an...
2024-11-22
讲解 civl 326 geotechnical d...
2024-11-22
辅导 term paper medicine whe...
2024-11-22
讲解 eng3004 course work辅导...
2024-11-22
讲解 ee512: stochastic proce...
2024-11-22
辅导 geog100 ol01 - fall 202...
2024-11-22
辅导 st5226: spatial statist...
2024-11-22
讲解 ece 101a engineering el...
2024-11-22
讲解 database development an...
2024-11-22
讲解 comp3134 business intel...
2024-11-22
讲解 practice exam 2, math 3...
2024-11-22
讲解 project 4: advanced opt...
2024-11-22
辅导 38003 organisational be...
2024-11-22
辅导 economic growth调试spss
2024-11-22
辅导 ee512: stochastic proce...
2024-11-22
讲解 eesb04 "principles of h...
2024-11-22
辅导 am2060 final assignment...
2024-11-22
辅导 acfim0035 fundamentals ...
2024-11-22
辅导 stat 612 (fall 2024) ho...
2024-11-22
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 99515681 微信:codinghelp
© 2024
www.7daixie.com
站长地图
程序辅导网!