Java Sockets Streams发送字符串和文件

时间:2015-12-10 23:53:00

标签: java sockets

写信给客户后我什么都没得到。服务器和客户端无法发送和接收文件和字符串。

服务器代码:

System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Connected to Server");
//DataInputStream inputStream = new DataInputStream(server.getInputStream());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
// OutputStream os = server.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(server.getOutputStream()), true);
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
//Start Authrentication

在客户端

System.out.println("Connection Established");
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
//  OutputStream outToServer = client.getOutputStream();
//   DataOutputStream out = new DataOutputStream(outToServer); // output from Client
InputStream inFromServer = client.getInputStream();    // Input from Server
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Hallo Server");
String got = br.readLine();
System.out.println(got +" 1 ");
while (!Objects.equals(got, "OK")) {
    System.out.println("Iam stuck");
    String newstatment = br.readLine();
    System.out.println(newstatment +" 1 ");
    if (Objects.equals(newstatment, "Please enter your username!")) {
        out.write(clientUsername);
    }

打印后连接到服务器时,它应该开始发送使用pw.Write

while (!authenticate) {
    //BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
    pw.write("Please enter your username!");
    String gotUser = in.readLine();
    if (Objects.equals(gotUser, this.username)) {
        pw.write("Please enter your password!");
        String gotPass = in.readLine();
        if (Objects.equals(gotPass, this.password)) {
            authenticate = true;
            pw.write("OK");

但似乎没有回复。

0 个答案:

没有答案