Java 学习之网络编程案例

网络编程案例

一,概念

1,网络编程不等于网站编程

2,编程只和传输层打交道,即TCP和UDP两个协议

二,案例

1,TCP实现点对点的聊天

Server端:两个输入流:读客户端和控制台,一个输出端:输出客户端

 

public class ChatServer {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket=new ServerSocket(44432);
            Socket socket=serverSocket.accept();//获取客户端的连接信息
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));//client 的输入内容
            PrintWriter printWriter =new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));//往client输出内容
            System.out.println("client say:"+bufferedReader.readLine());
            Scanner bufferedReader1=new Scanner(System.in);//控制台输入内容
            String message=bufferedReader1.next();
            while(!message.equals("end")) {
                printWriter.println(message);//send message to client
                printWriter.flush();
                System.out.println("client say:"+bufferedReader.readLine());// get message from client
                message=bufferedReader1.next();
            }
            bufferedReader.close();
            printWriter.close();
            bufferedReader1.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

Client端:两个输入流:读服务端和控制台,一个输出端:输出服务端

public class ChatClient {
    public static void main(String[] args) {
        try {
            Socket socket=new Socket("127.0.0.1",44432);//获取服务端的唯一标识
            PrintWriter printWriter=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));//BufferedWriter流也可以
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedReader bufferedReader1=new BufferedReader(new InputStreamReader(System.in));
            String message=bufferedReader1.readLine();
            while (!message.equals("end")){
                printWriter.write(message+"\r\n");//加\r\n,因为读取时是用readLine方式 ,等同于println方式
                printWriter.flush();//必须flush,不然另一端读不到。。。
                System.out.println("server say:"+bufferedReader.readLine());
                message=bufferedReader1.readLine();
            }
            bufferedReader.close();
            bufferedReader1.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2,UDP 实现高精度数字传输   通过数据报形式

接收端:

public class UdpServer {
    public static void main(String[] args) {
        byte[] buf=new byte[1024];//存储接受数据报的容器
        DatagramPacket datagramPacket=new DatagramPacket(buf,buf.length);//数据报内容写入容器
        try {
            DatagramSocket datagramSocket=new DatagramSocket(44423);
            while (true){
                datagramSocket.receive(datagramPacket);//接收发送方的数据报
                ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(buf);
                DataInputStream dataInputStream=new DataInputStream(byteArrayInputStream);
                System.out.println("clint say:"+dataInputStream.readDouble());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

发送端:

public class UdpClient {
    public static void main(String[] args) {
        double n=10000.00;
        try {
            ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
            DataOutputStream dataOutputStream=new DataOutputStream(byteArrayOutputStream);
            dataOutputStream.writeDouble(n);
            //创建代有数据报内容和目的地址
            DatagramPacket datagramPacket=new DatagramPacket(byteArrayOutputStream.toByteArray(),
                    byteArrayOutputStream.toByteArray().length,new InetSocketAddress("127.0.0.1",44423));
            DatagramSocket datagramSocket=new DatagramSocket(53421);//指定发送的端口
            datagramSocket.send(datagramPacket);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

 

 

 

 

 

posted @ 2017-04-27 17:14  DamonDr  阅读(371)  评论(0编辑  收藏  举报