Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C套接字服务器向Java套接字客户端接收数据时出错_Java_C_Sockets - Fatal编程技术网

从C套接字服务器向Java套接字客户端接收数据时出错

从C套接字服务器向Java套接字客户端接收数据时出错,java,c,sockets,Java,C,Sockets,我在尝试在简单Java套接字客户端和简单C套接字服务器之间来回发送字符串时遇到问题 其工作原理如下: Java客户端向C服务器发送msg out.writeBytes(msg); C服务器接收消息并放入buf if ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); } C服务器将消息发送回Java客户端 if (

我在尝试在简单Java套接字客户端和简单C套接字服务器之间来回发送字符串时遇到问题

其工作原理如下:

Java客户端向C服务器发送
msg

    out.writeBytes(msg);
C服务器接收消息并放入
buf

    if ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) {
        perror("recv");
        exit(1);
    }
C服务器将消息发送回Java客户端

    if ((len = send(new_fd, buf, numbytes, 0)) == -1)
        perror("send");
public void send(String text){
    Socket sock = null;
    DataInputStream in = null;
    DataOutputStream out = null;
    BufferedReader inReader = null;

    try {
        sock = new Socket(HOST, PORT);
        System.out.println("Connected");
        in = new DataInputStream(sock.getInputStream());
        out = new DataOutputStream(sock.getOutputStream());

        out.writeBytes(text + "\n");

        String res = "";

        //get msg from c server and store into res

        System.out.println("Response: " + res);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            sock.close();
            //in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
但是Java客户端无法从C服务器接收消息,我尝试使用
DataInputStream
BufferedReader
,读取
char[]
byte[]
,转换为
字符串
,但无法获取收到的消息。当试图在客户端读取数组时,我有时只得到第一个字符,这让我相信这是一个阻塞问题

任何帮助都将不胜感激

代码:

C服务器主循环

while(1) {  // main accept() loop
    sin_size = sizeof their_addr;
    new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
    if (new_fd == -1) {
        perror("accept");
        continue;
    }

    inet_ntop(their_addr.ss_family,
        get_in_addr((struct sockaddr *)&their_addr),
        s, sizeof s);
    printf("server: got connection from %s\n", s);

    if ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) {
        perror("recv");
        exit(1);
    }

    buf[numbytes] = '\0';

    printf("msg size: '%d' bytes\n",numbytes);
    printf("received msg: %s\n",buf);
    char* array = (char*)buf;
    printf("as char array: %s\n", array);


    if (!fork()) { // this is the child process
        close(sockfd); // child doesn't need the listener
        int len = 0;
        if ((len = send(new_fd, buf, numbytes, 0)) == -1)
            perror("send");
        printf("sent %d bytes\n", len);
        close(new_fd);
        exit(0);
    }
    close(new_fd);  // parent doesn't need this
}
Java客户端

    if ((len = send(new_fd, buf, numbytes, 0)) == -1)
        perror("send");
public void send(String text){
    Socket sock = null;
    DataInputStream in = null;
    DataOutputStream out = null;
    BufferedReader inReader = null;

    try {
        sock = new Socket(HOST, PORT);
        System.out.println("Connected");
        in = new DataInputStream(sock.getInputStream());
        out = new DataOutputStream(sock.getOutputStream());

        out.writeBytes(text + "\n");

        String res = "";

        //get msg from c server and store into res

        System.out.println("Response: " + res);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            sock.close();
            //in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

为什么要使用
分叉
?如果直接在
accept
之后执行,并且在循环中执行大量读/写操作,则会更有意义。在这里,您执行一次接收和一次发送,然后关闭连接,不需要
fork
。这里没有从套接字读取任何内容的Java代码。不清楚你在问什么。