TCP Programming Example (non-multithreaded chat)

Source: Internet
Author: User
Tags flush readline

Server-side:

Package socket;

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.io.PrintWriter;
Import java.net.*;

Import Java.util.Scanner;
		public class Chatserver {public static void main (string[] args) {ServerSocket server = null;
		Socket client = null;
		try {server = new ServerSocket (6666);
			} catch (IOException e) {System.out.println ("Connecting error!");
		System.exit (-1);
		} try {client = server.accept ();
			} catch (IOException e) {//TODO auto-generated catch block System.out.println ("Accept error!");
		System.exit (-1);
		} BufferedReader br = null;
		BufferedReader scan = null;
		PrintWriter pw = null;
		String line = null;
		String cmsg = null;
			try {br = new BufferedReader (New InputStreamReader (Client.getinputstream ()));
			Scan = new BufferedReader (new InputStreamReader (system.in));
PW = new PrintWriter (New OutputStreamWriter (Client.getoutputstream ()));			Cmsg = Br.readline (); while (! "
				Bye ". Equalsignorecase (cmsg)) {System.out.println (" Client: "+cmsg);
				line = Scan.readline ();
				Pw.println (line); 
				Pw.flush ();
			Cmsg = Br.readline ();
				} if ("Bye". Equalsignorecase (cmsg)) {pw.println ("Bye");
			Pw.flush ();
			} client.close ();
			Br.close ();
			Scan.close ();
		Pw.close ();
		} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
 }
	}

}

Client:

Package socket;

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.io.PrintWriter;
Import Java.net.Socket;

Import java.net.UnknownHostException;
		public class ChatClient {public static void main (string[] args) {Socket client = null;
		try {client = new Socket ("127.0.0.1", 6666);
			} catch (Unknownhostexception e) {System.out.println ("Unknown Host");
		System.exit (-1);
		} catch (IOException e) {e.printstacktrace ();
			} try {BufferedReader br = new BufferedReader (New InputStreamReader (Client.getinputstream ()));
			BufferedReader scan = new BufferedReader (new InputStreamReader (system.in));
			PrintWriter pw = new PrintWriter (New OutputStreamWriter (Client.getoutputstream ()));
			String line = null;
			String SMSG = null; while (! "
				Bye ". Equalsignorecase (SMSG)) {line = Scan.readline ();
				Pw.println (line);
				Pw.flush ();
				SMSG = Br.readline (); System.out.println ("Server:" +smsg);
				} if ("Bye". Equalsignorecase (SMSG)) {pw.println ("Bye");
			Pw.flush ();
			} client.close ();
			Br.close ();
			  Scan.close ();
		Pw.close ();
		} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
 }
	}

}

Description

1. You need to start the server first, then start the client, and the client sends the message first.

2. Server cannot start the server again at startup.

3. Server.accept (), as well as the read operation of the stream, are blocked, and will wait if there is no client connection, or if the data is not being read into.

4. After using PrintWriter for print, you must flush (), otherwise you cannot read it. This is because the flush method is forced to flush the buffer. What is a buffer, is to create a space on your machine, this space allows you to put something in the first, For example, if you do not call flush, the contents of the println go into the buffer instead of OS.PRINTLN to the client or server side.

5. The program is synchronous, that is, only the client sends a message once, the server sends again, the client sends again ... Pending improvement.

6. The program opens two windows under the CMD to run the effect to be good.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.