Socket communication in Java (ii) establishment and use of ServerSocket

Source: Internet
Author: User

Create a Java project in Eclipse Testmyserversocket

package com.yeqc.testmyserversocket.main;import  java.io.ioexception;import java.net.serversocket;import java.net.socket;import  Javax.swing.joptionpane;public class myserversocket {public static void main ( String[] args)  {//1-65535try {serversocket serversocket = new serversocket ( 54321)//serversocket.accept ();//Call Accept () to listen for the client's connection/** *accept () is a blocking method, so here it blocks the current thread  * Since it blocks the current main thread, it is created here because it is only a demonstration  * this is not strictly true and will be written in the right way in the future  * * In the current serversocket is already listening to this port of 12345 on this machine  * when this port its accept () is called, that is to say there is a client connected to this port  */Socket socket  = serversocket.accept ();//Establish a connection/** *  once the Accept () is executed and the socket is assigned, this means that the client is already connected  */ Joptionpane.showmessagedialog (null,  "has client connected to the 12345 port of this machine");//Popup Prompt box} catch  (ioexception e)  {e.printstacktrace ();}}} 

At this point you can see that the current program is running and not stopped, and there is no output prompt box, so you can see that the current program is blocked in the statement: Serversocket.accept ();

Then open the browser, enter 127.0.0.1:54321 in the Address bar (127.0.0.1 to read the address of the computer, after the colon is the port to read), you can see the pop-up prompt box.

Socket communication in Java (ii) establishment and use of ServerSocket

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.