Java的基础知识7——网络

1、TCP:是专门设计用于在不可靠的因特网上提供可靠、端到端的字节流通信的协议。它是一种面向连接的协议。TCP连接是字节流而非报文流。
UDP:向应用程序提供一种发送封装的原始IP数据报的方法,并且发送时无需建立连接,是一种不可靠的连接。

2、java.net包中定义的两个类Socket和ServerSocket,分别用来实现双向连接的client和server端。

3、建立连接时所需的寻址信息为远程计算机的IP地址和端口号。

4、简单示例:
(1)TCP-client

import java.io.*;
import java.net.*;

public class TCPClient {

    public static void main(String[] args) {

        try {
            Socket s = new Socket("127.0.0.1",5888);
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            DataInputStream dis = new DataInputStream(s.getInputStream());
            String str = null;
            dos.writeUTF("hello server!");
            dos.flush();

            if((str = dis.readUTF()) != null){
                System.out.println(str);
                System.out.println("from:"+"IP:"+s.getInetAddress()+"   port:"+s.getPort());
            }
            dos.close();
            dis.close();
            s.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
}

TCP-server

import java.io.*;
import java.net.*;

public class TCPServer {

    public static void main(String[] args) {

        try {
            ServerSocket ss = new ServerSocket(5888);
            Socket s = ss.accept();
            DataInputStream dis = new DataInputStream(s.getInputStream());
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            String str = null;
            if((str = dis.readUTF()) != null){
                System.out.println(str);
                System.out.println("from:"+s.getInetAddress()+s.getPort());
            }
            dis.close();
            dos.writeUTF("hello client!");
            dos.flush();
            dos.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

(2)实现client端和server端的简单通话
TCP-client

import java.io.*;
import java.net.*;

public class TCPClient {

    public static void main(String[] args) {

        try {
            Socket s = new Socket("127.0.0.1",3333);
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            DataInputStream dis = new DataInputStream(s.getInputStream());
            BufferedReader brs = new BufferedReader(new InputStreamReader(System.in));

            String str = null;
            String strs = brs.readLine();

            while(!strs.equals("byebye")){              
                dos.writeUTF(strs);
                dos.flush();
                str = dis.readUTF();
                System.out.println("server:"+str);
                strs = brs.readLine();
            }
            brs.close();
            dis.close();
            dos.close();
            s.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

TCP-server

public class TCPServer {

    public static void main(String[] args) {

        try {
            ServerSocket ss = new ServerSocket(3333);
            Socket s = ss.accept();
            DataInputStream dis = new DataInputStream(s.getInputStream());
            BufferedReader brs = new BufferedReader(new InputStreamReader(System.in));
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());           
            String str = dis.readUTF();
            System.out.println("client:"+str);
            String strs = brs.readLine();

            while(!strs.equals("byebye")){
                dos.writeUTF(strs);
                dos.flush();
                str = dis.readUTF();
                System.out.println("client:"+str);
                strs = brs.readLine();
            }   
            dos.close();
            brs.close();
            dis.close();
            s.close();          
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

(3)UDP-client

import java.io.IOException;
import java.net.*;

public class UDPClient {

    public static void main(String[] args) {

        byte array[] = (new String("HelloWorld")).getBytes();
        try {
            DatagramPacket dp = new DatagramPacket(array,array.length,new InetSocketAddress ("127.0.0.1",4321));
            DatagramSocket ds = new DatagramSocket(8888);
            ds.send(dp);
            ds.close();
        } catch (SocketException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

UDP-server

import java.io.IOException;
import java.net.*;

public class UDPServer {

    public static void main(String[] args) {
        byte array[] = new byte[1024];
        DatagramPacket dp = new DatagramPacket(array,array.length);
        try {
            DatagramSocket ds = new DatagramSocket(4321);
            while(true){
                ds.receive(dp);
                System.out.println(new String(array,0,dp.getLength()));
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

(4)传递一个long型的数据
UDP-client

import java.io.*;
import java.net.*;

public class Client {

    public static void main(String[] args) {
        long num = 10000L;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream das = new DataOutputStream(baos);
        DatagramPacket dp = null;
        DatagramSocket ds = null;
        try {
            das.writeLong(num);
            byte array[] = baos.toByteArray();
            dp = new DatagramPacket(array,array.length,new InetSocketAddress("127.0.0.1",4321));
            ds = new DatagramSocket(8888);
            ds.send(dp);
            ds.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

UDP-server

import java.io.*;
import java.net.*;

public class Server {

    public static void main(String[] args) {
        byte array[] = new byte[1024];
        long num = 0;
        DatagramPacket dp = new DatagramPacket(array,array.length);
        ByteArrayInputStream bais = null;
        DataInputStream dis = null; 

        try {   
            DatagramSocket ds = new DatagramSocket(4321);
            while(true){
                ds.receive(dp);
                bais = new ByteArrayInputStream(array);
                dis = new DataInputStream(bais);
                num = dis.readLong();
                System.out.println(num);
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值