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

时间:2013-12-09 22:02:46

标签: java c sockets raspberry-pi

我有两个应用程序,一个用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();
                }
            }
        }

    }

,第二个用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);    
}

我的输出是:

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:

请有人告诉我什么是错的? 为什么我没有收到消息?

我甚至用Wireshark检查了从我的电脑传出的数据包,它们包含了我的信息,但仍然不知道我在哪里犯了错误

2 个答案:

答案 0 :(得分:0)

1)尝试在DataOutputStream上调用writeBytes(String s)。

2)尝试在DataOutputStream上调用flush (尽管close应该调用flush)。

3)尝试使用PrintWriter发送字符串

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#write%28java.lang.String%29

4)另外,我认为你应该交换这两个

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

答案 1 :(得分:0)

printf将'bufor'视为char(8位),但java将写入16位UTF-16字符。第一个字符的第一个字节是0. printf看到0并认为它是字符串的结尾。将字符串写入套接字或读取并将其打印为wchar时,请尝试将字符串格式化为UTF-8。