从Java客户端接收数据(数据包含INT,短,字符串) [英] Receiving data from Java-Client (data contains int, short, string)

查看:183
本文介绍了从Java客户端接收数据(数据包含INT,短,字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找了几个小时,以获取有关我的问题的答案,但没有发现任何东西。也许我得到一些帮助这里。

I was searching for hours to get an answer about my question, but didnt find anything. Maybe I get some help here.

我试图做的事:
一个Java客户端发送消息到C-服务器。该邮件包含不同的类型,如整型,短,也是一个字符串(如消息= INT:total_msg_length;短:手术;字符串:你好 - > total_msg长度= 4(整数大小),操作= 2(短尺寸) ,你好= 5(每封信是1字节= 5)。

What I'm trying to do: A Java-Client sends a message to a C-Server. The message contains different types like integer, short and also a string (e.g. message = int: total_msg_length; short: operation; string: hello --> total_msg-length=4 (size of integer), operation = 2 (size of short), hello = 5 (each letter is 1 byte=5).

那么,怎样才能我在服务器收到消息?下面的code接收一个整数(正常工作)。下一步将收到一个短,然后一个字符串(转换在US-ASCII)。

So, how can I receive the message in my server? The code below receives an Integer (works fine). Next step will be to receive a short and then a string (converted in US-ASCII).

int *msg;
int recv_size;
int final_msg;

if( (recv_size = recv(client_socket, &msg, sizeof(msg), 0 )) < 0 ){
    error_exit("Fehler bei recv(message_len)");
}

final_msg = endian_swap(msg);
printf("Message: %d\n", final_msg);

return final_msg;

有没有使用一个字节数组,而不是字符缓冲区的方法吗? IM感谢每一个帮助。请原谅我的英语不好,我来自德国:-)

Is there a way to use a byte array instead of char buffer? Im thankful for every help. Please excuse my bad english, I'm from germany :-)

推荐答案

您需要创建一个通用的read_n_bytes功能。

You need to create a generic "read_n_bytes" function.

这可以用来读取邮件大小,操作和文字,在三个连续调用。

This you can use to read the message-size, the operation and the text, in three successive calls.

这三个调用,那么你在函数换到被调用来读整个消息。

Those three calls you then wrap in a function to be called to read an entire message.

一个普通的读者可能会是这样的:

A generic reader might look like this:

/*
 * Reads n bytes from sd into where p points to.
 *
 * returns 0 on succes or -1 on error.
 *
 * Note: 
 * The function's name is inspired by and dedicated to "W. Richard Stevens" (RIP).
 */
int readn(int sd, void * p, size_t n)
{
  size_t bytes_to_read = n;
  size_t bytes_read = 0;

  while (bytes_to_read > bytes_read)
  {
    ssize_t result = read(sd, p + bytes_read, bytes_to_read);
    if (-1 == result)
    {
      if ((EAGAIN == errno) || (EWOULDBLOCK == errno))
      {
        continue;
      }

#     ifdef DEBUG     
      {
        int errno_save = errno;
        perror("read() failed");
        errno = errno_save;
      }
#     endif

      break;
    }
    else if(0 == result)
    {
#     ifdef DEBUG
      {     
        int errno_save = errno;
        fprintf(stderr, "%s: Connection closed by peer.", __FUNCTION__);
        errno = errno_save;
      }
#     endif

      break;
    }

    bytes_to_read -= result;
    bytes_read += result;
  }

  return (bytes_read < bytes_to_read) ?-1 :0; 
}

这篇关于从Java客户端接收数据(数据包含INT,短,字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆