UDP Instant Small communication

Source: Internet
Author: User

Package 12th, import java.io.ioexception;import java.net.*;p ublic class Udpserver {/*** * * @param args * @throws ioexcept Ion */public static void Main (string[] args) throws IOException {//TODO auto-generated method stubSystem.out.println ("service ");D atagramsocket socket=new datagramsocket (55555), while (true) {byte[]b=new byte[1024];// UDP sends Datagrampacket packet=new datagrampacket (b,b.length) in the form of packets; socket.receive (packet); String S=new string (Packet.getdata (), 0,packet.getlength ()),//Gets the ipstring ip=packet.getaddress () to the sending end. ToString (); System.out.println (ip+ ":" +s);}}} Client Ackage the 12th chapter; Import Java.io.ioexception;import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;import Java.net.socket;import Java.net.socketexception;import java.net.UnknownHostException; Import Java.util.scanner;public class UdpClient {public static void main (string[] args) throws IOException {//TODO auto-g Enerated method Stubwhile (True) {Scanner scan=new Scanner (system.in); System.out.println ("Please enter the data to be sent"); String str =new string (); Str=scan.nextline (); byte[]b=str.getbytes ();//server-side IP address inetaddress serveraddress= Inetaddress.getbyname ("192.168.1.179");//Parameter list: Byte array, length of array, IP address of server, port number of server Datagrampacket packet=new Datagrampacket (b,b.length,serveraddress,55555);//datagramsocket can send this packet to the server Datagramsocket socket=new Datagramsocket (); socket.send (packet);}}

UDP Instant Small communication

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.