Java网络编程实例

UDP传输

1、接收端

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * @author Hanlin Wang
 */

public class Receiver {
    public static void main(String[] args) throws IOException {
        //建立buf
        byte[] buf = new byte[1024];

        //建立packet接受数据
        DatagramPacket packet = new DatagramPacket(buf, buf.length);

        //建立socket监听接受端口
        DatagramSocket socket = new DatagramSocket(8080);

        //利用socket接受数据,未接收数据时线程会阻塞在此
        socket.receive(packet);

        //利用packet打印接受的数据
        String text = new String(packet.getData(), 0, packet.getLength())+
                " from " + InetAddress.getLocalHost() + ":" + packet.getPort();
        System.out.println(text);

        //释放资源
        socket.close();
    }
}

2、发送端

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * @author Hanlin Wang
 */

public class Sender {
    public static void main(String[] args) throws IOException {
        //准备发送信息
        String text = "whataya want from me";

        //建立packet
        DatagramPacket packet = new DatagramPacket(text.getBytes(), text.length(), InetAddress.getLocalHost(), 8080);

        //建立socket
        DatagramSocket socket = new DatagramSocket(3000);

        //通过socket发送信息
        socket.send(packet);

        //释放资源
        socket.close();
    }
}

运行结果:

whataya want from me from WANG/192.168.1.1:3000

TCP传输

1、服务端

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Hanlin Wang
 */

public class Server {
    private final static int PORT = 8282;

    public static void main(String[] args) throws IOException, InterruptedException {
        //创建服务器,监听8282端口
        ServerSocket server = new ServerSocket(PORT);

        //等待接受客户端请求。若未收到客户端请求,将保持阻塞状态。
        Socket conn = server.accept();

        //利用客户端与服务端的连接conn,准备向客户端写入信息
        OutputStream out = conn.getOutputStream();

        //准备信息
        String str = "I'm Server, you know";

        System.out.println("开始与客户端交互!");

        //向客户端发送信息
        out.write(str.getBytes());

        InputStream in = conn.getInputStream();

        //缓存
        byte[] buf = new byte[1024];

        //获取客户端数据
        int len = in.read(buf);

        //打印客户端发送的数据
        System.out.println(new String(buf, 0, len));

        Thread.sleep(5000);

        System.out.println("与客户端交互结束!");

        //释放资源
        conn.close();
        server.close();
    }
}

2、客户端

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author Hanlin Wang
 */

public class Client {
    private final static int PORT = 8282;
    public static void main(String[] args) throws UnknownHostException, IOException {
        //创建客户端, 并连接到服务端。
        Socket conn = new Socket(InetAddress.getLocalHost(), 8282);

        //客户端向服务端发送信息。
        OutputStream out = conn.getOutputStream();

        out.write("Hello Server, I'm Client".getBytes());

        //得到服务端向客户端的输入流
        InputStream in = conn.getInputStream();

        //缓存
        byte[] buf = new byte[1024];

        //获取数据
        int len = in.read(buf);

        //输出结果
        System.out.println(new String(buf, 0, len));

        //释放资源
        conn.close();
    }
}

服务端运行结果:

开始与客户端交互!
Hello Server, I'm Client
与客户端交互结束!```

客户端运行结果:

I’m Server, you know
“`

你可能感兴趣的:(Java)