Buz-java (10)

Source: Internet
Author: User

import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;public class test_udp_send {    public static void main(String arg[]) throws Exception{        DatagramSocket dSocket=new DatagramSocket(3000);        String string ="Hello,word";        DatagramPacket datagramPacket=new DatagramPacket(string.getBytes(), string.length(),InetAddress.getByName("127.0.0.1"),8001);        dSocket.send(datagramPacket);        dSocket.close();    }}
import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;public class test_udp_server {    public static void main(String args[]) throws Exception{        byte buf[]=new byte[1024];        DatagramSocket dSocket= new DatagramSocket(8001);        DatagramPacket datagramPacket=new DatagramPacket(buf, 1024);        dSocket.receive(datagramPacket);        String str=new String(datagramPacket.getData());        System.out.println(str);        dSocket.close();            }}

  Import Java.io.ioexception;import java.io.inputstream;import java.net.inetaddress;import Java.net.socket;public class Test_tcp_send {public static void main (String arg[]) throws exception{new Client (). Connec    T (); }}class client{public void Connect () throws Exception, ioexception{socket client=new socket (inetaddress.ge        Tlocalhost (), 8009);        InputStream Inputstream=client.getinputstream ();        byte buf[]=new byte[1024];        int Length=inputstream.read (BUF);        System.out.println (New String (BUF));    Client.close (); }}
public class test_tcp_server {    public static void main(String args[]) throws Exception    {        new server().connect();    }}class server{        public void connect() throws Exception{                ServerSocket serverSocket=new ServerSocket(8009);        Socket client=serverSocket.accept();        OutputStream outputStream=client.getOutputStream();        outputStream.write("hello,word".getBytes());        outputStream.close();        client.close();    }}

Buz-java (10)

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.