首页 > > 详细

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
联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp
热点文章
程序代写更多图片

联系我们 - QQ: 99515681 微信:codinghelp
© 2014 www.7daixie.com
程序代写网!