简体   繁体   English

通过套接字将字符串从Java(PC)发送到C(Raspberry Pi)

[英]Sending string from Java (PC) to C (Raspberry Pi) via Socket

I have two apps, one written in Java that sends string, it looks like this: 我有两个应用程序,其中一个用Java编写,可以发送字符串,看起来像这样:

    import java.io.*;
import java.net.*;
public class Gniazdo{
        public static void main(String[] args) throws IOException {
            DataOutputStream dataOutputStream = null;
            Socket socket = null;
            String mes = "Test message";
            try {
                socket = new Socket("192.168.1.116", 4014);
                dataOutputStream = new DataOutputStream(socket.getOutputStream());

                dataOutputStream.writeChars(mes); 
            } catch (UnknownHostException e) {
                System.err.print(e);

            } finally {
                if(socket != null) {
                    socket.close();
                }
                if(dataOutputStream != null) {
                    dataOutputStream.close();
                }
            }
        }

    }

and second one is written in C and runs on Raspberry Pi, that should receive string : 第二个是用C编写的,并在Raspberry Pi上运行,应接收字符串:

#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>

int main(void) {
    unsigned int port;
    char bufor[1024];
    int gniazdo, gniazdo2;
    struct sockaddr_in adr, nadawca;
    socklen_t dl;

    printf("Enter port to listen : ");
    scanf("%u", &port);
    gniazdo = socket(AF_INET, SOCK_STREAM, 0);
    adr.sin_family = AF_INET;
    adr.sin_port = htons(port);
    adr.sin_addr.s_addr = INADDR_ANY;
    if (bind(gniazdo, (struct sockaddr*) &adr, 
             sizeof(adr)) < 0) {
        printf("Bind failed.\n");
        return 1;
    }
    if (listen(gniazdo, 10) < 0) {
        printf("Listen failed.\n");
        return 1;        
    }
    printf("Waiting for connection ...\n");
    while (gniazdo2 = accept(gniazdo,
                      (struct sockaddr*) &nadawca,
                      &dl)
          ) 
    {
   //     memset(bufor, 0, 1024);
        recv(gniazdo2, bufor, 1024, 0);
        printf("message from %s: %s\n", inet_ntoa(nadawca.sin_addr), bufor);
        close(gniazdo2);
    }
    close(gniazdo);    
}

and my output is: 我的输出是:

Enter port to listen : 4014
Waiting for connection ...
message from 192.168.1.103:
message from 192.168.1.103:
message from 192.168.1.103:
message from 192.168.1.103:

Please could anyone tell me whats wrong? 请谁能告诉我怎么了? Why i don't receive the message? 为什么我没有收到消息?

I even checked packets outgoing from my computer with Wireshark and they contains my message, but still don't know where I made mistake 我什至用Wireshark检查了从计算机传出的数据包,其中包含我的消息,但仍然不知道我在哪里弄错了

1) Try calling writeBytes(String s) on the DataOutputStream. 1)尝试在DataOutputStream上调用writeBytes(String s)。

2) Try calling flush on the DataOutputStream (even though close should be calling flush anyway). 2)尝试在DataOutputStream上调用flush(即使close无论如何仍应调用flush)。

3) Try using PrintWriter to send the String 3)尝试使用PrintWriter发送字符串

See 看到

http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html#PrintWriter%28java.io.OutputStream,%20boolean%29 http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html#PrintWriter%28java.io.OutputStream,%20boolean%29

and this method in particular 特别是这种方法

http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html#write%28java.lang.String%29 http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html#write%28java.lang.String%29

4) Also, I think you should swap these two 4)另外,我认为您应该交换这两个

            if(socket != null) {
                socket.close();
            }
            if(dataOutputStream != null) {
                dataOutputStream.close();
            }

printf is looking at 'bufor' as a char (8-bit), but java will write 16-bit UTF-16 characters. printf将'bufor'视为char(8位),但是java将写入16位UTF-16字符。 The first byte of the first character is 0. printf sees the 0 and thinks it's the end of the string. 第一个字符的第一个字节为0。printf看到0,并认为它是字符串的结尾。 Try formatting the string to UTF-8 when you write it to the socket or reading it and printing it as a wchar. 将字符串写入套接字或读取并将其打印为wchar时,请尝试将字符串格式化为UTF-8。

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

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