首页
编程语言
数据库
网络开发
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
热点文章
更多
辅导 cs1b spring 2024 tth hw...
2024-04-19
讲解 managing financial risk...
2024-04-19
辅导 cs 0449 – project 5: /...
2024-04-19
辅导 elec 2141 digital circu...
2024-04-19
讲解 csc171 — videogame pro...
2024-04-19
讲解 comp3411 artificial int...
2024-04-19
讲解 stat3061: random proces...
2024-04-19
辅导 accounting 452, spring ...
2024-04-19
辅导 finc5001 foundations in...
2024-04-19
辅导 7ssmm712 – topics in a...
2024-04-19
讲解 com 337 - film studies ...
2024-04-19
辅导 mes202tc - digital vlsi...
2024-04-19
辅导 geography 2041b distanc...
2024-04-19
辅导 ecos3006 international ...
2024-04-19
讲解 fit5225 2024 sm1 creati...
2024-04-19
讲解 cit 593: introduction t...
2024-04-19
讲解 math 4931: take home ex...
2024-04-19
辅导 csci 547|info 533: syst...
2024-04-19
辅导 cs536-s24 intro to pls ...
2024-04-19
讲解 fit5212 - assignment 1辅...
2024-04-19
热点标签
com6521
can202
cs 6476
basc0003
ee3121
acct2002
comp5313
busi2131
ise529
csit940/csit440
cenv6141
comp3027/comp3927
ftec5580
comp1433
msci223
mark203
en3098
eden1000
ece6483
econ4410
mats16302
elec372/472
ecmt1010
comp222
comp3211
engg1340
fit5216
csc1002
comp10002
cmpsc311
chc6186
econ3208
cs 161
comp27112
comp282
swen20003
comm1190
elec9764
acfi3308
acct7101
fin6035
comp2048
geog0163
comp2013
coen 146
dts101tc
sehh2042
comp30023
comp4880/8880
cs 455
07
stat0045.
fil-30023
celen085
psyc40005
math40082
are271
comp9311
ee5311
imse2113
comp 2322
acct2102
fnd109
int102
is3s664
is6153
data4000
accfin5034
fit5212
cs536-s24
fit5225
ecos3006
mes202tc
finc5001
stat3061
csc171
cs1b
7ssmm712
bu.450.760
comp3411
cs170
swen90004
cpt206
comp5313/comp4313—large
bl5611
kxo206
comp532
elec207
kxo151
cs 2820
cpt108
math2319
dts204tc
qm222
comp2511
ccs599
infs1001
mat2355
eeee4123
25721
ifn647
pols0010
hpm 573
stat0023
csci 1100
qbus6860
comp9417
comp2003j
cse340
cs 2550
cs 61b
cs360
fin 3080
ierg 4080
cs6238
cit 594
finm7406
hw6
elec9713
asb-2522
mso3610
lit301
mcd4540
geog0030
125.330
biol0006
125.320
cs3334
fit2093
acct1101
110.309
masy1-gc
cs314
elec0048
gds104
mg5637
fit2096
math5905
eel4837
sehs4515
cpt s 321
asb2522 investment
ma214
co2104
mgmt2015
32516
math32051
econ1012
mark2052
econ0019
comp3310
dsci 525
abmf3184
aps106
antc27
finm7401
itp122
tech2300
math3026
comp9024
cao107
36318
is2022
cs 211
fit1047
ics4u
2xc3
en.540.635
4qqmn506
finn3081
phys10362
sta601
ec481e
math5165
csi 2120
el1205
comp7250
ecos3013
beam065
info1113
comp2051
csc325
mne 6130
ai6126
ecs150
is61x6
cse115
seng6110
bus265
cpts260
mphy0009
csc306
eco2011
ee3004
inu1111
st332
idepg001
info6001
cpt106
finm7409
联系我们
- QQ: 99515681 微信:codinghelp
© 2024
www.7daixie.com
站长地图
程序辅导网!