Chat Program Two Way Communication Java: S. Nageswara Rao, Corporate Trainer June 8, 2011 36 Comments
Chat Program Two Way Communication Java: S. Nageswara Rao, Corporate Trainer June 8, 2011 36 Comments
Chat Program Two Way Communication Java: S. Nageswara Rao, Corporate Trainer June 8, 2011 36 Comments
import java.io.*;
import java.net.*;
public class GossipClient
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("127.0.0.1", 3000);
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new
InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
import java.io.*;
import java.net.*;
public class GossipServer
{
public static void main(String[] args) throws Exception
{
ServerSocket sersock = new ServerSocket(3000);
System.out.println("Server ready for chatting");
Socket sock = sersock.accept( );
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new
InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
pwrite.flush();
The flush() method of PrintStream class flushes any uncleared buffers
in memory. Generally when the streams are closed, the buffers are
pushed out of all the data contained in. But, in the program no stream
is closed; so, it is necessary to flush the data explicitly with flush()
method.
As you can observe, both programs are using same streams and
objects. The difference comes in while loop. Client sends first and then
receives where as server first receives and then sends. The other terms
of the code are explained in the earlier three applications.
Do you know?
1. A frame can be created without a border.
2. Coffee cup on the frame title bar can be changed with your photo.
3. Size of the button depends on the layout manager you choose.
4. The space along the border of the frame and components inside can
be adjusted.
5. Java support freelance drawing using AWT.
6. Components can be added without using layout manager.
7. Dialog box can be modal or modeless.
8. There exists HeadlessException in AWT.
9. You can draw a cube, cylinder etc. without having predefined
methods.
10. addActionListener() is a method of Button but not ActionListener.
11. Importing both java.awt and java.awt.event
Live Chat for Slack
Ad
Social Intents
IO Streams Overview Java
way2java.com
Download Cleaner for Mac
Ad
mackeeper.com