希望你没事,我开始学习java socket编程和处理客户机服务器,我在理解io库的不同类和对象时遇到了很多问题,我真的需要有人帮我总结一下。printstream,printwriter,dataoutputstream,outputstream之间的区别是什么,我猜还有outputstreamwriter。我们什么时候使用每一个(举例说明会很好)?如果我要求太多,请回复我一个youtube视频或一些外部链接。请不要引用eclipse的话来回复我。下面是我一直在编写的一个代码,它使客户机的消息变成大写:
◙ 服务器代码:
package clientServer;
import java.net.*;
import java.io.*;
public class Server{
public static void main(String[] args) throws IOException{
ServerSocket incoming = new ServerSocket(9999);
System.out.println("Connecting...");
Socket client = incoming.accept();
System.out.println("Client is connected");
String messageIn,messageOut;
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
messageIn = in.readLine();
messageOut = messageIn.toUpperCase();
PrintStream outnet = new PrintStream(client.getOutputStream());
outnet.print(messageOut + "\n");
outnet.flush();
outnet.close();
client.close();
incoming.close();
}
}
◙ 客户代码:
package clientServer;
import java.net.*;
import java.io.*;
public class Client{
public static void main(String[] args) throws IOException{
Socket client = new Socket("localhost",9999);
System.out.println("Connected to the Server");
PrintStream outnet = new PrintStream(client.getOutputStream());
String message;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.print("Veillez tappez quelque chose: ");
message = input.readLine();
outnet.print(message + "\n");
// outnet.flush();
// outnet.close();
message = in.readLine();
System.out.println("Server: " + message);
client.close();
}
}
当我使用printstream或dataoutputstream时,代码运行良好,但当我使用printwriter或其他东西时,代码运行不好。
暂无答案!
目前还没有任何答案,快来回答吧!