Java curriculum design team -socket chat room (personal summary)

Java curriculum design team -socket chat room (personal summary)

First, the curriculum design team blog link

https://www.cnblogs.com/haijie-wrangler/p/12169314.html

Second, I am responsible for the module or mission statement

Task 1 The server to accept the socket threads as well as data on the client's forwarding operations
Task 2 Find the database, add, delete, change operations

Third, submit their own code records

Note: Only part of the submitted records, detailed records submitted, please visit the team blog

Fourth, responsible for their own module or task details

1, the server need to define the definition of a server-side control object to handle user login and registration, and the process of creating a user thread to complete the task of accessing and forwarding messages

First, the user needs to send the user login interface registration information or the user login information to the server, the server needs to determine if the receiving user login information, the database information matches, return a successful login (Object sent encapsulated) return to offline messaging, information will be sent home for the historical information, and to join a thread that connected the HashMap (easy to find the recipient to forward the thread), and create user threads to transmit messages online chat service. If the received user registration information, the user information newly registered insert (including account and password) in the user table in the database, returns a registration success (encapsulated Object).

The main method of circulating listen for user connections, and determine whether the user is required to register a new connection or login

2. The need to create the server thread to service the requirements of the client sends to respond

The loop thread needs to listen for client requests (Object encapsulated transmission), the request for the data type is determined

If the client requests are forwarded online message, then:

Find recipient thread has a corresponding thread connection, forwarding information. At the same time, the appropriate information into the database and returns a boolean variable indicates forwarding is successful.

If the client requests are forwarded file, then:

Find the thread in the thread has been the recipient of connection, forwarding documents. It returns a boolean variable indicates whether or not to forward the success (here forward the file can not do off-line forward, the sender and recipient need concurrent)

3, the corresponding database operation

The first is to build the table:

User表:包含所有已注册过的用户的账号和密码信息,若用户注册成功则将改用户账号信息插入到数据库中

每个用户的个人消息表,包含各个其他用户与该用户的聊天信息记录

包含两个标记字段,放置一个Flag字段,Flag的值为1表示是对方发来的信息,值为2表示是该用户发给对方的信息。还有一个Type字段,其值为1表示该信息还没有被改用户读取,为离线消息,值为2表示该信息已被查看,为历史消息

其次,使用jdbc对数据库的操作:

如果对方在线,转发为在线消息,在自己的消息表中将该信息设置为自己为发送方(Flag为2)历史消息(Type为2)。在对方的消息表中将该条信息设置为接收方(Flag为1),并置为历史消息(Type为2)

如果对方不在线,则转发为离线消息,在自己的消息表中将该信息设置为自己为发送方(Flag为2)历史消息(Type为2)。在对方的消息表中将该条信息设置为接收方(Flag为1),并置为历史消息(Type为1)

五、课程设计感想

在本次课程设计中,项目设计的阶段困难重重,socket使用阻塞式的传输方式,一开始,消息传输总有接收方的获取操作和发送方的传送操作对接不上的问题,最后通过使用线程的等待来确保发送和接收方的对接

本次团队合作让我对JAVA面向对象的设计有了更加深刻的理解,项目实现了类似QQ的聊天和传送文件功能,虽然在实现过程出现了很多问题,通过不断的调试最终都解决了!

Guess you like

Origin www.cnblogs.com/huanghongbe/p/12169347.html