简体   繁体   中英

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:

    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 :

#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

1) Try calling writeBytes(String s) on the DataOutputStream.

2) Try calling flush on the DataOutputStream (even though close should be calling flush anyway).

3) Try using PrintWriter to send the String

See

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

4) Also, I think you should swap these two

            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. The first byte of the first character is 0. printf sees the 0 and thinks it's the end of the string. Try formatting the string to UTF-8 when you write it to the socket or reading it and printing it as a wchar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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