Java--socket programming

Source: Internet
Author: User

1. Socket BASIC Communication model

2. Using the socket based on the TCP protocol

3. Using UDP protocol-based SOCKET4

4, based on Bluetooth protocol, there are many protocols.

IP address: a symbol that identifies a computer in the network.

PORT: The computer has a port, each port can have an application to communicate, 80 port Web,http protocol used port, 21 23 port for FTP, 135 138 windows is more important. interface, the value range 0-256*256-1. The section terminating port below 1024 is the operating system ports, the other we can use.

Socket English "socket", commonly referred to as "socket", is used to describe the IP address and port, which is a handle to a communication chain. The interface of network communication,

Applications typically use sockets to make requests to the network or to answer network requests.

TCP protocol 1, server-side methods

Click on a button to open a thread. New Serverthread () Start ().

We can get the outputsttream inside the socket to send the message to the client.

2. Tcpclient.java Client Code

UDP protocol 1, server-side

Modify the method inside the run ().

UDP is simpler than TCP because it is accepted in the form of a packet without the concept of a stream.

2, the Client

UdpClient. java

Real machine test, simulator not, test the UDP protocol. Print out the results on the server.

A bunch of seven or eight-slot characters in the back, byte[] The length of the array is 1024, so many bytes

String sting = new String (Packet.getdata (), Packet.getoffset (), Packet.getlength ())

Set the offset, and not necessarily from the No. 0 bit of the array, to begin accepting. After the settings are sent from the data header just beginning to accept.

How much data was received this time.

This time the data is taken from the first of the array, to the end of the first.

Java--socket programming

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.