首页 > > 详细

C代写 COMP9331 Computer Networks and Applications代写Java编程

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
客户端判断是否与目标客户端已经有了链接,如果有,则断开与目标客户端的链接
Requirement
1
COMP3331/9331 Computer Networks and Applications
Assignment for Session 1, 2017
Version 2.0
Due: 11:59pm Friday, 28 April 2017 (Week 8)
Mandatory Demo: Your regular lab slot (Week 9)
1. Change Log
Version 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 objectives
Instant messaging applications such as WhatsApp, WeChat, Facebook Messenger, etc. are widely
used with millions of subscribers participating in them globally. In this assignment, you will have
the opportunity to implement your own version of an instant messaging application. In addition to
basic messaging functionality, you will also implement many additional services that are available
in many of the aforementioned applications. Your application is based on a client server model
consisting of one server and multiple messaging clients. The clients communicate with the server
using TPC. The server is mainly used to authenticate the clients and direct the messages (online or
offline) between clients. Besides, the server also has to support certain additional functions
(presence notification, blacklisting, timeout, etc.). The extended version asks you to implement
peer-to-peer messaging that bypasses the server.
2.1 Learning Objectives
On 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 Specification
There are two versions of this assignment, a standard version (with a total of 13 marks) and an
extended version (with a total of 15 marks of which 2 marks are bonus marks). The
Updates to the assignment, including any corrections and clarifications, will be posted on the
subject website. Please make sure that you check the subject website regularly for updates.
2
specifications for the extended version can be found in Section 5 of the specification. Note that the
bonus marks may not be proportional to the amount of extra work that you will have to do. They are
there to encourage you to go beyond the standard assignment. The bonus marks can be used to
make up for lost marks in the lab exercises and the second programming assignment but NOT for
any of the exams (mid-session and final).
The assignment includes 2 major modules, the server program and the client program. The server
program will be run first followed by multiple instances of the client program (Each instance
supports 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 extended
version asks you implement additional functionality whereby two users can exchange messages
with each other directly (i.e. bypassing the server) in a peer-to-peer fashion.
4.1. Server
The server has the following responsibilities -
User Authentication - When a new client requests for a connection, the server should prompt the
user to input the username and password and authenticate the user. The valid username and
password combinations will be stored in a file called credentials.txt which will be in the same
directory 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 NOT
hardcode this information in your program. You may assume that each user name and password
will be on a separate line and that there will be one white space between the two. If the credentials
are correct, the client is considered to be logged in (i.e. online) and a welcome message is
displayed. 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 line
argument supplied to the server), and cannot login during this duration (even from another IP
address). While a user is online, if someone uses the same username/password to log in (even from
another IP address), then this new login attempt is denied. If the username is invalid then block
access from that IP address for block_duration.
Timeout - The server should keep track of all online users. If the server does not receive any
commands from a user for a period of timeout seconds (timeout is a command line argument
supplied to the server), then the server should automatically log this user out. Note that, to be
considered 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 the
server, i.e. send a broadcast notification to all online users when a user logs in and logs out.
List of online users - The server should provide a list of users that are currently online in response
to such a query from a user.
Online history – The sever should provide a list of users that logged in for a user specified time in
the past (e.g. users who logged in within the past 15 minutes).
Message Forwarding - The server should forward each instant message to the correct recipient
assuming they are online.
3
Offline Messaging - When the recipient of a message is not logged in (i.e. is offline), the message
will be saved by the server. When the recipient logs in next, the server will send all the unread
messages 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 user
A has blocked user B, B can no longer send messages to A i.e. the server should intercept such
messages and inform. B that the message cannot be forwarded. Blocked users also do not get
presence notifications i.e. B will not be informed each time A logs in or logs out.
4.2. Client
The client has the following responsibilities -
Authentication - The client should provide a login prompt to enable the user to authenticate with
the server.
Message - The client should allow the user to send a message to any other user and display
messages sent by other users. The client should also allow the user to send a broadcast message to
all online users.
Notifications - The client should display presence notifications sent by the server about users
logging 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 users
currently online from the server.
Find online history – The client should provide a way for the user to obtain a list of all users who
had logged in within a user specified time period.
Blacklist – The client should allow a user to block a user from sending any further messages. The
client should also allow a user to unblock a user that was earlier blocked.
4.3 Commands supported by the client
After a user is logged in, the client should support all the commands shown in the table below. For
the following, assume that commands were run by user A.
Command Description
message Send to through the server. If the user is
online then deliver the message immediately, else store the
message for offline delivery. If has blocked A, then a
message to that effect should be displayed for A. If the is
not present in the credentials file (i.e. invalid user) or is self (A)
then an appropriate error message should be displayed. The
used in our tests will be a few words at most.
broadcast Send to all online users except A and those users who
have blocked A. Inform. A that message could not be sent to some

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

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