[英]Socket programming - write() fixed number of bytes from Java Servlet to server C
need some help reading the exact bytes from my java client side to C server. 需要一些帮助,将确切的字节从我的Java客户端读取到C服务器。 STREAM.
流。 I would like to read, lets say, the first two bytes then know which (string/Total No of bytes) are being sent thus use my recv_exactly() function which will take in the actual number of bytes as an argument.
我想阅读,可以说前两个字节,然后知道正在发送哪个(字符串/总字节数),因此使用我的recv_exactly()函数,该函数将实际的字节数作为参数。 This is so that I can limit the wait time instead of reading all 1024 expected buffer size.
这样一来,我可以限制等待时间,而不是读取所有1024个预期的缓冲区大小。 AND also, any ideas how i can send a struct from the java side to make this neat.
而且,任何想法,我如何可以从Java端发送一个结构,使这一整洁。 Thanks a bunch!
谢谢一群!
//*************** Java Client *****************
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
sslsocket.startHandshake();
kmipoutstream = sslsocket.getOutputStream();
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(kmipoutstream);
// figure out what we want to ask for
final String path = request.getPathInfo();
System.out.println("request pathInfo: " + path);
if (path == null || path.endsWith("/users")) {
//***********SHOULD I SEND 13 FIRST = 2 BYTES?? ***************
outputstreamwriter.write("13")
outputstreamwriter.write("GET ALL USERS");
} else if (path.endsWith("/keys")) {
outputstreamwriter.write("GET ALL KEYS");
} else if (path.endsWith("/templates")) {
outputstreamwriter.write("GET ALL TEMPLATES");}
outputstreamwriter.flush();
BufferedReader wireBufReader = new BufferedReader(
new InputStreamReader(sslsocket.getInputStream()));
String tmp = wireBufReader.readLine();
System.out.println(tmp);
int numrows = Integer.parseInt(tmp);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-control", "max-age=0");
response.setHeader("Content-Range", "0-" + numrows + "/" + numrows);
while (!(tmp = wireBufReader.readLine()).isEmpty()) {
response.getOutputStream().println(tmp);}
kmipoutstream.close();
}
//********** Server.C *******************
inBuf = calloc(1, 1024);
if (inBuf == NULL){
debug_print("ERROR: Memory allocation for inbuf.\n", NULL);
endProcessing = 1;}
printf("This is inBuf= %s \n", *inBuf);
while (!endProcessing){
sts = RS_SUCCESS;
do{
//do accept, followed by negotiate
sts = rs_ssl_negotiate_viaAccept(rs_ssl_env, IOMODE_NONBLOCKING,
listenerSocket, &rs_ssl_conn, &ssllog);
printf("After negotiate and accept sts = %d\n", sts);
if (RS_SUCCESS != sts){
debug_print("ERROR: Error during accept and negotiate: %d\n", sts);
rslog_print(ssllog);
break;
/*
* receive the get request, parse it out, and call the db method.
*/
//memset(inBuf, 0, 1024);
//*******I NEED HELP HERE. NOT SURE HOW TO KNOW THE EXACT NUMBER OF
BYTES BEING SENT BEFORE I CALL
RS_SSL_RECV_EXACTLY ********************
if( = rs_ssl_recv_exactly(rs_ssl_conn, inBuf, 2, &ssllog)){
debug_print("ERROR: During HTTP receive: %d\n", sts);
rslog_print(ssllog);
break; }
else
{ if (sts = db_get_userlist_json(jInfo->db_ctx, &jsonBuf, &numrows))
{debug_print("error getting json user: %d\n", sts);
break;}}
__atoe_l(inBuf, actualBytes);
debug_print("successfully received %d bytes of request:\n<%s>\n", bytesRecvd, inBuf);
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
sslsocket.startHandshake();
kmipoutstream = sslsocket.getOutputStream();
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(kmipoutstream);
// figure out what we want to ask for
final String path = request.getPathInfo();
System.out.println("request pathInfo: " + path);
if (path == null || path.endsWith("/users")) {
// ******** SHOULD I SEND 13 FIRST = 2 BYTES?? ***********
outputstreamwriter.write("13");
outputstreamwriter.write("GET ALL USERS");
} else if (path.endsWith("/keys")) {
outputstreamwriter.write("GET ALL KEYS");
} else if (path.endsWith("/templates")) {
outputstreamwriter.write("GET ALL TEMPLATES");
}
outputstreamwriter.flush();
BufferedReader wireBufReader = new BufferedReader(
new InputStreamReader(sslsocket.getInputStream()));
String tmp = wireBufReader.readLine();
System.out.println(tmp);
int numrows = Integer.parseInt(tmp);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-control", "max-age=0");
response.setHeader("Content-Range", "0-" + numrows + "/" + numrows);
while (!(tmp = wireBufReader.readLine()).isEmpty()) {
response.getOutputStream().println(tmp);
}
kmipoutstream.close();
}
This is so that I can limit the wait time instead of reading all 1024 expected buffer size.
这样一来,我可以限制等待时间,而不是读取所有1024个预期的缓冲区大小。
But recv() doesn't behave like that. 但是recv()的行为并非如此。 It blocks until at least one byte is available (in blocking mode), then transfers whatever is available, up to the specified length.
它会阻塞,直到至少有一个字节可用(在阻塞模式下),然后再传输所有可用字节,直到指定的长度。 It makes no attempt to fill the buffer, unless that much data happens to be available.
除非有大量数据可用,否则它不会尝试填充缓冲区。
So your problem doesn't exist in the first place. 因此,您的问题根本就不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.