Socket编程-write()从Java Servlet到服务器C的固定字节数
需要一些帮助来读取从java客户端到C服务器的确切字节。流动比如说,我想读取前两个字节,然后知道发送的是哪个字符串/总字节数,因此使用recv_函数,该函数将实际字节数作为参数。这样我就可以限制等待时间,而不是读取所有预期的1024个缓冲区大小。还有,我想知道如何从java端发送一个struct,使其整洁。 非常感谢Socket编程-write()从Java Servlet到服务器C的固定字节数,java,c,sockets,servlets,Java,C,Sockets,Servlets,需要一些帮助来读取从java客户端到C服务器的确切字节。流动比如说,我想读取前两个字节,然后知道发送的是哪个字符串/总字节数,因此使用recv_函数,该函数将实际字节数作为参数。这样我就可以限制等待时间,而不是读取所有预期的1024个缓冲区大小。还有,我想知道如何从java端发送一个struct,使其整洁。 非常感谢 //*************** Java Client ***************** protected void doGet(H
//*************** 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();
}
这样我就可以限制等待时间,而不是读取所有预期的1024个缓冲区大小
但recv并不是这样的。它在阻塞模式下阻塞,直到至少有一个字节可用,然后传输任何可用的字节,直到指定的长度。它不会尝试填充缓冲区,除非碰巧有那么多数据可用
因此,您的问题根本不存在。您的代码很难阅读,因为它的缩进和空格的使用到处都是。考虑一下这个问题,这样我们就可以更容易理解了。如果你这样做,你将大大增加获得良好帮助的机会。具体来说,将每个块缩进4个空格,不要使用制表符,避免空行超过1行,并尽量少用空行。好运不,不,你不是。我已经手动修复了您的第一个代码块,以便缩进有意义。请以同样的方式处理其他文件。注释:OutputStreamWriter OutputStreamWriter=new OutputStreamWriter KmiOutStream,StandardCbarsets.UT_8;否则将采用默认服务器的编码。很高兴知道。指定的长度是指发送的确切字节数?所以EJP,我希望我的recv使用发送的确切字节长度。我有3条if语句写入我的C服务器。使用recv,我不知道如何做到这一点。我给recv打两次电话吗??我很困惑,你必须提前发送长度,然后写一个循环,接收直到你得到这个长度,在你前进的过程中增加偏移参数,减少长度参数。