[英]Java Socket programming with TCP not working
I've been trying to write a client process that will communicate with a serve. 我一直在尝试编写将与服务进行通信的客户端流程。
I used the same code below but instead of scanner i used BufferedReader and the client input was a string. 我在下面使用了相同的代码,但不是使用BufferedReader,而是使用BufferedReader,而客户端输入是一个字符串。 Furthermore, the server just changes the string to uppercase and sends it back to the client to be displayed on the screen. 此外,服务器仅将字符串更改为大写,然后将其发送回客户端以在屏幕上显示。 It worked. 有效。
However, when i changed the code so that the client can enter a double number [(ex: 1.6) the server should round it and send it back so that it'll be printed on the screen], i get no response. 但是,当我更改代码以使客户端可以输入双精度数字[(例如:1.6)时,服务器应将其取整并发送回去,以便将其打印在屏幕上],但我没有任何反应。
And i got this error: 我得到了这个错误:
run:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at socketserver.SocketServer.main(SocketServer.java:38)
Java Result: 1
notes: 笔记:
Client class 客户类
package socketclient;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class SocketClient {
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws Exception
{
double inputDouble;
double modifiedNum;
Scanner inFromUser = new Scanner(System.in);
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
Scanner inFromServer = new Scanner(clientSocket.getInputStream());
inputDouble = inFromUser.nextDouble();
outToServer.writeDouble(inputDouble);
modifiedNum = inFromServer.nextDouble();
System.out.println("FROM SERVER: " + modifiedNum);
outToServer.close();
clientSocket.close();
}
}
Server class 服务器类
package socketserver;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class SocketServer {
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws Exception
{
double clientNum;
double roundedNum;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
Scanner inFromClient = new Scanner(connectionSocket.getInputStream());
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
clientNum = inFromClient.nextDouble();
roundedNum= Math.round(clientNum);
outToClient.writeDouble(roundedNum);
}
}
}
Your error is to use Scanner
to read data sent by client from the server and the converse. 您的错误是使用Scanner
读取客户端从服务器发送的数据,反之亦然。
You use DataOutputStream
to sent a double
so you need a DataInputStream
to read the double
; 您使用DataOutputStream
发送一个double
DataOutputStream
,因此需要一个DataInputStream
来读取double
DataInputStream
; on both sides. 双方。
Change your Scanner
s to DataInputStream
for inputFromServer
and inputFromClient
. 将您的Scanner
更改为DataInputStream
,以用于inputFromServer
和inputFromClient
。 Also don't forget to flush
your outputs and close
the streams at the end. 同样不要忘记flush
输出并最后close
流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.