Network Programming (Learning to organize)---to achieve a simple console chat room

Source: Internet
Author: User

1. Simple chat Room (console):

Feature implementation:

Exchange of information between client and server:

2, the knowledge points involved:

This I would like to say, detailed later see Code!

    

1) Three elements of network communication
1. IP
2. Port number.
3. Agreement

2) inetaddress (IP Class)

Common methods:
Getlocalhost (); Get the IP address of this machine
Getbyname ("IP or hostname") generates an IP address object based on a string of IP addresses or a host name. (used to get someone else's IP address object)

Gethostaddress () returns a string representation of an IP address.
GetHostName () returns the host name of the computer

  

3) There is no class description for the port number.
Range of port numbers: 0~65535
From 0 to 1023, the system is tightly bound to some services.
1024~65535 we can use ....

4) Protocol for network communication:
UDP communication protocol

TCP communication protocol.

5) TCP
(1) TCP Communication protocol Features:
1. TCP is the transmission of data based on IO streams, and is oriented to the connection.
2. There is no size limit when TCP transmits data.
3. TCP is connection-oriented and ensures data integrity through a three-time handshake mechanism. Reliable protocol.
4. TCP is connection-oriented, so the speed is slow.
5. TCP is differentiated between the client and the server.
For example: Call, Qq\feiq file transfer, Thunder download ....


(2) socket under the TCP protocol:
Socket (client), the TCP client will be connected to the server as soon as it is started.
ServerSocket (service-side Class)

(3) Client use steps for TCP:
1. Establish the client service for TCP.
2. Get to the corresponding stream object.
3. Write or read data
4. Close the resource.  

3, the following directly affixed to my test code:

Client code:  

1  Packagecn.chating;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 Importjava.io.IOException;6 ImportJava.io.InputStreamReader;7 ImportJava.io.OutputStreamWriter;8 ImportJava.net.Socket;9 Importjava.net.UnknownHostException;Ten  One /** A * Chat: Client -  * @authorSmall Wind micro-spirit -  * the  */ -  Public classChatClient { -  -      +      Public Static voidMain (string[] args)throwsException { -          +         //Establish client ASocket client=NewSocket ("192.168.9.122", 22222); at         /***********************************io start **************************************/         -         //gets the input character stream object -BufferedReader reader=NewBufferedReader (NewInputStreamReader (Client.getinputstream ())); -          -         //gets the output character stream object -BufferedWriter writer=NewBufferedWriter (NewOutputStreamWriter (Client.getoutputstream ())); in          -         //get keyboard input stream toBufferedReader keywrite=NewBufferedReader (NewInputStreamReader (system.in)); +          -String content=NULL; the          *String get=NULL; $         Panax NotoginsengSystem.err.println ("Please enter the information you want to send:"); -          the          while((Content=keywrite.readline ())! =NULL){//sending information to the server +              AWriter.write ("client says:" +content);  theWriter.newline ();//line Break +Writer.flush ();//Send -              $             //Receiving service-side information $             if(get = Reader.readline ())! =NULL){                 -SYSTEM.OUT.PRINTLN ("service side says:" +get); -             } the         } -         /***********************************io End **************************************/        Wuyi         //Close Resource the client.close (); -     } Wu  -}

Service-side code:

1  Packagecn.chating;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.InputStream;6 ImportJava.io.InputStreamReader;7 ImportJava.io.OutputStreamWriter;8 ImportJava.net.ServerSocket;9 ImportJava.net.Socket;Ten  One /** A * Chat: - * The service side of a question and answer -  * @authorSmall Wind micro-spirit the  * -  */ -  Public classChatserver { -  +  Public Static voidMain (string[] args)throwsexception{ -          +         //setting up the socket service side AServerSocket Server =NewServerSocket (22222); at          -         //blocking method, waiting for a connection from the receiving client -Socket Rsocket =server.accept (); -          - /***********************************io start **************************************/         -         //reads the information sent by the connected client input byte stream inInputStream is =Rsocket.getinputstream (); -          to         //gets the input buffer character stream object +BufferedReader reader =NewBufferedReader (NewInputStreamReader (IS)); -          the         //gets the output character stream object *BufferedWriter writer =NewBufferedWriter (NewOutputStreamWriter (Rsocket.getoutputstream ()));  $         Panax Notoginseng         //get the input stream of the keyboard -BufferedReader Keyreader =NewBufferedReader (NewInputStreamReader (system.in)); the          +String content =NULL; A          theString send =NULL; +          -          while(content = Reader.readline ())! =NULL){ $              $SYSTEM.OUT.PRINTLN ("Server Received:" +content); -              -             //keyboard hangs, once the             if(send = Keyreader.readline ())! =NULL){   - writer.write (send);WuyiWriter.newline ();//line Break theWriter.flush ();//Send -             } Wu         } - /***********************************io End **************************************/         About          $         //Close Resource - rsocket.close (); -     } -  A}

Network Programming (Learning to organize)---to achieve a simple console chat room

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.