There are different languages where you can create a chat, and has its own capabilities how to work with. In this tutorial it is based on java using command line, a simple chat where you might communicate client-server connection.
Unfortunately, my sample program shows a simple chat in java. Now, let us overview what is IP (Internet Protocol), each machine has one or more IP address and defines how to get data packets across the internet from one to another. However, you need client program communicates with server program not a client machine communicates with the server machine. Within existence of Java and the internet components had early coverage of TCP/IP. For now, we use a default address which is locahost or 127.0.0.1.
Unfortunately, my sample program shows a simple chat in java. Now, let us overview what is IP (Internet Protocol), each machine has one or more IP address and defines how to get data packets across the internet from one to another. However, you need client program communicates with server program not a client machine communicates with the server machine. Within existence of Java and the internet components had early coverage of TCP/IP. For now, we use a default address which is locahost or 127.0.0.1.
Here are the sample codes:
The serve code
import java.net.*;
import java.io.*;
public class Server
{
static ServerSocket server;
static Socket client;
static DataInputStream in;
static String message;
public static void main (String [] args)throws IOException
{
System.out.println("starting server");
server = new ServerSocket(3306);
System.out.println("Server started");
while(true)
{
client = server.accept();
System.out.println("Connection");
in = new DataInputStream(client.getInputStream());
message = in.readUTF();
System.out.println("Recieved: " + message);
}
}
}
The client code:
import java.net.*;
import java.io.*;
import java.util.*;
public class Client
{
static Scanner console = new Scanner(System.in);
static Socket socket;
static DataOutputStream out;
static String message;
public static void main (String[] args)throws IOException, UnknownHostException
{
System.out.println("trying to connect..");
socket = new Socket("localhost",3306);
System.out.println("connected");
System.out.println("Sent message now");
message = console.next();
out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(message);
System.out.println("Sent to Server: " + message);
System.out.println();
}
}
Now, you’ll already do coding with client and server. To enable to communicate with server and client, you are going to create a .bat files both client and server to run you program. To do that, open a notepad and type the following code for client.
@echo off
java Client
pause
Note: make sure you save the file extension .bat. Example Client.bat
The java Client above is the name of your java Class.
The same procedure for the server, again open notepad and type the following code for server.
@echo off
java Server
pause
Note: make sure you save the file extension .bat. Example Server.bat
The java Server above is the name of your java Class.
To run the program, first run the server before client with the .bat file extension.
Note: This is a sample chat program created in java, if you are going to work with and expand this tutorial, that’s great!
Next: <<next vs. nextLine in Java | do and while loop>>
0 comments:
Post a Comment