简体   繁体   中英

Client Server LAN Messenger in Java

I am creating an Intra Office Messaging System in Java using client-server architecture. The features I want to implement are: real-time chatting and messaging to a specific user or group of users, file transfer and voice-chat.

I have implemented the server and the client module using which the client can login on the server. I have used DataInputStream and DataOutputStream for this. When user submits username and password, I store them in a single string separated by a semi-colon ";" and then I send this string to server using DataInputStream where I separate them and run a DB query and send appropriate acknowledgement to the client application.

Now I want to implement chatting and messaging. My question is, shall I use the same approach for this? Or is there a better solution? Also, how can I send the message to specific client(s) (client A wants to send message only to client B). While suggesting a solution, please keep in mind that I have to implement voice chat as well (gstreamer) as filetransfer!!!

Also, I maintain an array containing names of all clients who log-in to the server which is used to display the list of logged-in clients to each client.

Firstly, by sending the username and password as a plain-text string, you're practically giving them away freely - anyone with a few basic tools can sniff the username and passwords. You are going to need to read up on cryptography and how you can secure the connections. Java has a built-in cryptography library which makes this very easy to do.

If possible, I would recommend going for an already developed chat protocol like XMPP (Jabber), for which there already exist many free Java library implementations, such as Smack, that will do everything for you. There really shouldn't be any need to re-invent the wheel here unless you are doing this for a school project that doesn't allow any use of external libraries, which would be extremely ambitious in of itself. XMPP has support for text chat, voice chat, and file transfers.

There are also already several fully featured open-source chat clients that you could modify to suite your specific needs. One thing to keep in mind though, is the licensing on open-source projects. Some open-source licenses, like the popular GPL, require that using any part of the open-source project in your project requires that you release the source code for your entire project. This could be extremely disastrous for a corporation, so watch out.

If you still want to start from scratch, then you'll need to implement your own protocol of communication. You'll have to design this yourself, while taking into consideration how you'll incorporate gstreamer and file transfers.

Again, I would recommend at least looking at some already designed protocols, like XMPP, to get some ideas.

Usually, protocols have,

  • Some data explaining what type of request/response this is. This could be a numeric value stored as a single byte, or some text string like as is done in HTTP
  • Some more data pertaining to who the message is for. Could be an IP address, username, combination of both, etc. Not necessary if you're doing the communication directly, ie not through a middle-man server
  • The time the request was sent
  • The data itself
  • Some sort of encryption. Best initiated after the user was authorized

For example, a really basic protocol could be,

Request type: 1 byte. 1 = text, 2 = voice data, 3 = file transfer, 4 = request for currently logged-in client list

Destination: int (IP address)

Time: long. Best to send this as UTC time, e.g. what System.currentTimeMillis returns

Length of data: int

Data: variable length data, depending on type

Then, for each type of data being sent, you'd implement the data differently,

Text: string as sent by DataStream

Voice: voice data from gstreamer (not sure how gstreamer works)

File transfer:

    File name: String as sent by DataStream

    Length: long

    Data: As read from FileInputStream

List of currently-logged in clients:
    Data: as sent by DataStream.writeObject

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM