Customer advisory < socket programming based on UDP protocol (datagram socket) >

Source: Internet
Author: User

1, the construction method of Datagrampacket class

2, the construction method of Datagramsocket class

3. Common methods of Datagramsocket class

4. Service Side

 package com.ljb.app.datagram;import java.io.IOException;import java.net.DatagramPacket; import java.net.datagramsocket;import java.net.socketaddress;import java.net.socketexception;/* * *  Customer Advisory Service  *  @author  LJB *  @version  2015 March 12  */public  class askserver { /**  *  @param  args  */ public static  void main (String[] args)  {  try {   //  Create receiver Sockets (server), and bind the port number    datagramsocket ds = new datagramsocket (8800);    //   Determines the size of the array of data received by the packet    byte[] buf = new byte[1024];       //  Create a packet of the receiving type, and the data is stored in the array    DatagramPacket dp = new  Datagrampacket (buf,buf.length);      //  receive data via sockets     Ds.receive (DP);       //  parsing the data sent by the sender    string mess = new string (buf   0 , dp.getlength ()); &NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("client says:"  + mess);       //  response Information    String reply =  "Hello, I am in, please inquire." ";    byte[] buf2 = reply.getbytes ();      //  Response Address    socketaddress sa = dp.getsocketaddress ();       //  packet    datagrampacket dp2 = new datagrampacket (buf2 , &NBSP;BUF2.LENGTH&NBSP;,&NBSP;SA);      //  send    ds.send (DP2) ;      //  Release Resources    ds.close ();  } catch  ( socketexception e)  {   e.printstacktrace ();  } catch  (IOException  e)  {  &nbsP;e.printstacktrace ();   }     }} 

5, client

Package com.ljb.app.datagram;import java.io.ioexception;import java.net.datagrampacket;import  java.net.datagramsocket;import java.net.inetaddress;import java.net.socketexception;import  java.net.UnknownHostException;/** *  Customer Advisory Client  *  @author  LJB *  @version  2015 March 12  */public class AskClient { /**  *  @param  args   */ public static void main (String[] args)  {  //  Determine the information sent to the server, server address, Port   String mess =  "Hello, I want to ask a question?" ";   byte[] buf = mess.getbytes ();     inetaddress ia =  null;  try {   ia = inetaddress.getbyname ("localhost");   } catch  (unknownhostexception e)  {   e.printstacktrace ();   }    int port = 8800;    //  Create a packet, send the specified length of information to the specified port on the specified server   DatagramPacket dp = new  Datagrampacket (Buf , buf.length , ia , port);    //  Create a Socket object   datagramsocket ds = null;  try {   ds =  new datagramsocket ();  } catch  (socketexception e)  {    e.printstacktrace ();  }    //  send packet   try {    ds.send (DP);  } catch  (IOEXCEPTION&NBSP;E)  {   e.printstacktrace ();  }    //  create receive array   byte[] buf2 = new byte[ 1024];    //  creating a Receive packet   DatagramPacket dp2 = new  Datagrampacket (buf2 , buf2.length);    //  receive data   try {    ds.receive (DP2);  &nBSP;}  catch  (ioexception e)  {   e.printstacktrace ();  }     //  parse the received data   string reply = new string (buf2 , 0  ,  dp2.getlength ());   system.out.println ("server-side said:"  + reply);     //   Release Resources   ds.close ();  }}

Operation Result:

Service side:

Client said: Hello, I would like to ask a question?

Client:

Server side said: Hello, I am in, please consult.

Customer advisory < socket programming based on UDP protocol (datagram socket) >

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.