Java udp protocol interaction information and JAVAUDP protocol information

Source: Internet
Author: User

Java udp protocol interaction information and JAVAUDP protocol information

As the UDP protocol interaction of the app is required, I learned it specially. In fact, it is similar to the server and socket of java. Below I wrote a simple demo.

Server:

Package com. test1; import java. io. IOException; import java.net. datagramPacket; import java.net. datagramSocket; import java.net. inetSocketAddress; import java.net. socketException; public class UpdServer {private byte [] buff = new byte [1024]; private comment rampacket dp; private comment ramsocket ds; private InetSocketAddress socketAddress; public UpdServer (String host, int port) {socketAddress = new InetSocke TAddress (host, port); try {ds = new DatagramSocket (socketAddress);} catch (SocketException e) {System. out. println ("Connection Failed:" + e. getLocalizedMessage (); e. printStackTrace ();} System. out. println ("the server enables listening... ");} private String receive (String host, int port) {String info =" "; try {dp = new DatagramPacket (buff, 0, buff. length); ds. receive (dp); info = new String (dp. getData (), 0, dp. getLength ();} c Atch (Exception e) {e. printStackTrace ();} return info;} public static void main (String [] args) {String host = "127.0.0.1"; int port = 8888; UpdServer server = new UpdServer (host, port); String info = server. receive (host, port); System. out. println (info); // send the message to the client server. send (new String ("Hello, cool! ");} Private void send (String string) {DatagramPacket dps = new DatagramPacket (buff, 0, buff. length, dp. getAddress (), dp. getPort (); dps. setData (string. getBytes (); try {ds. send (dps);} catch (IOException e) {e. printStackTrace ();}}}

 

Client:

Package com. test1; import java. io. IOException; import java.net. datagramPacket; import java.net. datagramSocket; import java.net. inetAddress; import java.net. socketException; public class UpdClient {private byte [] buff = new byte [1024]; private DatagramPacket dp; private DatagramSocket ds; public UpdClient () {try {ds = new DatagramSocket ();} catch (SocketException e) {e. printStackTrace () ;}} public st Atic void main (String [] args) {UpdClient updClient = new UpdClient (); String host = "127.0.0.1"; int port = 8888; updClient. send (host, port, new String ("Hello, boy! "). GetBytes (); // accept String info = updClient. receive (); System. out. println (info);} private String receive () {DatagramPacket dpr = new DatagramPacket (buff, buff. length); String info = ""; try {ds. receive (dpr); info = new String (dpr. getData (), 0, dpr. getLength ();} catch (IOException e) {e. printStackTrace ();} return info;} private void send (String host, int port, byte [] B) {try {dp = new DatagramPacket (buff, 0, buff. length, InetAddress. getByName (host), port); dp. setData (B); ds. send (dp);} catch (Exception e) {e. printStackTrace ();}}}

 


Urgent C # program, using UDP protocol to achieve information interaction between the client and the server

Open a Thread for each request and execute the Send () operation. Thread. Sleep (1000); While (IsWorking ){...}

It will be more rewarding to write your own code.

How to Design the course of java student information management system

Generally, you need to master simple SQL database operations.
Then we know how to connect to and operate the database through jdbc using java.
Then, if you want to have a jsp or swing interface

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.