2017-01-30 33 views
0

私は比較的新しいcurlで、curlを使用して文字列を送るCでクライアントを実行しようとしています。一方、サーバーとして、私はHttpServletを使用してHTTP要求を聞いているJavaプログラムを持っています。 私はいくつかのデータをカールして送信すると、サーバーはそれを取得したものの、送信された形式ではないことがわかります。例えば Iがデータを送信していた場合:サーバー側の要求内容がなるで "8D015678" :ここでは56、68、48、49、53、54、55、56 は、クライアントコードが(C)である。CURL:クライアントとサーバーの通信Windows c/java

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); 
} 
ここ

は、サーバーのパーサコード(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]); 

これは、私は、シンボルへのASCIからコンバータを持っている必要があることを意味していますか? カールのデータがそのまま渡されると思いましたか?私は何が欠けていますか? 私のデータが必要な場合、たとえば。 8D015678をサーバから8D015678として受け取るにはどうすればいいですか? データがどのように引き渡され、クライアントがこれらの要求を受け取るカールとサーバで対処するかに関する説明をお願いします。 ありがとうございます。

答えて

0

ストリームの終了時に-1を返します。それ以外の場合は、値を簡単にcharに変換できます。この戻り値とエラーコードの組み合わせは、古くて残念であり、今日は別の方法で設計されていました。それをcharに変換して処理するだけです。

公式ドキュメントはhttps://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()

であるが働い例えば http://www.tutorialspoint.com/java/io/inputstream_read.htm を参照してください。

本質は次のとおりです。

// 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); 
     } 
関連する問題