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);
}
}
}
Comments
Post a Comment