简体   繁体   English

CURL:客户端和服务器通信Windows c / java

[英]CURL: CLient and Server communication Windows c/java

I am relatively new at curl and I am trying to do a Client in C which sends strings using curl. 我在curl方面比较陌生,我正在尝试使用C语言做一个Client,它使用curl发送字符串。 On the other side, as a Server, I have a Java program which is listening to HTTP requests using HttpServlet. 另一方面,作为服务器,我有一个Java程序正在使用HttpServlet侦听HTTP请求。 When I send with curl some data I see that the server gets it but not in the format that was sent. 当我用curl发送一些数据时,我看到服务器可以接收到它,但是不是以发送的格式。 For example if I am sending data: "8D015678" In server side Request content will be: 56, 68, 48, 49, 53, 54, 55, 56 Here is Client code(c): 例如,如果我正在发送数据:“ 8D015678”在服务器端,请求内容将是:56,68,48,49,53,54,55,56这是客户端代码(c):

int status = 0;
char * ip_to_connect = "http://127.0.0.1:5004/unilateral
const char * request = "8D015678";
status = curl_easy_setopt(curl, CURLOPT_URL, ip_to_connect);

if (!status)
{
    status = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
}
if (!status)
{
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(request));
}
if (!status)
{
    status =  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
}
if (!status)
{
    status = curl_easy_perform(curl);
}

Here is parser code of Server(java): 这是Server(java)的解析器代码:

   System.out.println("[requestHandlerFactory] header is not Content");
    int contentLength = request.getContentLength();
    this.content = new byte[contentLength];
    try {
        ServletInputStream input = request.getInputStream();
        int read = input.read(content, 0, contentLength);
        System.out.println("[doPost] contentLength = " + contentLength + "data = " + content[0]);

Does this mean that I need to have a convertor from ASCI to symbol? 这是否意味着我需要从ASCI到symbol的转换器? I thought in curl data is passed as is? 我以为curl数据按原样传递? What am I missing? 我想念什么? If I want my data, eg. 如果我想要我的数据,例如。 8D015678 to be gotten by server as 8D015678 how shall I send it? 服务器将8D015678作为8D015678来发送? Please some explanation on how data is passed and how to dealwith client in curl and server receiving those requests Thanks in advance. 请对数据如何传递以及如何处理curl和服务器接收这些请求中的客户端进行一些说明,在此先感谢。 Hope it is clear 希望清楚

InputStream.read(..) returns -1 when the stream has ended, otherwise the value can be trivially converted to an char. 流结束时, InputStream.read(..)返回-1,否则该值可以简单地转换为char。 This combination of return value and error code is old and rather unfortunate and would have been designed differently today. 返回值和错误代码的这种组合是过时的,很不幸,今天的设计可能有所不同。 Just convert it to a char and process it. 只需将其转换为char并进行处理即可。

The official documentation is at https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read() 官方文档位于https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()

See http://www.tutorialspoint.com/java/io/inputstream_read.htm for a working example. 有关工作示例,请参见http://www.tutorialspoint.com/java/io/inputstream_read.htm

The essence is: 本质是:

  // new input stream created
         is = new FileInputStream("C://test.txt");

         System.out.println("Characters printed:");

         // reads till the end of the stream
         while((i=is.read())!=-1)
         {
            // converts integer to character
            c=(char)i;

            // prints character
            System.out.print(c);
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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