【问题标题】:Unable to receive data from the server无法从服务器接收数据
【发布时间】:2013-12-17 11:48:48
【问题描述】:

这是我的 c 服务器端代码。我正在向 java 上的客户端打招呼

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include<errno.h>
#include<arpa/inet.h>

#define PORT 6865
#define BUF 256

int main(int argc, char *argv[])
{
 struct sockaddr_in host, remote;
 int host_fd, remote_fd;
 int size = sizeof(struct sockaddr);;

int read_t,i;
char data[]="hello";

host.sin_family = AF_INET;
host.sin_addr.s_addr = htonl(INADDR_ANY);
host.sin_port = htons(PORT);
memset(&host.sin_zero, 0, sizeof(host.sin_zero));

host_fd = socket(AF_INET, SOCK_STREAM, 0);
if(host_fd == -1) {
    printf("socket error %d\n", host_fd);
    return 1;
}

if(bind(host_fd, (struct sockaddr *)&host, size)) {
    perror("bind error is\n");
printf("errorno is %d\n",errno);
    return 1;
}

if(listen(host_fd, 10)) {
    printf("listen error");
    return 1;
}

printf("Server setup, waiting for connection...\n");
remote_fd = accept(host_fd, (struct sockaddr *)&remote, &size);

printf("connection made\n");

//read_t = recv(remote_fd,data,sizeof(data),0);
//data[read_t]='\0';
printf("read = %d, data = %s \n", sizeof(data), data);
if(sendto(remote_fd,data,sizeof(data),0,NULL,1)==-1)
{
    printf("error in sending back\n");
    return 1;
}
    read_t = recv(remote_fd,data,sizeof(data),0);
data[read_t]='\0';
    printf("read = %d, received_data = %s \n", sizeof(data), data);
shutdown(remote_fd, SHUT_RDWR);
close(remote_fd);

return 0;
}  

这是java代码

  package com.java.client;

 import java.io.BufferedInputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.net.Socket;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;

  public class ClientFirst
  {
 public static void main(String[] argv) throws UnknownHostException, IOException 
 {
     char[] data = new char[10];
     Socket s = new Socket("10.9.79.80", 6865);


     InputStreamReader ins= new  InputStreamReader(s.getInputStream());
         int da= ins.read(data, 0, 9);
         System.out.print(Integer.toString(da));

    }
 }

当我尝试从 c 服务器接收数据时,我没有得到它。 我得到的是 5,而不是打招呼。

【问题讨论】:

    标签: java c sockets


    【解决方案1】:

    您打印的是从流中读取的字符数,而不是实际读取的数据。试试这个:

    int da = ins.read(data, 0, 9);
    System.out.print( data );
    

    干杯,

    【讨论】:

    • @Anders R. Bystrup Thanx Man
    【解决方案2】:

    尝试打印出datavariable,而不是变量daInputStreamReader.read的返回值,像这样:

    System.out.print(data);
    

    InputStreamReader.read 改为返回读取的字符数。

    【讨论】:

      【解决方案3】:

      da 是 5,因为您从套接字读取了 5 个字节。 hello 由 5 个字节组成,所以一切正常。您收到的信件已存储在变量data中。

      【讨论】:

        猜你喜欢
        • 2015-05-18
        • 1970-01-01
        • 2020-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多