Write a socket program in java for simple stand alone chatting application.
Client side
Import java.io.*;
Import java.net.*;
public class Chatclient
{
Public static void main(String args[])
{
Boolean b = true;
try
{
Socket s1 = new Socket(“localhost”,40);
DataInputStream in1 = new DataInputStream(s1.getInputStream());
DataInputStream out1 = new DataInputStream(s1.getOutputStream());
printStream pw = new printStream(out1);
System.out.println(“Send Message”);
While(b)
{
DataInputStream in = new DataInputStream(System.in);
String str1 = in.readLine();
If(str1.equals(“quit”))
b=false;
else
{
pw.println(str1);
String str2 = in1.readLine();
System.out.println(str2);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
Server side
Import java.io.*;
Import java.net.*;
public class Chatserver
{
Public static void main(String args[])
{
Boolean b = true;
try
{
ServerSocket s = new ServerSocket(40);
ServerSocket s1 = s.accept();
DataInputStream in1 = new DataInputStream(s1.getInputStream());
DataOutputStream out1 = new DataOutputStream(s1.getOutputStream());
printStream pw = new printStream(out1);
while(b)
{
String str1 = in1.readLine();
If(str1.equals(“quit”))
b =false;
else
{
System.out.println(str1);
DataInputStream in = new DataInputStream(System.in);
String str = in.readLine();
pw.println(str2);
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
Import java.io.*;
Import java.net.*;
public class Chatclient
{
Public static void main(String args[])
{
Boolean b = true;
try
{
Socket s1 = new Socket(“localhost”,40);
DataInputStream in1 = new DataInputStream(s1.getInputStream());
DataInputStream out1 = new DataInputStream(s1.getOutputStream());
printStream pw = new printStream(out1);
System.out.println(“Send Message”);
While(b)
{
DataInputStream in = new DataInputStream(System.in);
String str1 = in.readLine();
If(str1.equals(“quit”))
b=false;
else
{
pw.println(str1);
String str2 = in1.readLine();
System.out.println(str2);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
Server side
Import java.io.*;
Import java.net.*;
public class Chatserver
{
Public static void main(String args[])
{
Boolean b = true;
try
{
ServerSocket s = new ServerSocket(40);
ServerSocket s1 = s.accept();
DataInputStream in1 = new DataInputStream(s1.getInputStream());
DataOutputStream out1 = new DataOutputStream(s1.getOutputStream());
printStream pw = new printStream(out1);
while(b)
{
String str1 = in1.readLine();
If(str1.equals(“quit”))
b =false;
else
{
System.out.println(str1);
DataInputStream in = new DataInputStream(System.in);
String str = in.readLine();
pw.println(str2);
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
Excellent and informative article! The concepts are explained very clearly and are helpful for anyone looking to build strong programming skills. For learners searching for practical and career-oriented training, the Best Java Real Time Projects Online Training in Hyderabad is a great opportunity to gain real-time project experience and industry exposure. Thanks for sharing such valuable content!
ReplyDeleteThis article nicely highlights the importance of learning Java fundamentals. Core Java plays a crucial role in building strong programming skills for any developer. You can also explore Core JAVA Online Training to understand concepts with real-time examples.
ReplyDelete