In the previous article,
“Broadcasting audio over the network using Java”, I described a method for receiving and broadcasting audio over the network using the built-in Java tools.
Here I will continue to develop this idea, and tell you how to make a simple IP-telephony system using Java.
The IP telephony system consists of the server part, which stores user credentials and their current IP, and a console client, which is able to make and receive calls directly from the second subscriber.
')
Fully source can be
viewed on github .
All interested in asking under the cat.
NetworkingAudioServer - server part of the system
The server part is based on servlets, Apache Tomcat and MySQL.
Database structure
The database consists of two tables:
users - stores user credentials and
userinfo - maps each user IP and the time it was last updated.
CREATE TABLE IF NOT EXISTS `users` ( `email` varchar(100) NOT NULL PRIMARY KEY, `password` varchar(100) NOT NULL, `confirm` varchar(100) NOT NULL, `user_id` varchar(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The
email and
password names are self-
explanatory ,
confirm — the confirmation email address token or
done , if the e-mail is confirmed,
user_id is the md5 hash from the e-mail address (since it has a fixed length, it is convenient to use it to identify an incoming call ).
CREATE TABLE IF NOT EXISTS `userinfo` ( `email` varchar(100) NOT NULL PRIMARY KEY, `user_id` varchar(100) NOT NULL, `ip` varchar(100) NOT NULL, `last_update` TIMESTAMP NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The
email and
user_id fields correspond to the fields from the previous table, what
ip is clear from the name,
last_update is the time of the last IP update.
Servlets
RegisterServlet registers a user in the database and sends an e-mail confirmation request using the following script:
ConfirmServlet - performs the confirmation e-mail.
UpdateIPServlet - updates the client's IP when prompted, the IP is determined automatically.
GetUserInfoServlet - takes an
email or
user_id as a parameter, returns an
email or
user_id when requested, or the current IP, or a value
'offline' if the IP has not been updated for more than three minutes.
Configuring the server side
To install the server part you need:
- Create MySQL Database
- Specify the correct values of the login, password and MySQL database in the class MyDBConnect
- Build up and cover up on Tomcat
- Specify the correct SERVER_URL value in the mail sending script, place the script itself at / common_scripts / sendConfirm and set execution rights
- Create a folder / common_scripts / tmp and set write permissions
jNetworkingAudioClient - console client
Customer structure
The console client consists of four classes with program logic —
Main ,
Master ,
Slave and
IPUpdater , and two auxiliary classes —
Util and
DeclinedException .
The
Util class stores client settings — such as sound settings and buffer size.
The
Main class is responsible for the program logic of the interface.
The
IPUpdater class sends the IP update request to the servlet every 90 seconds.
The
Master class listens to the network port and, in turn, contains two nested stream classes:
MicrophoneReader - reads data from the microphone and
Sender - sends data to the second subscriber.
Slave class: initiates the connection by sending the second subscriber the md5-hash of the email address, then, if the call is accepted, starts reading the data from the socket and sending it to the audio output.
I’ll probably not describe the client’s device in more detail - everyone can get acquainted with the source code themselves.
Client launch
Here you can take a ready-made executable jar.
Run:
java -jar jNetworkingAudioClient.jar http://serverUrl 2>log.txt
Those interested can try on my server:
java -jar jNetworkingAudioClient.jar http://tabatsky.ru/networkingaudio/ 2>log.txt
But: with a view of habraeffektivnost server I set a limit of 100 registered users.
One last thing: if you are sitting behind a router, you need to
redirect port 7373 to your car.
UPD:
There was a small problem - when I try to update my own IP through an external host, the servlet instead of my external IP gets the IP of my router on the internal network - 192.168.1.1.
I came up with a small crutch - a trigger for MySQL:
CREATE TRIGGER `my_host_ip` BEFORE INSERT ON `userinfo` FOR EACH ROW SET NEW.ip=IF(NEW.ip='192.168.1.1','tabatsky.ru',NEW.ip);