Java network Programming "socket Server and client"

Source: Internet
Author: User

Socket Programming Everyone is not unfamiliar, Java learning must learn part of Java Network programming is one of the core content. Java network programming also includes TCP, Udp,url and other modules. TCP corresponds to the socket module, UDP corresponds to the Datagrampacket module. URL corresponds to the URL module. Where TCP and UDP are network transport protocols, TCP is the data Flow transport protocol, and UDP is the packet transport protocol. The similarities and differences between the two is not here to say, recommend a primer book, "TCPIP Primer Classic." Let's start with the Socket server and client programming.

One, Socket service side

Package Www.rockcode.com.tnetty.server;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import java.io.printwriter;import java.net.serversocket;import java.net.Socket;/** * @ Author Xums * Socketserver * TODO * May 4, 2017-morning 11:07:49 */public class Socketserver {public static void main (string[] Arg s) {Socketserver server = new Socektserver ();
Server.init ();} public void init () {ServerSocket serversocket = null; Socket socket = NULL; BufferedReader br = null; PrintWriter pw = null;try {serversocket = new ServerSocket (9999); socket = serversocket.accept (); br = new BufferedReader (NE W InputStreamReader (Socket.getinputstream ()));p w = new PrintWriter (Socket.getoutputstream ()); String msg = Br.readline (), while (null!=msg) {System.out.println ("received message:" +msg); msg = Br.readline ();}} catch (IOException e) {e.printstacktrace ();} Finally{if (NULL!=PW) {pw.close ();} if (NULL!=BR) {try {br.close ();} catch (IOException e) {e.printstacktrace ()}} if (Null!=socket) {try {socket.close ();} catch (IOException e) {e.printstacktrace ()}}}}

  

Second, Socket client

Package Www.rockcode.com.tnetty.client;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.net.inetsocketaddress;import java.net.Socket;/** * @author xums * socketclient * TODO * May 4, 2017-morning 10:47:20 */public class Socketclient {public static void main (string[]                args) {socketclient client = new Socketclient (); Client.init ();} public void init () {socket socket = NULL; BufferedReader br = null; PrintWriter pw = null;try {socket = new socket () Socket.connect (new Inetsocketaddress (9999), socket.setkeepalive); true),//Default Falseif (socket.isconnected ()) {br = new BufferedReader (new InputStreamReader (system.in));p w = new PrintWriter (Socket.getoutputstream (), true);//If the second argument does not select True or if only one argument is selected, remember to flush () String msg = "" When the message is finished, while (true) { Boolean alive = Socket.getkeepalive ();//Determine if the server is online if (!alive) {throw new Exception ("The service side is not in line!") ");} Else{msg = Br.readline (); if (null!=msg) {System.out.println ("Send message:" +msg);p w.println (MSG); Pw.flush ();}}}} catch (Exception e) {System.out.println (E.getmessage ());} Finally{if (NULL!=PW) {pw.close ();} if (NULL!=BR) {try {br.close ();} catch (IOException e) {e.printstacktrace ()}} if (Null!=socket) {try {socket.close ();} catch (IOException e) {e.printstacktrace ()}}}}

Socket Programming is a must-know point of knowledge, even if there are many open source communication frameworks, such as Mina, Netty, Cindy, and so on. Most of the business is now Netty, from the most primitive Java IO to Java NIO, to Java AIO, to Netty and so on. The bottom is inseparable from the Socket, all technology updates and continuous development are behind the business in the drive. People pursue efficient, high-speed, high-quality communications, prompting the open source framework to evolve. About open source communication framework later, and we learn together, thank you for watching!

Java network Programming "socket Server and client"

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.