This is a Client-Server Chat Application, written in java, using sockets. Using this one can connect 2 people/computers/terminals.
Requirements- java (jre / java runtime environment)
Usage-
The Server program is named as MyServer.class and the Client program is named as MyClient.class
The Server needs to be started first. It can be started by typing MyServer in the terminal, inside the folder where the MyServer.class file is present:
$ java MyServer
Then the Client can be started similar to the server, like:
$ java MyClient
By defualt, when no arguments are specified, both the client and server are assumed to be on the same system/host, and the port 1025 is used for communication.
Different ports for the server can be used as:
$ java MyServer 2055
Then to connect the client to the server:
$ java MyClient 2055
This assumes that the server is on the same system/host as the client; if the server is located elsewhere, one can use the form:
$ java MyClient 192.168.1.4
Here 192.168.1.4 is the IP address of the server.
If you get a "Connection refused" exception, then perhaps you have given the wrong IP or port address.
When connecting from different machines, the ports for the communication should be available; otherwise you could get a "Connection timed out" exeception.
Code-
The code is included along with the class files, as MyServer.java and MyClass.java, and is licensed under the GNU GPL.
If the class files are not running on your system, or you want to modify the code, one can use-
$ javac MyServer.java
and
$ javac MyClient.java
to compile the code to class files (requires jdk / java development kit).
Note-
This application is not multithreaded, so at a time only a single client can chat with the server.
If two people want to communicate using this application, one can run the server, and the other can run the client, giving the IP of the server.
If the 2nd person wants to initiate the chat, then he can start the server, and the 1st person can run the client.
Demo-
a) Starting the server-
Press Ctrl^C or Alt+F4 to quit this application.
Server- Using port: 1025 Waiting for connection from Client..
b) Starting the client on another terminal and typing a message-
Press Ctrl^C or Alt+F4 to quit this application.
Client- Using port: 58541 Successfully connected to Server. IP: /127.0.0.1 Port: 1025 Name: localhost
Enter message (Type /y to terminate the message) : Hello /y
Waiting for response...
c) The server gets the clients message and responds-
Connection received from client. IP: /127.0.0.1 Port: 59048 Name: localhost
Waiting for response...
CLIENT-> Hello
Enter message (Type /y to terminate the message) : Hi /y
Waiting for response...
d) The client receives the message-
SERVER-> Hi
Enter message (Type /y to terminate the message) : How are you? /y
Waiting for response...
e) And so on..
On pressing Ctrl^C, the application quits.