after that run Client.java and give a IP address using command line.
Server.java
/**
*
* @author janith
*/
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException{
int PORT = 8134;
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message="";
String received="";
System.out.println("Chat Server Started");
ServerSocket sock = new ServerSocket(PORT);
Socket conn = sock.accept();
do{
inStream = conn.getInputStream ();
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
System.out.println("Client sent: "+message);
DataInputStream dis = new DataInputStream(System.in);
message = dis.readLine();
outStream = conn.getOutputStream();
outDataStream = new DataOutputStream (outStream);
System.out.println("Enter your message here: ");
outDataStream.writeUTF(message);
}while(!message.equals("bye"));
conn.close();
}
}
Client.java
/**
*
* @author janith
*/
import java.io.*;
import java.net.*;
public class Client{
public static void main(String[] args) throws IOException {
int PORT = 8134;
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message = "";
InetAddress host = InetAddress.getLocalHost();
String diffHost = args[0];
Socket sock = new Socket(diffHost,PORT);
System.out.println("Chat Client Started");
do{
System.out.println("Enter your message here: ");
DataInputStream dis = new DataInputStream(System.in);
message = dis.readLine();
outStream = sock.getOutputStream();
outDataStream = new DataOutputStream (outStream);
outDataStream.writeUTF(message);
inStream = sock.getInputStream ();
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
System.out.println("Server Sent: "+message);
}while(!message.equals("bye"));
}
}
No comments:
Post a Comment