Java socket based on UDP/IP protocol

Source: Internet
Author: User

Java socket based on UDP/IP protocol application

Server side: 1, create Datagramsocket, specify port number

2. Create Datagrampacket

3, receive the data sent by the client

4. Read data

Client:

1. Define send message: The IP port number of the server to send the content

2. Create a datagrampacket that contains the information that will be sent

3. Create Datagramsocket

4. Send data

Server-side

Import java.io.IOException;
Import Java.net.DatagramPacket;
Import Java.net.DatagramSocket;
Import java.net.InetAddress;
Import java.net.SocketException;

/**
*
server-side, implementation of UDP-based user login
* Created by Jim Calark on 2017/3/19.
*/
public class Udpserver {
public static void Main (String[]args) throws SocketException, ioexception{
1. Create server-side datagramsocket, specify port
Datagramsocket datagramsocket = new Datagramsocket (8800);
2. Create a datagram to accept data sent by the client
byte []data = new byte[1024];//creating byte array, specifying the size of the accepted datagram
Datagrampacket datagrampacket = new Datagrampacket (data,data.length);
3, receive the data sent by the client
System. out. println ("Server is turned on, waiting for client Connection");
This method will block until the data is received
Datagramsocket.receive (Datagrampacket);
4, read the data sent by the client
Parameter: Data to be converted array 0 starting from subscript 0 of the array datagrampacket.getlength () length is the length received
String info = new String (Data,0,datagrampacket.getlength ());
System. out. println ("Here is the message from the server, the client:--" +info);

/**
Responding to a client
*/
1, define the client's address, port number, data
Get Client IP Address
InetAddress inetaddress = datagrampacket.getaddress ();
Get Client port number
int port= datagrampacket.getport ();
Save the contents of the response to a byte array
byte []DATA2 = "Welcome!" ". GetBytes ();
2 Create a datagram that contains the data information for the response
Datagrampacket datagramPacket12 = new Datagrampacket (data2,data2.length,inetaddress,port);
3. Responding to clients
Datagramsocket.send (datagramPacket12);
4. Close Resources
Datagramsocket.close ();

}
}

Client
Import java.io.IOException;
Import java.net.*;

/**udpClient
* Created by Jim Calark on 2017/3/19.
*/
public class UdpClient {
public static void Main (string[] args) throws Unknownhostexception, socketexception,ioexception{
1. Define server address, port number, data
InetAddress inetaddress = inetaddress.Getbyname("localhost");
int port = 8800;
byte [] data = "User name: the most handsome; Password: 123". GetBytes ();
2. Create a datagram that contains the information sent
Datagrampacket datagrampacket = new Datagrampacket (data,data.length,inetaddress,port);
3. Create a Datagramsocket object
Datagramsocket datagramsocket = new Datagramsocket ();
4. Send datagrams to server side
Datagramsocket.send (Datagrampacket);

/**
*
client receives server-side response information
*
*/
1. Create a datagram to receive server-side response data and save the data in a byte array
byte [] data2 = new byte[1024];
Datagrampacket datagramPacket1 =new datagrampacket (data2, data2.length);
2. Receiving server response data
Datagramsocket.receive (DATAGRAMPACKET1);
3. Read data
string reply = new string (Data2,0,datagrampacket1.getlength ());
System. out. println ("Here is the message from the client, server side:--" + reply);
4. Close Resources
Datagramsocket.close ();


}
}

Java socket based on UDP/IP protocol

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.