Java Series lecture One: TCP Way socket Communication case explained

Source: Internet
Author: User
Tags readline

Development of network communications software, such as QQ, Alibaba and other instant messaging software, socket communication is essential to the basic knowledge, today we talk about the TCP communication in the socket of the software development steps and the main code explanation:

The first step: the development of server-side program Server.java, the source code is as follows:

package t16;import java.io.bufferedreader;import java.io.ioexception;import  java.io.inputstream;import java.io.inputstreamreader;import java.io.outputstream;import  java.io.printstream;import java.net.serversocket;import java.net.socket;public class  server {//Server-side program Public static void main (String[] args)  {/** * 1. Create a server-side serversocket, specify a port, and you can specify ip *  parameter 1: Port  *  Parameter 2: The number of clients waiting to be queued  */try {//1 when there are multiple NICs. Create server-side serversocketserversocket serversocket = new serversocket (8888,5); SYSTEM.OUT.PRINTLN ("Server started successfully:" +serversocket.tostring ());//2. Waiting for customer connection socket clientsocket =  Serversocket.accept (); System.out.println ("Customers come, please pick-up ...");//3. Bind input Stream Inputstream is = clientsocket.getinputstream (); Inputstreamreader isr = new inputstreamreader (IS); Bufferedreader br = new bufferedreader (ISR);//4. Bound output Stream outputstream  Os = clientsocket.getoutputstream (); Printstream ps = new printstream (OS);//5. Receive Data System.out.println ("server-side receive:" +br.readline ()); /6. Send data string str =  "Welcome to this server."; Ps.println (str);//7. Close socket communication clientsocket.close (); Serversocket.close ();}  catch  (ioexception e)  {e.printstacktrace ();}}


The second step: Develop the client program Client.java, the source code is as follows:

package t16;import java.io.bufferedreader;import java.io.filereader;import  java.io.ioexception;import java.io.inputstream;import java.io.inputstreamreader;import  java.io.outputstream;import java.io.printstream;import java.net.socket;import  java.net.unknownhostexception;import java.util.calendar;public class client {/** *   Client program:  * 1. Specify the IP address of the server;  * 2. Specify the port of the server;  *  * */public static  void main (String[] args)  {try {//1. Create a client socket; Socket socket = new socket ("127.0.0.1", 8888); SYSTEM.OUT.PRINTLN ("Connection server successfully ....");//2. Bind input/output stream Outputstream os = socket.getoutputstream (); Printstream ps = new printstream (OS); Inputstream is = socket.getinputstream (); Inputstreamreader isr = new inputstreamreader (IS); Bufferedreader br = new bufferedreader (ISR);//3. Number of sendsAccording to Ps.println ("I am the client, request connection ...");//4. Receive Data SYSTEM.OUT.PRINTLN ("Client Receives server data:" +br.readline ());//5. Close Socketsocket.close ( );}  catch  (unknownhostexception e)  {e.printstacktrace ();}  catch  (ioexception e)  {e.printstacktrace ();}}


The third step: first run the server-side program, and then run the client program, you can see the network after the transmission of the print results


Description: The program is a relatively simple entry-level code, the entire program can only do one network communication, if you need to make a continuous communication software software, but also use multi-threading, IO Flow operations, database operations and many other technologies integrated application.


If you are interested to see my Java from the beginner to proficient in +QQ Instant Messaging software Project Training video , link address: http://edu.51cto.com/course/6946.html

Java Series lecture One: TCP Way socket Communication case explained

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.