Java 客户端应用程序未从服务器应用程序接收字符串输出
我打算让服务器应用程序向客户端发送字符串对象 应用程序,并让它打印出发送的内容,直到从 服务器等于“停止” 下面的源是服务器应用程序Java 客户端应用程序未从服务器应用程序接收字符串输出,java,sockets,network-programming,Java,Sockets,Network Programming,我打算让服务器应用程序向客户端发送字符串对象 应用程序,并让它打印出发送的内容,直到从 服务器等于“停止” 下面的源是服务器应用程序 /* Server application */ import java.util.*; import java.io.*; import java.net.*; class FunServer{ public static void main(String args[])throws Exception { Ser
/*
Server application
*/
import java.util.*;
import java.io.*;
import java.net.*;
class FunServer{
public static void main(String args[])throws Exception
{
ServerSocket ss = new ServerSocket(1234);
Socket link = ss.accept();
System.out.println("Connection has been established");
//Read
Scanner reader = new Scanner(link.getInputStream());
//Write
PrintWriter writer = new PrintWriter(link.getOutputStream());
Scanner keyboard = new Scanner(System.in);
String send;
while(true){
System.out.println("Please, enter your input.");
send = keyboard.nextLine();
if(send.equalsIgnoreCase("stop")){
break;
}
wrter.println(send);
}
link.close();
}
}
下面的源代码是客户端应用程序
import java.util.*;
import java.io.*;
import java.net.*;
class FunClient{
public static void main(String args[])throws Exception
{
String output;
InetAddress local = InetAddress.getLocalHost();
Socket link = new Socket(local, 1234);
System.out.println("Connection Successful.");
Scanner reader = new Scanner(link.getInputStream());
PrintWriter writer = new PrintWriter(link.getOutputStream());
while(true){
output = writer.nextLine();
if(output.equals("close")){
break;
}
System.out.println(output);
}
link.close();
}
}
用户输入有效字符串后,客户端中不会打印任何内容
应用程序
import java.util.*;
import java.io.*;
import java.net.*;
class FunClient{
public static void main(String args[])throws Exception
{
String output;
InetAddress local = InetAddress.getLocalHost();
Socket link = new Socket(local, 1234);
System.out.println("Connection Successful.");
Scanner reader = new Scanner(link.getInputStream());
PrintWriter writer = new PrintWriter(link.getOutputStream());
while(true){
output = writer.nextLine();
if(output.equals("close")){
break;
}
System.out.println(output);
}
link.close();
}
}
我是否以错误的方式使用扫描仪和打印机
谢谢。也许您不明白Socket.getInputStream()和Socket.getInputStream()的作用
Socket.getInputStream()
返回一个InputStream,您可以从中读取该InputStream以获取从对等方发送给您的数据。您从未在客户端或服务器上读取套接字的输入流,因此您从未看到对等方发送的任何内容。感谢您的建议,我将继续讨论这些概念。