学习Java二十三天网络编程

使用网络编程实现互相对话:

服务器端

package csdn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ServerSocketDemo {
    public static void main(String[] args) {
        //1.创建ServerSocker类型的对象并提供端口号;
        Scanner scanner = new Scanner(System.in);
        Socket socket = null;
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(6666);
            //2.等待客户端的连接请求,调用accept方法;
            System.out.println("客户端正在连接。。。。。。");
            socket = ss.accept();
            System.out.println("客户端已经连接");
            //3.使用输入输出流进行通信
            BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintStream ps = new PrintStream(socket.getOutputStream());

            String s = bf.readLine();
            System.out.println("客户端说:" + s);
            String s1 = scanner.next();
            ps.println(s1);
            //4.关闭Socker并释放有关资源
            ps.close();
            bf.close();
            ss.close();
            socket.close();

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

    }
}

客户端:

package csdn;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class SockerDemo {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

                //1.创建Socker类型的对象并提供服务器的通信地址和端口号;
        Socket socket = new Socket("127.0.0.1",6666);
        //3.使用输入输出流进行通信
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintStream ps  =new PrintStream(socket.getOutputStream());

        
        System.out.println("请输入信息:");
        String s1 = scanner.next();
        ps.println(s1);

        String s = br.readLine();
        System.out.println("服务器说:"+s);

        // 4.关闭Socker并释放有关资源
        br.close();
        ps.close();
        socket.close();

    }
}

 现在更改,实现可以无限对话,直到客户端说”再见“,双方断开连接

服务器:

 

package csdnall;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ServerSocketDemo {
    public static void main(String[] args) {
        //1.创建ServerSocker类型的对象并提供端口号;
        Scanner scanner = new Scanner(System.in);
        Socket socket = null;
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(6666);
            //2.等待客户端的连接请求,调用accept方法;
            System.out.println("客户端正在连接。。。。。。");
            socket = ss.accept();
            System.out.println("客户端已经连接");
            //3.使用输入输出流进行通信
            BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintStream ps = new PrintStream(socket.getOutputStream());

            while(true){
                String s = bf.readLine();
                System.out.println("客户端说:" + s);
                if("再见".equals(s)){
                    break;
                }else {
                    System.out.println("服务器请说:");
                    String s1 = scanner.next();
                    ps.println(s1);
                    System.out.println("服务器发送成功");
                }
            }
            //4.关闭Socker并释放有关资源
            ps.close();
            bf.close();
            ss.close();
            socket.close();

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

    }
}

客户端:

package csdnall;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class SockerDemo {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

                //1.创建Socker类型的对象并提供服务器的通信地址和端口号;
        Socket socket = new Socket("127.0.0.1",6666);
        //3.使用输入输出流进行通信
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintStream ps  =new PrintStream(socket.getOutputStream());


        while(true){
            System.out.println("请输入信息:");
            String s1 = scanner.next();
            //ps.println(s1);
            if ("再见".equals(s1)){
                ps.println(s1);
                System.out.println("发送成功");
                break;
            }else {
                ps.println(s1);
                System.out.println("发送成功");
                String s = br.readLine();
                System.out.println("服务器说:"+s);
            }



        }



        // 4.关闭Socker并释放有关资源
        br.close();
        ps.close();
        socket.close();

    }
}

改为多线程程序,即为可以接受多个客户端连接:

服务器端:

package csdnthread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ServerSocketDemo {
    public static void main(String[] args) throws IOException {
        //1.创建ServerSocker类型的对象并提供端口号;
        Scanner scanner = new Scanner(System.in);
        Socket socket = null;
        ServerSocket ss = null;
        ss = new ServerSocket(6666);
        try {

            while(true){

                //2.等待客户端的连接请求,调用accept方法;
                System.out.println("客户端正在连接。。。。。。");
                socket = ss.accept();
                System.out.println(socket.getInetAddress()+"客户端已经连接");
                new Thread(new SockerDemo1(socket)).start();
            }


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

    }
}

服务器多线程端:

package csdnthread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class SockerDemo1 implements Runnable {
    private Socket socket;
    public SockerDemo1(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedReader bf = null;
        PrintStream ps = null;
        Scanner scanner = new Scanner(System.in);
        try {
            bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            ps = new PrintStream(socket.getOutputStream());
            while(true){
                String s = bf.readLine();

                    System.out.println(socket.getInetAddress()+"客户端说:" + s);
                    System.out.println("服务器请说:");
                    String s1 = scanner.next();
                    ps.println(s1);
                    System.out.println("服务器发送成功");



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

客户端:

package csdnthread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class SockerDemo {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

                //1.创建Socker类型的对象并提供服务器的通信地址和端口号;
        Socket socket = new Socket("127.0.0.1",6666);
        //3.使用输入输出流进行通信
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintStream ps  =new PrintStream(socket.getOutputStream());


        while(true){
            System.out.println("请输入信息:");
            String s1 = scanner.next();
            ps.println(s1);
            System.out.println("发送成功");
            //ps.println(s1);
            if ("再见".equals(s1)){
                break;
            }else {

                String s = br.readLine();
                System.out.println("服务器说:"+s);
            }



        }



        // 4.关闭Socker并释放有关资源
        br.close();
        ps.close();
        socket.close();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值