java网络编程

TCP传输文本

方式1.客户端

package gctest.TCP;

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

public class CLient {
    public static void main(String[] args) throws IOException {
        //1.创建客户端Socket对象
        Socket socket=new Socket("localhost",6666);
        //2.通过Socket对象获取输出流对象
        OutputStream out=socket.getOutputStream();
        //3,通过输出流对象写入数据
        out.write("helloworld".getBytes());
        out.close();
        socket.close();
    }
}


方式1.服务端

package gctest.TCP;

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

public class Server {
    public static void main(String[] args) throws IOException {
        //1.创建服务端socket对象
        ServerSocket ss=new ServerSocket(6666);
        //2.通过ServerSocket对象获取Socket对象
        //ss.accept()阻塞式方法,直到有客户端与之建立链接才会继续向下执行
        Socket socket=ss.accept();
        //3.通过Socket对象获取输入流对象
        InputStream in=socket.getInputStream();
        //4.通过输入流对象读取数据
        int len;
        byte[] b=new byte[1024];
        while((len=in.read(b))!=-1){
            System.out.println(new String(b,0,len));
        }
        in.close();
        socket.close();
    }
}

方式2.客户端

package gctest.TCP;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client2 {

    public static void main(String[] args) throws IOException {
        Scanner sc=new Scanner(System.in);
        while(true){
            Socket socket=new Socket("localhost",6677);
            //2.获取输出流对象
            OutputStream out=socket.getOutputStream();
            String words=sc.next();
            if("over".equals(words)){
                out.close();
                socket.close();
                break;
            }
            out.write(words.getBytes());
            out.close();
            socket.close();
        }
    }
}

方式2.服务端

package gctest.TCP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server2 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss=new ServerSocket(6677);
        System.out.println("服务端已开启等待客户端链接。。");
        while(true){
            Socket socket=ss.accept();
            String ip=socket.getInetAddress().getHostA#ddress();
            new Thread(()->{
                try {
                    InputStream in=socket.getInputStream();
                    BufferedReader br=new BufferedReader(new InputStreamReader(in));
                    System.out.println(ip+":"+br.readLine());
                    in.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}

TCP传输文件

方式3.客户端

package gctest.TCP;

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

public class Client3 {
    public static void main(String[] args) throws IOException {
        Socket socket=new Socket("localhost",8899);
        OutputStream out=socket.getOutputStream();
        FileInputStream fis=new FileInputStream("D:\\MyFile\\img\\zcl1.png");
        byte[] buf=new byte[1024];
        int len;
        while((len=fis.read(buf))!=-1){
            out.write(buf,0,len);
        }
        socket.shutdownOutput();
        //从服务端接收到返回的信息
        InputStream in=socket.getInputStream();
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        System.out.println(br.readLine());
        in.close();
        fis.close();
        out.close();
        socket.close();
    }
}

方式3.服务端

package gctest.TCP;

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

public class Server03 {
    public static void main(String[] args) throws IOException {
         //创建ServerSocket对象
        ServerSocket ss=new ServerSocket(8899);
        System.out.println("Server working");
        while(true){
            Socket socket=ss.accept();
            new Thread(()->{
                try {
                    InputStream in=socket.getInputStream();
                    FileOutputStream fos=new FileOutputStream("zcl.png");
                    int len;
                    byte[] b=new byte[1024];
                    while((len=in.read(b))!=-1){
                        fos.write(b,0,len);
                    }
                    //获取输出流想客户端发送的数据
                    OutputStream out=socket.getOutputStream();
                    out.write("success".getBytes());
                    out.close();
                    fos.close();
                    in.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}

UDP

发送端:

package gctest.TCP;

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

public class UDPSend {
    public static void main(String[] args) throws Exception {
        //基于UDP协议创建Socket对象 无需指定端口号IP地址
        DatagramSocket ds=new DatagramSocket();
        //创建数据包对象
        byte[] buf="你好".getBytes();
        DatagramPacket dp=new DatagramPacket(buf,buf.length, InetAddress.getByName("localhost"),9999);
        //发送数据包
        ds.send(dp);
        //释放资源
        ds.close();
    }
}


接收端:

package gctest.TCP;

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

public class UDPReceive {
    public static void main(String[] args) throws Exception {
        //1.基于UDP协议创建socket对象
        DatagramSocket ds=new DatagramSocket(9999);
        byte[] buf=new byte[1024];
        DatagramPacket dp=new DatagramPacket(buf,buf.length);
        //3.接受数据包 receive阻塞式方法
        ds.receive(dp);
        //4.解析数据包的数据
        byte[] data=dp.getData();
        InetAddress inetAddress=dp.getAddress();
        //从数据包中获取端口号
        int port=dp.getPort();
        System.out.println(new String(data,0,dp.getLength()));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值