Java Learning Note (59)-Socket programming

Source: Internet
Author: User

Client
ImportJava.io.IOException;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress;ImportJava.net.UnknownHostException;/ * * Client * * Public  class Client {     Public Static void Main(string[] args)throwsIOException {/ * * Send data to server */        //1. Define the server's address, port, dataInetAddress address = Inetaddress.getbyname ("127.0.0.1");intPort =8800;byte[] data ="Have you been 2 today?" ". GetBytes ();//2. Create a datagram that contains the data information sentDatagrampacket packet =NewDatagrampacket (data, data.length, address, Port);//3. Create a Datagramsocket objectDatagramsocket socket=NewDatagramsocket ();//4. Send DatagramsSocket.send (packet);/ * * Receive server-side response data */        //1. Creating a datagram for receiving data that stores a response        byte[] data2=New byte[1024x768]; Datagrampacket packet2=NewDatagrampacket (data2, data2.length);//2. Receiving response DataSocket.receive (Packet2);//3. Reading dataString info=NewString (Data2,0, Packet2.getlength ()); System.out.println ("I'm the client," said the server .+info);//4. Closing ResourcesSocket.close (); }}
Service side
ImportJava.io.IOException;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress;/* * Server side * * Servers must be started earlier than client */ Public  class Server {     Public Static void Main(string[] args)throwsIOException {/ * * Receive data sent by client */        //1. Create a server-side datagramsocket, specify the portDatagramsocket socket =NewDatagramsocket (8800);//2. Create datagrams to receive data sent by the storage client        byte[] data =New byte[1024x768]; Datagrampacket packet =NewDatagrampacket (data, data.length);//3. Receive DataSocket.receive (packet);//4. Reading dataString info =NewString (data,0, Packet.getlength ()); System.out.println ("I am the server," said the client, "+ info);/ * * Response client * /        //1. Define the address, port, and data of the clientInetAddress address = packet.getaddress ();intPort = Packet.getport ();byte[] Data2 ="2, What about you?" ". GetBytes ();//2. Create a datagram that contains the data information for the responseDatagrampacket Packet2 =NewDatagrampacket (Data2, Data2.length, address, Port);//3. Responding to clientsSocket.send (Packet2);//4. Closing ResourcesSocket.close (); }}

Java Learning Note (59)-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.