简体   繁体   English

Java客户端-服务器聊天(服务器未接收到字符串)

[英]Java client-server chat (server not receiving string)

trying to make a simple client server chat program. 尝试制作一个简单的客户端服务器聊天程序。 I've already go it so that the server reads a user input which is then sent to the client. 我已经去过了,以便服务器读取用户输入,然后将其发送到客户端。 the client then receives this and displays it. 客户端然后接收并显示它。 I then have the client reading the user input and sending it to the server however the server doesn't receive it. 然后,我让客户端读取用户输入并将其发送到服务器,但是服务器未收到它。 Heres my code: 这是我的代码:

Client: 客户:

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


class client {
   public static void main(String args[]) {
      try {
         Socket skt = new Socket("localhost", 1234);
         BufferedReader in = new BufferedReader(new
         InputStreamReader(skt.getInputStream()));
         System.out.print("Waiting for server to respond... \n");

         while (!in.ready()) {}
         System.out.print("Received Message: ");
         System.out.println(in.readLine()); // Read one line and output it

         System.out.print("\n");
         //in.close();

         System.out.print("Message:");
         Scanner sc = new Scanner (System.in);
         String data2 = sc.nextLine();
         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         System.out.print("Sending Message: '" + data2 + "'\n");
         out.print(data2);
         out.close();


      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}

Server: 服务器:

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



class server {


   public static void main(String args[]) {

      try {
         System.out.println("Waiting for client connection...");
         ServerSocket srvr = new ServerSocket(1234);
         Socket skt = srvr.accept();
         System.out.println("Client has connected!\n");
         System.out.print("Message:");
         Scanner sc = new Scanner (System.in);
         String data = sc.nextLine();
         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         System.out.print("Sending Message: '" + data + "'\n");
         out.print(data);

         BufferedReader in = new BufferedReader(new
         InputStreamReader(skt.getInputStream()));


         while (!in.ready()) {out.close();}      
         System.out.print("Received Message: ");
         System.out.println(in.readLine()); // Read one line and output it
         System.out.print("\n");


         //in.close();

         //in.close();
         //out.close();
         //skt.close();
         //srvr.close();
      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}

Both the client and the server use the same code for send/receive so I don't understand why the server won't receive. 客户端和服务器都使用相同的代码进行发送/接收,所以我不明白为什么服务器无法接收。

Cheers, 干杯,

Tom 汤姆

print() send the text but not a new line. print()发送文本,但不发送新行。 Try println() instead. 请尝试使用println()。

readLine() on the server waits for a new line, which you are not sending. 服务器上的readLine()等待不发送的新行。

BTW: I wouldn't test for in.ready() this is more likely to be confusing than useful IMHO. 顺便说一句:我不会测试in.ready()这比有用的恕我直言更容易造成混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM