简体   繁体   English

从C客户端向Java服务器发送字符串时出错

[英]error while sending a String from C client to Java server

I send an integer from a C client to a java server and it worked perfectly. 我从C客户端向Java服务器发送一个整数,它运行良好。 But when i tried to do the same thing with a string i got and error this is the client code to send the String 但是,当我尝试对得到的字符串执行相同的操作并且出错时,这是发送字符串的客户端代码

char clientString[30];

    printf("String to send : \n");

        if( send( to_server_socket, &clientString, sizeof( clientString ), 0 ) != sizeof( clientString ) )
        {
            printf( "socket write failed");
            exit( -1 );
        }

And the java code to read it 和读取的Java代码

DataInputStream din = new DataInputStream(socket.getInputStream());
          String clientString=din.readUTF();
           System.out.println(clientString);

Error 错误

java.io.EOFException at java.io.DataInputStream.readFully(DataInputStream.java:180) at java.io.DataInputStream.readUTF(DataInputStream.java:592) at java.io.DataInputStream.readUTF(DataInputStream.java:547) at ServiceRequest.run(ServiceRequest.java:43) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) at java.lang.Thread.run(Thread.java:680) java.io.DataInputStream.readFully(DataInputStream.java:180)处的java.io.EOFException java.io.DataInputStream.readUTF(DataInputStream.java:547)处java.io.DataInputStream.readUTF(DataInputStream.java:592)在ServiceRequest.run(ServiceRequest.java:43)在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:439)在java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:303)在java.util.concurrent.ThreadPoolExecutor $ Worker.runTask(ThreadPoolExecutor.java:895)上的java.util.concurrent.FutureTask.run(FutureTask.java:138)在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor) java:918),位于java.lang.Thread.run(Thread.java:680)

EDIT :I tried using din.readLine() ,I don't have the error anymore but if i type fffffff12 on the client i got fffffff12`?7E^Ê?h in the server 编辑:我尝试使用din.readLine() ,我不再有错误,但是如果我在客户端上输入fffffff12,我会在服务器中得到fffffff12`?7E ^Ê?h

You send all of the data in the clientString array, no matter how long the input really is. 无论输入多长时间,您将在clientString数组中发送所有数据。 Terminate the string properly and only send eg strlen(clientString) bytes instead. 正确终止字符串,而仅发送例如strlen(clientString)字节。

Maybe the problem is that on the client side you are writing ASCII string but on the server side you are reading UTF, try to read the data as ASCII, as well if possible please mention the exception that occurred. 可能的问题在于,在客户端上您正在编写ASCII字符串,而在服务器端上您正在读取UTF,请尝试将数据读取为ASCII,并且如果可能的话,请提及发生的异常。 This method can raise two exceptions: 1- An IO Exception or 2- An EOF exception. 此方法可能引发两个异常:1- IO异常或2- EOF异常。

readUTF doesn't just read bytes from a socket. readUTF不仅从套接字读取字节。 It starts by reading the length of the string (as a 16-bit integer) and then reading the string. 首先读取字符串的长度(作为16位整数),然后读取字符串。 The problem is that what you send is not what is required for readUTF to work successfully. 问题是您发送的内容不是readUTF成功运行所必需的。

As Joachim Pileborg noted, you are also sending the entire 30 bytes of clientString (including any remaining bytes that were not explicitly set). 正如Joachim Pileborg指出的那样,您还将发送clientString的整个30个字节(包括未明确设置的所有剩余字节)。 You should send it like this instead: 您应该这样发送:

send(to_server_socket, clientString, strlen(clientString), 0);

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

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