首页 新闻 会员 周边 捐助

Java Socket通信

0
悬赏园豆:10 [已解决问题] 解决于 2015-04-02 20:35

当Socket端和ServerSocket建立连接以后,使用BufferedReader和BufferedWriter作为输入输出流读取和发送数据,如果Socket端使用两个线程分别实现收发数据的功能,那么当接收数据的线程被readLine方法阻塞时,另一个线程能否使用write方法向ServerSocket端成功的发送数据?

Mayb...的主页 Mayb... | 初学一级 | 园豆:156
提问于:2015-04-01 16:58
< >
分享
最佳答案
0

可以的,双向管道,在Client中,在主线程中发送数据之前,将TestSocket使用readLine()方法阻塞,然后主线程发送数据,在Server端直接读取,以下为具体实现

package com.mayb.Net.TCP;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
    public static BufferedReader read;
    public static BufferedWriter send;
    public static Socket client;
    public static boolean flag = true;

    public static void main(String[] args) throws UnknownHostException, IOException {
        client = new Socket("localhost", 8888);

        read = new BufferedReader(new InputStreamReader(client.getInputStream()));
        send = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        new TestSocket(client).start();
        try {
            Thread.currentThread().sleep(2000);
        } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        System.out.println("发送数据");
        send.write("yes ,you can\n");
        send.flush();
    }
}

class TestSocket extends Thread {
    private Socket c;

    public TestSocket (Socket socket) {
        // TODO 自动生成的构造函数存根
        c = socket;
    }
    @Override
    public void run() {
        try {
            System.out.println("准备读取.....");
            Client.read.readLine();
            System.out.println("读取结束.....");

            System.out.println("关闭Client");
            c.close();
            Client.flag = false;
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }

    }
}

//服务端实现
package com.mayb.Net.TCP;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {

        ServerSocket server = new ServerSocket(8888);
        Socket sss = server.accept();
        BufferedReader read = new BufferedReader(new InputStreamReader(sss.getInputStream()));
        BufferedWriter send = new BufferedWriter(new OutputStreamWriter(sss.getOutputStream()));
        try {
            Thread.currentThread().sleep(3000);
        } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        String string = read.readLine();
        System.out.println(string + "One Client has connected");
        while (Client.flag)
            ;
        System.out.println("关闭Server");
        try {
            Thread.currentThread().sleep(40000);
        } catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        server.close();
    }

}
Mayb... | 初学一级 |园豆:156 | 2015-04-02 20:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册