• 服务器 客户端 连续 发送实现2--socket


    首先感谢我宿友廖某的指导
    服务器

    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 Server {
    	public static void main(String[] args) throws IOException {
    		ServerSocket server = new ServerSocket(5050);
    		while (true) {
    			Socket client = server.accept();
    			System.out.println(client.getPort() + "已上线");
    			clientserver c = new clientserver(client);
    			Scanner sc = new Scanner (System.in);
    			String str = sc.nextLine();
    			c.setName(str);
    			c.start();
    		}
    	}
    }
    class clientserver extends Thread {
    	Socket client;
    	clientserver(Socket socket) {
    		this.client = socket;
    	}
    	public void run() {
    		System.out.println("与" + client.getInetAddress() + "连接成功");
    		try {
    			BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));
    			PrintStream print = new PrintStream(client.getOutputStream());
    			boolean flag = true;
    			while (flag) {
    				String s = input.readLine();
    				if (s.equals("bye")) {
    					flag = false;
    					System.out.println(client.getInetAddress()+"退出连接");
    				} else {
    					System.out.println(s);
    				}
    			}
    		} catch (Exception e) {
    			System.out.println("连接异常" + client.getPort() + "已下线");
    		}
    	}
    }
    

    客户端

    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 Client extends Thread {//把客户端弄成线程的话 就可以实现客户端持续给服务器发信息 不用对方收到
    	Socket client;
    	Client(Socket client) {
    		this.client = client;
    	}
    		while (true) {
    			try {
    				PrintStream print = new PrintStream(client.getOutputStream());
    				Scanner scanner = new Scanner(System.in);
    				String send = scanner.nextLine();
    				print.println(send);
    			} catch (Exception e) {
    				System.out.println(e);
    			}
    		}
    	}
    	public static void main(String[] args) {
    		try {
    			Socket socket = new Socket("localhost", 5050);
    			Client c = new Client(socket);
    			c.start();
    			System.out.println("与" + socket.getInetAddress() + "连接成功");
    			try {
    				BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    				while (true) {
    					String str = input.readLine();
    					System.out.println(str);
    				}
    			} finally {
    				socket.close();
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    https://blog.csdn.net/o0DarkNessYY0o/article/details/52410980

  • 相关阅读:
    js json 与字符串 转换过程由于书写不统一规范引发的一个问题
    asp.net 运行时,"未能映射路径"
    android html 图片处理类--加载富文本工具类
    System.Web.HttpCompileException (0x80004005): (0): error CS0016: 未能写入输出文件
    “请运行Chkdsk工具”怎么解决
    android studio新项目时提示:Plugin is too old, please update to a more recent version
    mvvm和mvc区别?
    什么是mvvm?
    js去除html标签
    数组的翻转(非reverse)
  • 原文地址:https://www.cnblogs.com/cznczai/p/11148101.html
Copyright © 2020-2023  润新知