讲解:C Assignment for Session 1Java

Introduction已经完成所有要求的功能运行方式见题目具体函数大致功能见注释整体结构Server:主线程:用来监听socket链接,当客户端连接进来后创建单独的子线程为客户端服务子线程:用来处理各个客户端的消息Client:主线程:创建监听线程,然后获取用户输入命令,然后处理各项命令对服务器链接并创建对服务器返回消息的处理线程如果需要对其他客户端建立链接,则会对其他客户端创建返回消息处理线程 (主动链接其他客户端)监听线程:等待其他客户端的链接(用于private message),链接后创建返回消息的处 理线程(被其他客户端链接)返回消息处理线程:接受并处理服务器以及链接的其他客户端的消息消息的通信格式首先是4个字节的数据Len,表示后续数据具体的长度然后是Len字节的数据这样做是防止在tcp通信过程中相邻两个数据包粘包,导致消息不能区分重点数据结构解释struct socket_info{int fd; socket套接字struct sockaddr_in addr; 具体地址};struct user_data{char Name[30]; 用户名char Psd[30]; 密码struct socket_info Con; 建立链接的具体信息int State; -1表示从未登陆 0表示已经下线 1表示在线int Block; 0表示没有由于多次登录被惩罚 1与0相反struct timeval Time; 在block=1时有意义,表示惩罚开始的时间struct timeval LogOffTime; 在state不为-1时有意义,表示前一次下线时间int BlackList[100]; 第i个元素0表示把第i个用户没有拉黑 1表示拉黑int Msg_count; 待接收离线消息的总数char Msg[100][1000]; 离线消息char Privte_addr[40]; “ip 端口”用来让别的用户获取,并建立private链接};客户端代码部分:int state[100]; 第i个元素=0表示未占用 =1表示占用中struct socket_info P_List[100];char name_list[100][30];当state[i]=1时P_List[i]和name_list[i]有意义,分别表示建立了private链接的其他客户端的链接信息和用户名Client接受的消息格式分为三种:1、字符M后面接内容:这类消息直接屏幕输出M后的内容(因为服务器范围的大部分消息都不需要处理知识显示,所以服务器返回的大部分消息以及其他服务器发送对的私密消息都是这种格式)2、字符P后面接内容:这类消息表示的是这个socket链接的是其他客户端,内容为对方客户端的用户名3、IP 空格 端口号 空格 用户名:这类消息在客户端请求对别的客户端进行private链接时由服务器返回,表示的是目标客户端的ip,端口,以及用户名具体各功能的处理流程1)登陆:客户端在与服务器建立接连后,发送“名字 密码”格式的数据进行登陆验证服务器在判断后给出以下几种反馈:E1:名字不对E2:密码不对E3:名字不对,最后一次失败,开始惩罚时间E4:密码不对,最后一次失败,开始惩罚时间E5:惩罚时间中E6:该用户已登录OK:登陆成功在用户登陆成功后用户需要向服务器发送自己用于接受其他客户端接入的端口地址然后服务器对所有用户广播该用户登陆成功向该用户发送其离线时服务器缓存的消息2)message客户端发送命令格式M目标用户名 消息服务器在对目标进行判断后(是否存在以及是否把发送端拉黑)决定是否向目标用户发送消息(M消息),如果不发送则向发送端返回具体错误信息(M错误信息)如果可以发送但用户不在线,则服务器缓存下来,在目标用户上线后,发送给目标用户3)broadcast客户端发送命令格式B消息处理过程类似于message,不过是需要向所有在线用户发送,发送同样需要判断对方是否把发送端拉黑,如果有拉黑的用户,则需要向发送端反馈信息4)whoelse客户端发送命令格式W服务器查找当前所有在线的用户,将名字逐条发送给客户端5)whoelsesince客户端发送命令格式S秒数服务器查找当前所有在线的用户以及不在线并且前一次下线时间在S秒之类的用户,将名字逐条发送给客户端6)block客户端发送命令格式L用户名服务器需要判断该用户名是否存在,以及是否为发送方本人,以及是否已经拉黑,然后进行具体的拉黑处理再将结果发送给客户端7)unblock处理过程与上面类似8)startprivate客户端发送命令格式P用户名服务器判断用户名是否为发送方本人,如果不是且目标存在,则返回目标的ip,端口,名字,否则返回具体的错误消息客户端在收到具体的ip 端口 名字后,去向客户端B建立链接,同时向客户端B发送命令:P自己的名字这样客户端B就知道是谁与自己建立的链接9)private客户端判断是否与目标客户端已经有了链接,如果有,则向目标客户端发送M消息10)stopprivate客户端判断是否与目标客户端已经有了链接,如果有,则断开与目标客户端的链接Requirement1COMP3331/9331 Computer Networks and ApplicationsAssignment for Session 1, 2017Version 2.0Due: 11:59pm Friday, 28 April 2017 (Week 8)Mandatory Demo: Your regular lab slot (Week 9)1. Change LogVersion 1.0 released on 20 th March 2017.Version 2.0 released on 10 th April 2017.Added a command to close the p2p messaging session. This only applies for the extended version.2. Goal and learning objectivesInstant messaging applications such as WhatsApp, WeChat, Facebook Messenger, etc. are widelyused with millions of subscribers participating in them globally. In this assignment, you will havethe opportunity to implement your own version of an instant messaging application. In addition tobasic messaging functionality, you will also implement many additional services that are availablein many of the aforementioned applications. Your application is based on a client server modelconsisting of one server and multiple messaging clients. The clients communicate with the serverusing TPC. The server is mainly used to authenticate the clients and direct the messages (online oroffline) between clients. Besides, the server also has to support certain additional functions(presence notification, blacklisting, timeout, etc.). The extended version asks you to implementpeer-to-peer messaging that bypasses the server.2.1 Learning ObjectivesOn completing this assignment, you will gain sufficient expertise in the following skills:1. Detailed understanding of how instant messaging services work.2. Expertise in socket programming.3. Insights into designing an application layer protocol.3. Assignment SpecificationThere are two versions of this assignment, a standard version (with a total of 13 marks) and anextended version (with a total of 15 marks of which 2 marks are bonus marks). TheUpdates to the assignment, including any corrections and clarifications, will be posted on thesubject website. Please make sure that you check the subject website regularly for updates.2specifications for the extended version can be found in Section 5 of the specification. Note that thebonus marks may not be proportional to the amount of extra work that you will have to do. They arethere to encourage you to go beyond the standard assignment. The bonus marks can be used tomake up for lost marks in the lab exercises and the second programming assignment but NOT forany of the exams (mid-session and final).The assignment includes 2 major modules, the server program and the client program. The serverprogram will be run first followed by multiple instances of the client program (Each instancesupports one client). They will be run from the terminals on the same and/or different hosts.For the standard version, all messages between clients MUST be sent via the server. The extendedversion asks you implement additional functionality whereby two users can exchange messageswith each other directly (i.e. bypassing the server) in a peer-to-peer fashion.4.1. ServerThe server has the following responsibilities -User Authentication - When a new client requests for a connection, the server should prompt theuser to input the username and password and authenticate the user. The valid username andpassword combinations will be stored in a file called credentials.txt which will be in the samedirectory as the server program. An example credential.txt file is provided on the assignment page.Username and passwords are case-sensitive. We may use a different file for testing so DO NOThardcode this information in your program. You may assume that each user name and passwordwill be on a separate line and that there will be one white space between the two. If the credentialsare correct, the client is considered to be logged in (i.e. online) and a welcome message isdisplayed. When all messaging is done, a user should be able to logout from the server.On entering invalid credentials, the user is prompted to retry. After 3 consecutive failed attempts,the user is blocked for a duration of block_duration seconds (block_duration is a command lineargument supplied to the server), and cannot login during this duration (even from another IPaddress). While a user is online, if someone uses the same username/password to log in (even fromanother IP address), then this new login attempt is denied. If the username is invalid then blockaccess from that IP address for block_duration.Timeout - The server should keep track of all online users. If the server does not receive anycommands from a user for a period of timeout seconds (timeout is a command line argumentsupplied to the server), then the server should automatically log this user out. Note that, to beconsidered active, a user must actively issue a command. The receipt of a message does not count.Presence Broadcasts - The server should notify the presence/absence of other users logged into theserver, i.e. send a broadcast notification to all online users when a user logs in and logs out.List of onliC代写 Assignment for Session 1代写留学生Java语言ne users - The server should provide a list of users that are currently online in responseto such a query from a user.Online history – The sever should provide a list of users that logged in for a user specified time inthe past (e.g. users who logged in within the past 15 minutes).Message Forwarding - The server should forward each instant message to the correct recipientassuming they are online.3Offline Messaging - When the recipient of a message is not logged in (i.e. is offline), the messagewill be saved by the server. When the recipient logs in next, the server will send all the unreadmessages stored for that user (timestamps are not required).Message Broadcast – The server should allow a user to broadcast a message to all online users.Offline messaging is not required for broadcast messages.Blacklisting - The server should allow a user to block / unblock any other user. For example, if userA has blocked user B, B can no longer send messages to A i.e. the server should intercept suchmessages and inform. B that the message cannot be forwarded. Blocked users also do not getpresence notifications i.e. B will not be informed each time A logs in or logs out.4.2. ClientThe client has the following responsibilities -Authentication - The client should provide a login prompt to enable the user to authenticate withthe server.Message - The client should allow the user to send a message to any other user and displaymessages sent by other users. The client should also allow the user to send a broadcast message toall online users.Notifications - The client should display presence notifications sent by the server about userslogging in and out from the server.Find users online - The client should provide a way for the user to obtain a list of all the userscurrently online from the server.Find online history – The client should provide a way for the user to obtain a list of all users whohad logged in within a user specified time period.Blacklist – The client should allow a user to block a user from sending any further messages. Theclient should also allow a user to unblock a user that was earlier blocked.4.3 Commands supported by the clientAfter a user is logged in, the client should support all the commands shown in the table below. Forthe following, assume that commands were run by user A.Command Descriptionmessage Send to through the server. If the user isonline then deliver the message immediately, else store themessage for offline delivery. If has blocked A, then amessage to that effect should be displayed for A. If the isnot present in the credentials file (i.e. invalid user) or is self (A)then an appropriate error message should be displayed. Theused in our tests will be a few words at most.broadcast Send to all online users except A and those users whohave blocked A. Inform. A that message could not be sent to some4recipients.whoelse This should display the names of all users that are currentlyonline excluding A. Users can be displayed in any order.whoelsesince This should display the names of all users who were logged in atany time within the past seconds excluding A. Note thatthis, may include users that may currently be offline. If isgreater than the time since when the server has been running, thenall users who logged in since the sever initiation time should belisted. This suggests that you will need to keep a login historysince the start of the server. Users can be displayed in any order.block blocks the from sending messages to A. A message shouldbe displayed to A confirming the blocking action. If is self(i.e., A) or is invalid, then an appropriate error message should bedisplayed. must not be informed that A has blocked them.unblock unblocks the who has been previously blocked by A. Amessage should be displayed to A confirming the unblockingaction. If is self (i.e., A) or is invalid or was not alreadyblocked, then an appropriate error message should be displayed.logout log out user A.Any command that is not listed above should result in an error message being displayed to the user.The interaction with the user should be via the terminal (i.e. console). All messages must bedisplayed in the same terminal. There is no need to create separate terminals for messaging withdifferent users.We do not mandate the exact text that should be displayed by the client to the user for the variousmessages. However, you must make sure that the displayed text is easy to comprehend. Pleasemake sure that you DO NOT print any debugging information on the client terminal.We also prefer that you do not print anything at the terminal running the server. We suggest thatyou use an optional debug flag (e.g. –d) for the server. When this flag is turned on, your server canprint debugging information to the terminal.Some examples illustrating client server interaction using the above commands are provided inSection 10.4.4 File Names ExecutionThe main code for the server and client should be contained in the following files: server.c, orServer.java or server.py, and client.c or Client.java or client.py. You arefree to create additional files such as header files or other class files and name them as you wish.The server should accept the following three arguments:• server_port : this is the port number which the server will use to communicate with theclients. Recall that a TCP socket is NOT uniquely identified by the server port number. So itis possible for multiple TCP connections to use the same server-side port number.5• block_duration : this is the duration in seconds for which a user should be blocked afterthree unsuccessful authentication attempts.• timeout : this is the duration in seconds of inactivity after which a user is logged off by theserver.The server should be executed before any of the clients. It should be initiated as follows:If you use Java:java Server server_port block_duration timeoutIf you use C:./server server_port block_duration timeoutIf you use Python:python server.py server_port block_duration timeoutThe client should accept the following two arguments:• server_IP : this is the IP address of the machine on which the server is running.• server_port : this is the port number being used by the server. This argument should bethe same as the first argument of the server.Note that, you do not have to specify the port to be used by the client. You should allow the OS topick a random available port. Each client should be initiated in a separate terminal as follows:If you use Java:java Client server_IP server_portIf you use C:./client server_IP server_portIf you use Python:python client.py server_IP server_portNote: When you are testing your assignment, you can run the server and multiple clients on thesame machine on separate terminals. In this case, use 127.0.0.1 (local host) as the server IP address.5. Extended VersionSome users may prefer to have some privacy during messaging. They may want to message theirfriends directly without all their conversation being routed via the server. A peer-to-peer messagingclient is a good solution for this scenario. In addition to the above functionalities, the extendedversion should implement peer-to-peer messaging (also referred to as private messaging).The client for the extended version should support the following commands (in addition to thoselisted in Section 4.3)Command Descriptionstartprivate This command indicates that user A wishes to commence p2pmessaging with . The client should obtain the IP addressand port number being used by the from the server. Theclient should establish a TCP connection to this IP address andport number combination. A confirmation message should be转自:http://ass.3daixie.com/2019031241076350.html

你可能感兴趣的:(讲解:C Assignment for Session 1Java)