码迷,mamicode.com
首页 > 编程语言 > 详细

buz-java(十)

时间:2017-12-13 19:39:28      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:分享   serve   rgs   []   main   java   blog   test   out   

技术分享图片

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().connect();
    
}
}
class client{
    
    public void connect() throws Exception, IOException{
        Socket client=new Socket(InetAddress.getLocalHost(),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(十)

标签:分享   serve   rgs   []   main   java   blog   test   out   

原文地址:http://www.cnblogs.com/ysy521/p/8034136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!