简单字符串发送客户端服务器处理程序java

cx6n0qe3  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(257)

我想用java编程一个非常简单的服务器-客户机对,这意味着我有一个服务器,可以在localhost/port下显示字符串。因此,如果我打开网站,它会显示如下内容:

Hello World
Hi
How are you?

客户机都在使用同一个程序,但他们看到的页面完全相同。每个客户端都可以在需要时将当前页附加几行。
我做了很多研究,但还没有找到一个好的例子。
目前,我的程序是这样的:服务器:

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

public class DomServer {
    public static void main(String[] args) throws IOException {
        // don't need to specify a hostname, it will be the current machine
        ServerSocket ss = new ServerSocket(7777);
        System.out.println("ServerSocket awaiting connections...");
        Socket socket = ss.accept(); // blocking call, this will wait until a connection is attempted on this port.
        System.out.println("Connection from " + socket + "!");

        // get the input stream from the connected socket
        InputStream inputStream = socket.getInputStream();
        // create a DataInputStream so we can read data from it.
        DataInputStream dataInputStream = new DataInputStream(inputStream);

        // read the message from the socket
        String message = dataInputStream.readUTF();
        System.out.println("The message sent from the socket was: " + message);

        System.out.println("Closing sockets.");
        //ss.close();
        //socket.close();
    }
}

客户:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class DomClient {
    public static void main(String[] args) throws IOException {
        // need host and port, we want to connect to the ServerSocket at port 7777
        Socket socket = new Socket("localhost", 7777);
        System.out.println("Connected!");

        // get the output stream from the socket.
        OutputStream outputStream = socket.getOutputStream();
        // create a data output stream from the output stream so we can send data
        // through it
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);

        System.out.println("Sending string to the ServerSocket");

        // write the message we want to send
        boolean terminate = false;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 

        while(!terminate) {
            String text = reader.readLine();
            if(text.equals("end")) {
                terminate = true;
            }
            dataOutputStream.writeUTF(text);
            dataOutputStream.flush(); // send the message

        }
        dataOutputStream.close(); // close the output stream when we're done.

        System.out.println("Closing socket and terminating program.");
        socket.close();
    }
}

这是一个修改过的版本:https://gist.github.com/chatton/8955d2f96f58f6082bde14e7c33f69a6
目前,我遇到的问题是,当我输入第三行时,连接会自动关闭。我得到java.net.socketexception:管道断开(写入失败)。此外,我不知道如何解决与消耗品字符串网站的问题。
有没有人编写过类似的程序?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题