This program demonstrate how to establish connection between the client and pass text message between them.
ChatServer.java act as server in the network
import java.net.*;
import java.io.*;
class ChatServer{
public static void main(String[] args) throws IOException{
ServerSocket server = null; //server socket for handling server operation
try{
server = new ServerSocket(90); //establish connection port
}
catch(IOException ioEx){
System.out.println("Error in port 90");
System.exit(1);
}
Socket serverSocket = null; //handle client request
try{
System.out.println("Waiting for client!");
serverSocket = server.accept(); //listen to the client request
System.out.println("connection accepted at " + serverSocket);
}
catch(IOException ioEx){
System.out.println("Failed to Connect");
System.exit(1);
}
//output to the client
DataOutputStream frmServer = new DataOutputStream(serverSocket.getOutputStream());
//get input from client
BufferedReader toServer = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
//get input from keyboard;
BufferedReader kybd = new BufferedReader(new InputStreamReader(System.in));
String clientMsg, serverMsg;
System.out.println("Start Chatting! Type exit to terminate!");
boolean end = false;
do{
clientMsg = toServer.readLine(); //accept client's message;
System.out.println("From Client : " + clientMsg);
//int len = msgClient.length();
//accept msg and send to client
serverMsg = kybd.readLine();
frmServer.writeBytes(serverMsg);
frmServer.write(13);
frmServer.write(10);
frmServer.flush();
if(serverMsg.equals("exit"))
end = true;
}while(!end);
frmServer.close();
server.close();
}
}
import java.net.*;
import java.io.*;
class ChatServer{
public static void main(String[] args) throws IOException{
ServerSocket server = null; //server socket for handling server operation
try{
server = new ServerSocket(90); //establish connection port
}
catch(IOException ioEx){
System.out.println("Error in port 90");
System.exit(1);
}
Socket serverSocket = null; //handle client request
try{
System.out.println("Waiting for client!");
serverSocket = server.accept(); //listen to the client request
System.out.println("connection accepted at " + serverSocket);
}
catch(IOException ioEx){
System.out.println("Failed to Connect");
System.exit(1);
}
//output to the client
DataOutputStream frmServer = new DataOutputStream(serverSocket.getOutputStream());
//get input from client
BufferedReader toServer = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
//get input from keyboard;
BufferedReader kybd = new BufferedReader(new InputStreamReader(System.in));
String clientMsg, serverMsg;
System.out.println("Start Chatting! Type exit to terminate!");
boolean end = false;
do{
clientMsg = toServer.readLine(); //accept client's message;
System.out.println("From Client : " + clientMsg);
//int len = msgClient.length();
//accept msg and send to client
serverMsg = kybd.readLine();
frmServer.writeBytes(serverMsg);
frmServer.write(13);
frmServer.write(10);
frmServer.flush();
if(serverMsg.equals("exit"))
end = true;
}while(!end);
frmServer.close();
server.close();
}
}
ChatClient.java act as client to the network
import java.net.*;
import java.io.*;
class ChatClient{
public static void main(String[] args) throws IOException{
Socket client = null;
BufferedReader toClient = null;
DataOutputStream frmClient = null;
BufferedReader kybd = new BufferedReader(new InputStreamReader(System.in));
try{
client = new Socket(InetAddress.getLocalHost(),90); //send request to server;
toClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
frmClient = new DataOutputStream(client.getOutputStream());
}
catch(UnknownHostException unknwnEx){
System.out.println("Server not Found");
System.exit(1);
}
System.out.println("Start Conversation!");
boolean end = false;
String clientMsg, serverMsg;
do{
//read message from the keyboard;
clientMsg = kybd.readLine();
//send to server;
frmClient.writeBytes(clientMsg);
frmClient.write(13);
frmClient.write(10);
frmClient.flush();
//receive message from server
System.out.println("You: ");
serverMsg = toClient.readLine();
System.out.println("From Server: " + serverMsg);
if(clientMsg.equals("exit"))
end = true;
}while(!end);
toClient.close();
frmClient.close();
client.close();
}
}
import java.net.*;
import java.io.*;
class ChatClient{
public static void main(String[] args) throws IOException{
Socket client = null;
BufferedReader toClient = null;
DataOutputStream frmClient = null;
BufferedReader kybd = new BufferedReader(new InputStreamReader(System.in));
try{
client = new Socket(InetAddress.getLocalHost(),90); //send request to server;
toClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
frmClient = new DataOutputStream(client.getOutputStream());
}
catch(UnknownHostException unknwnEx){
System.out.println("Server not Found");
System.exit(1);
}
System.out.println("Start Conversation!");
boolean end = false;
String clientMsg, serverMsg;
do{
//read message from the keyboard;
clientMsg = kybd.readLine();
//send to server;
frmClient.writeBytes(clientMsg);
frmClient.write(13);
frmClient.write(10);
frmClient.flush();
//receive message from server
System.out.println("You: ");
serverMsg = toClient.readLine();
System.out.println("From Server: " + serverMsg);
if(clientMsg.equals("exit"))
end = true;
}while(!end);
toClient.close();
frmClient.close();
client.close();
}
}
No comments:
Post a Comment