Java program of Client-Server network for Chatting between Client and Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.net.*;
import java.io.*;
 
public class  chatserver
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(2000);
Socket sk=ss.accept();
BufferedReader cin=newBufferedReader(newInputStreamReader(sk.getInputStream()));
PrintStream cout=new PrintStream(sk.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s;
while (  true )
{
s=cin.readLine();
if (s.equalsIgnoreCase("END"))
{
cout.println("BYE");
break;
}
System. out.print("Client : "+s+"\n");
System.out.print("Server : ");
s=stdin.readLine();
cout.println(s);
}
ss.close();
sk.close();
cin.close();
cout.close();
stdin.close();
}
}
 
public class  chatclient
{
public static void main(String args[]) throws Exception
{
Socket sk=new Socket("192.168.0.19",2000);
BufferedReader sin=new BufferedReader(new InputStreamReader(sk.getInputStream()));
PrintStream sout=new PrintStream(sk.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s;
while (  true )
{
System.out.print("Client : ");
s=stdin.readLine();
sout.println(s);
s=sin.readLine();
System.out.print("Server : "+s+"\n");
if ( s.equalsIgnoreCase("BYE") )
break;
}
sk.close();
sin.close();
sout.close();
stdin.close();
}
}

Output:

Java chatclient

From Server : Hi
From Client: Hi
From Server: Good morning
From Client: End
From Server:Bye

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

33 thoughts on “Java program of Client-Server network for Chatting between Client and Server

  1. so great!!!!! thank you
    زۆر نایابە دەست خۆش!!!!بە ڕاستى ئافەرین

  2. import java.net.*;
    import java.io.*;

    public class chatserver
    {
    public static void main(String args[]) throws Exception
    {
    ServerSocket ss=new ServerSocket(2000);
    Socket sk=ss.accept();
    BufferedReader cin=new BufferedReader(new InputStreamReader(sk.getInputStream()));
    PrintStream cout=new PrintStream(sk.getOutputStream());
    BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
    String s;
    while ( true )
    {
    s=cin.readLine();
    if (s.equalsIgnoreCase(“END”))
    {
    cout.println(“BYE”);
    break;
    }
    System. out.print(“Client : “+s+”\n”);
    System.out.print(“Server : “);
    s=stdin.readLine();
    cout.println(s);
    }
    ss.close();
    sk.close();
    cin.close();
    cout.close();
    stdin.close();
    }
    }

    import java.net.*;
    import java.io.*;

    public class chatclient
    {
    public static void main(String args[]) throws Exception
    {
    Socket sk=new Socket(“localhost”,2000);
    BufferedReader sin=new BufferedReader(new InputStreamReader(sk.getInputStream()));
    PrintStream sout=new PrintStream(sk.getOutputStream());
    BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
    String s;
    while ( true )
    {
    System.out.print(“Client : “);
    s=stdin.readLine();
    sout.println(s);
    s=sin.readLine();
    System.out.print(“Server : “+s+”\n”);
    if ( s.equalsIgnoreCase(“BYE”) )
    break;
    }
    sk.close();
    sin.close();
    sout.close();
    stdin.close();
    }
    }

  3. getting error while running this server program as below, please check

    error: cannot find symbol
    BufferedReader cin=newBufferedReader(newInputStreamReader(sk.get
    InputStream()));
    ^
    symbol: method newInputStreamReader(InputStream)
    location: class chatserver

  4. BufferedReader cin=new BufferedReader(newInputStreamReader(sk.get
    InputStream()));
    you forgot to give space between the words new and BufferedReader

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in