首页 \ 问答 \ 通过套接字java发送字节数组而不发送长度(Sending byte array through socket java without sending length)

通过套接字java发送字节数组而不发送长度(Sending byte array through socket java without sending length)

我有一个问题。 我想通过套接字发送一个字节数组,但我不想发送它的长度。 我是从服务器端读取的吗?

我尝试过这样的事情:

dos = new DataOutputStream(socket.getOutputStream);
dos.write(myByteArray);

并在服务器端

byte data = new data[100];
dis.read(data);

但是这样,我读了100个字节或者没有字节。 我可以读取确切的号码。 我发送的字节数? 谢谢


I hava a question. I want to send an array of bytes through a socket but I don't want to send the length of it. Cand I read it from a server side?

I've tried something like this:

dos = new DataOutputStream(socket.getOutputStream);
dos.write(myByteArray);

and on the server side

byte data = new data[100];
dis.read(data);

but in this way, I read 100 bytes or another no of bytes. Can I read exacty the no. of bytes I send? Thanks


原文:https://stackoverflow.com/questions/34267523
更新时间:2022-07-04 14:07

最满意答案

你没有做任何事情告诉Django这些表格是分开的。 他们俩都只有一个name字段; 什么都没有告诉他们分开。

一个简单的解决方案是将prefix参数用于表单实例化,以便Django使字段唯一。

if request.is_ajax():
    form1 = NewCategory(request.POST or None, prefix='form1')
    if form1.is_valid():
        ...
else:
    form1 = NewCategory(prefix='form1')

if request.is_ajax():
    form2 = NewServiceIn(request.POST or None, prefix='form2')
    if form2.is_valid():
        ...
else:
    form2 = NewServiceIn(prefix='form2')

You haven't done anything to tell Django that the forms are separate. They both only have a name field; there is nothing at all to tell them apart.

A simple solution for this would be to to use the prefix parameter to the forms instantiations, so that Django makes the fields unique.

if request.is_ajax():
    form1 = NewCategory(request.POST or None, prefix='form1')
    if form1.is_valid():
        ...
else:
    form1 = NewCategory(prefix='form1')

if request.is_ajax():
    form2 = NewServiceIn(request.POST or None, prefix='form2')
    if form2.is_valid():
        ...
else:
    form2 = NewServiceIn(prefix='form2')

相关问答

更多
  • 你没有做任何事情告诉Django这些表格是分开的。 他们俩都只有一个name字段; 什么都没有告诉他们分开。 一个简单的解决方案是将prefix参数用于表单实例化,以便Django使字段唯一。 if request.is_ajax(): form1 = NewCategory(request.POST or None, prefix='form1') if form1.is_valid(): ... else: form1 = NewCategory(prefix=' ...
  • 如果项目存在,您可以检查for_sale_detail并返回HttpResponseNotFound或者如果没有则返回Http404异常 。 You can check on for_sale_detail if the item exists and return HttpResponseNotFound or raise Http404 exception if not.
  • 你可以试试这个。 from django.http import HttpResponse from django.template.loader import render_to_string def search_query(request): var_1 = Products.objects.all() var_2 = request.GET.get("q") if var_2: items = items.filter( Q(First__ ...
  • 获得要删除的图像列表后,将其传递给将由Django视图处理的Ajax调用,如下所示: function submitData(data) { var csrftoken = getCookie('csrftoken'); // from https://docs.djangoproject.com/en/1.7/ref/contrib/csrf/#ajax $.ajax({ type: 'POST', beforeSend: function(xhr, se ...
  • 如果您打算通过Ajax返回JavaScript可用于更新DOM的响应,则需要返回一个HttpResponse ,最好使用JSON格式,可以在您的$ .ajax调用中将其传递给处理程序。 例: import json from django.contrib.auth.models import User from django.http import HttpResponse from django.shortcuts import render def index(request): if r ...
  • 回答第一条评论中提出的问题,如果你有 Django中的django.template.context_processors.request > = 1.8 Django中的django.core.context_processors.request <1.8 将上下文预处理器添加到TEMPLATE_CONTEXT_PROCESSORS设置,然后您可以通过{{ request.resolver_match.app_name }}访问模板中当前应用的名称。 您可以通过上下文提供model_name ,如果您不使 ...
  • 这里有几个明显的错误。 首先,你张贴到“/ ajax_demo /”,但你没有作为一个URL; 只有“/ demo /”。 您确实要更改网址格式或您要发布的网址。 其次,您错误地包含了应用程序模式; 不得使用包含“$”的终端。 它应该是: url(r'^$', include( 'app.urls', namespace = "mainPage")), 第三,你没有在你的请求中包含任何数据。 你需要一个data密钥: $ .ajax({url:'/ demo /',输入:'get',data:{“clus ...
  • 我想我在这里找到了答案: 从远程访问localhost上的站点 显然,在没有安全问题的情况下,无法从其他网络访问本地主机。 I think I found the answer here: Access to a site on localhost from remote Apparently local host cannot be accessed from another network without security issues.
  • 您可以尝试在ajax调用中检查响应文本: $('.delete').click(function(){ url_delete = 'my-url.com; $.ajax({ type: 'GET', url: url_delete, data:$(".delete_reserv").serialize(), success:function(data) { if(data == 'removed') { ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。