gpt4 book ai didi

java - 如何让服务器以多行响应? ( java )

转载 作者:行者123 更新时间:2023-12-02 00:11:27 24 4
gpt4 key购买 nike

我想从文件中获取文本,然后将其发送到服务器以使其大写,然后再发送回打印输出的客户端。我该如何实现这一目标?

我可以读取一行并将其发送回客户端,并且我已设法将多行写入输出流(供服务器读取),但我不知道现在该怎么办..

我有一个从文件中读取文本的客户端:

import java.io.*;
import java.net.*;
import java.util.Date;

public class Client
{
public static void main(String[] args)
{
try
{
// First create the input from keyboard
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client Program");

// Next we need to find out the IP address and port number of the server
System.out.print("Enter IP Address of server: ");
String ip = input.readLine();
System.out.print("Enter port number of server: ");
String port_string = input.readLine();

// The port number needs to be an int, so convert the string to an int
int port = Integer.parseInt(port_string);

// Connect to the server
Socket sock = new Socket(ip, port);


// Create the incoming stream to read messages from
DataInputStream network = new DataInputStream(sock.getInputStream());

//Create the output stream to the client
DataOutputStream message = new DataOutputStream(sock.getOutputStream());

//Send message
//message.writeUTF("some text");
FileReader file = new FileReader("text.dat");
BufferedReader input_file = new BufferedReader(file);

// Loop until EOF
while (input_file.ready()){
// Read next line from the file
String line = input_file.readLine();
// Write line to server
message.writeUTF(line + "\n");
//System.out.println(line);
}

// Display our address
System.out.println("Address: " + sock.getInetAddress());
String line;

// Loop until the connection closes, reading from the network
while ((line = network.readUTF()) != null)
{
// Display the received message
System.out.println(line);
}
}
catch (IOException ioe)
{
// This is expected when the server closes the network connection
System.err.println("Error in I/O");
System.err.println(ioe.getMessage());
System.exit(-1);
}
}
}

然后服务器应该接受这些字符串并将它们大写:

import java.io.*;
import java.net.*;
import java.util.*;

public class Server
{
public static void main(String[] args)
{
try
{
// First create the input from the keyboard
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Server Program");

// Get the port to listen on
System.out.print("Enter port number to listen on: ");
String port_string = input.readLine();

// The port number needs to be an int, so convert the String to an int
int port = Integer.parseInt(port_string);

// Create a ServerSocket to listen on this address
ServerSocket server = new ServerSocket(port);

// Accept an incoming client connection on the server socket
Socket sock = server.accept();

// Create the output stream to the client
DataOutputStream network = new DataOutputStream(sock.getOutputStream());

//Create the incoming stream to read messages from
DataInputStream message = new DataInputStream(sock.getInputStream());



String newLine = inFromClient.readLine();

//Line to read
String line;
line = message.readUTF();
line = line.toUpperCase();




// Send message
network.writeUTF(newLine);

// Close sockets. This will cause the client to exit
sock.close();
server.close();
}
catch (IOException ioe)
{
System.err.println("Error in I/O");
System.err.println(ioe.getMessage());
System.exit(-1);
}
}
}

最佳答案

问题是您的服务器仅读取一次流并关闭套接字连接。您的服务器如何知道您已完成将客户端数据发送到服务器套接字?您应该修改服务器以监听端口,直到发送完整个文本。为此,请执行以下操作 -

String newLine;
while ( true )
newLine = inFromClient.readLine();
if (newLine.equalsIgnoreCase("END"))
{
break;
}

newLine = newLine.toUpperCase();

// Send message
network.writeUTF(newLine);
}
// Close sockets. This will cause the client to exit

sock.close();
server.close();

在发送完所有行后,从客户端发送“END”。

关于java - 如何让服务器以多行响应? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12746476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com