小编Rox*_*Rox的帖子

我如何收听鼠标滚轮按?

有没有办法听老鼠轮按下(不移动滚轮,只是按下它)?

我已经检查了MouseWheelListener API,但鼠标滚轮按下没有任何东西,只是车轮移动.

java swing mousewheel

7
推荐指数
1
解决办法
2254
查看次数

当我使用原始套接字发送syn-packet时,为什么服务器不响应syn-ack数据包?

我正在尝试使用原始套接字,我刚刚编写了一个小程序,它发送带有syn标志集的TCP数据包.我可以在服务器端看到与Wireshark一起发送的数据包,它们看起来不错,但服务器从不响应任何syn-ack数据包.

我已经将syn我的程序构造的数据包(参见下面的代码)与hping3发送的数据包进行了比较(因为hping3的数据包总是得到一个syn-ack).我的syn包和hping3的syn包之间唯一不同的是ip identification数字tcp source port(在hping3中随机化)tcp sequence number(也在hping3中随机化)和ip checksum字段.所有这四个字段都基于一些随机数,这就是它们不同的原因.所有其他领域都是平等的!但我的程序没有得到任何同步,但hping3呢!

我正在使用Kali Linux发送数据包(当然是root用户)和CentOS用于服务器.

我错过了必要的东西或者只是误解了什么吗?

删除了代码

编辑
这是Wireshark在客户端捕获的整个数据包(下面分为4个图像).请注意,除了ip标识,源端口,序列号和校验和的值之外,hping3发送的数据包完全相同:

图像已删除


这是数据包的十六进制转储.

Hexdump已移除

编辑2

好了,现在我根据RFC793创建了伪标头.伪标头仅用于tcp校验和计算.现在IP头似乎是正确的,但Wireshark抱怨该数据包不包含一个完整的TCP头,它真的好像已经损坏,因为一些字段包含我没有设置的奇怪值.

首先,我tcp_header为tcp头和伪头分配一个带空格的缓冲区(称为).其次,我为包含ip,tcp和伪标头空间的ip头创建了一个缓冲区.
首先,我填充tcp_header其数据,然后我将其复制到ip_header使用该sendto函数发送之前.

做一些事情出错时,我复制的内容tcp_packetip_packet还是我做别的事情了?

#include <cstdlib>
#include <stdio.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#define __FAVOR_BSD 1
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include …
Run Code Online (Sandbox Code Playgroud)

c c++ tcp raw-sockets

7
推荐指数
1
解决办法
1515
查看次数

Java:同步ExecutorService是必要的吗?

我有一个包含ExecutorService的类,可以在线程之间共享,例如:

class MyExecutor {
    ExecutorService e = Executors.newSingleThreadExecutor();
    ....
    .... 
    public void add(Runnable r) {
         e.executre(r);
    } 
}
Run Code Online (Sandbox Code Playgroud)

是否有必要同步方法中的ExecutorService对象,add因为add可以从差异线程调用该方法,或者ExecutorService线程是否安全?

java synchronization executorservice

6
推荐指数
1
解决办法
2709
查看次数

SAXException2:在对象图中检测到一个循环.怎么回事?

我有一个Web服务,其中包含基于我拥有的数据库模式使用NetBeans生成的Java类文件.

我有时会遇到奇怪的例外,其中一个就是这个:

javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML: org.mylib.Person[ personId=1 ] ->org.mylib.TeamPerson[ teamPersonPK=org.mylib.teamPersonPK[ teamId=1, personId=1 ] ] -> org.mylib.Person[ personId=1 ]]
Run Code Online (Sandbox Code Playgroud)

我用google搜索了这个异常并找到了一些类似的情况,但我仍然无法理解这个问题.我刚刚使用NetBeans生成了这些类(Person.java,Team.java,TeamPerson.java),那么问题是如何发生的呢?

当我试图让所有人:

Iterator iter = team.getTeamPersonCollection().iterator();
while(iter.hasNext()) {
            Person person = ((TeamPerson)iter.next()).getPerson();
...
}
Run Code Online (Sandbox Code Playgroud)

编辑
如果我从TeamPerson中删除Team引用,我会收到以下错误:

Internal Exception: Exception [EclipseLink-7154] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [teamPersonCollection] in entity class [class org.mylib.Team] has a mappedBy value of [team] which does …
Run Code Online (Sandbox Code Playgroud)

java web-services sax

6
推荐指数
2
解决办法
9117
查看次数

Callable如何在引擎盖下工作?可调用对象如何返回值?

我试图了解如何Callable在一个不同的线程上运行时能够返回一个值.

我期待中的类Executors,AbstractExecutorService,ThreadPoolExecutor并且FutureTask,在所有可用的java.util.concurrent软件包.

您可以通过调用Executors中的方法(例如newSingleThreadExecutor())来创建ExecutorService对象.然后你可以传递一个Callable对象 ExecutorService.submit(Callable c).

由于该call()方法是由一个提供的线程运行的ExecutorService,返回的对象在哪里"跳转"回调用线程?

看看这个简单的例子:

1    ExecutorService executor = Executors.newSingleThreadExecutor();
2    public static void main(String[] args) {
3       Integer i = executor.submit(new Callable<Integer>(){
4           public Integer call() throws Exception {
5              return 10;
6           }
7       }).get();
8       System.out.print("Returns: " + i + " Thread: " + Thread.currentThread.getName());
9       // prints "10 main"
10    }
Run Code Online (Sandbox Code Playgroud)

如何将由单独线程运行的call方法中的整数返回到Integer对象(第3行),以便它可以通过System.out主线程(第7行)中的语句打印出来?

主线程是否有可能在 …

java multithreading

6
推荐指数
1
解决办法
1803
查看次数

是否有必要在catch-block中对事务进行回滚?

也许是一个愚蠢的问题,但是如果EntityManager.merge()抛出异常,是否有必要在catch-block中对事务进行回滚?或者异常本身是否意味着合并不起作用,以便下次运行commit时抛出异常的先前更改将不适用?

例:

public void setPerson(Person person) {
EntityManagerFactory emf =   Persistence.createEntityManagerFactory("MyLib");
     EntityManager em = emf.createEntityManager();
     try {            
         if(!em.getTransaction().isActive()){
            em.getTransaction().begin();
         }
         em.merge(person);
         em.getTransaction().commit();
         emf.getCache().evict(Person.class); // clear Person cache
     } catch (Exception ex){
         em.getTransaction().rollback(); // Is this necessary?   
     } finally {
         em.close();
     }
}
Run Code Online (Sandbox Code Playgroud)

java transactions eclipselink

5
推荐指数
1
解决办法
1824
查看次数

使用DTD文件中的JAXB生成Java类 - 如何修改DTD?

我想使用JAXB从dtd文件生成Java类.

dtd看起来像这样:

<!--Contents-->
    <!ELEMENT persons (header, content) >
    <!ELEMENT groups (header, content) >

<!--Header-->
    <!ELEMENT header (version) >
    <!ELEMENT version(#PCDATA) >

<!--Content-->
    <!ELEMENT content(person, group)* >

<!--Person-->
    <!ELEMENT person(p_id, p_name) >
    <!ELEMENT p_id (#PCDATA) >
    <!ELEMENT p_name (#PCDATA) >    

<!--Group-->
    <!ELEMENT group(g_id) >
    <!ELEMENT g_id(#PCDATA) >
Run Code Online (Sandbox Code Playgroud)

使用JAXB生成类时,我得到以下内容:

  • 的ObjectFactory
  • 内容

在Content类中,检索所有人和组的方法是

public List<Object> getPersonOrGroup() {
    if (personOrGroup == null) {
        personOrGroup = new ArrayList<Object>();
    }
    return this.personOrGroup;
}
Run Code Online (Sandbox Code Playgroud)

有没有什么我可以在dtd文件中更改,所以Java类的生成将在java类中进行personsgroups分离Content,所以要检索所有人和组将是Content.getPersons()和 …

java xml dtd jaxb

5
推荐指数
2
解决办法
1万
查看次数

如何从POST请求中检索cookie?

我可以使用发送POST请求org.apache.http.clien.HttpClient并获取HTTP响应.但是我登录时没有得到HTML内容,因为我的PHP脚本需要cookie.那么,如何在POST请求之后读取POST请求响应的cookie并使用GET请求将其发回?

    HttpClient httpClient = new DefaultHttpClient();

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("username", "user"));  
    nameValuePairs.add(new BasicNameValuePair("password", "passwd"));  

    HttpPost httpPost = new HttpPost("http://localhost/index.php");
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse httpResponse = httpClient.execute(httpPost);

    BufferedInputStream bis = new BufferedInputStream(httpResponse.getEntity().getContent()); // Just gets the HTML content, not the cookies
Run Code Online (Sandbox Code Playgroud)

java cookies httpclient http-post

5
推荐指数
1
解决办法
3万
查看次数

如何指定共享Docker卷的大小?

如果我想创建一个类似于ext4的15GB的数据量,我该怎么做?

docker volume create --name vol 只是创建一个空卷.

docker volume create --opt type=ext4 --name vol 创建一个ext4卷,但我无法指定它的大小,因为ext4根据ext4的挂载选项不支持它.

docker docker-volume

5
推荐指数
2
解决办法
7557
查看次数

用php渲染图像并用html &lt;img&gt;标签输出

我有一个带有几个可以渲染图像的函数的类。

// render.php
class Render {
   public function Render($some_arguments) {
      ...
      header("Content-Type: image/png");
        $im = @imagecreate(110, 20)
            or die("Cannot Initialize new GD image stream");
        $background_color = imagecolorallocate($im, 0, 0, 0);
        $text_color = imagecolorallocate($im, 233, 14, 91);
        imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
        imagepng($im);
        return "data:image/png;base64,".base64_encode($im);
   }
}
Run Code Online (Sandbox Code Playgroud)

然后,我有一个包含html代码的php文件,我想在< img>标记中输出图像:

// index.php
include("render.php");
$render = new Render();
echo "<htlm><head></head><body>";
echo "<img src=\"".$render->Render(1)."\" />";
echo "</body></html>";
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中运行index.php时,我只会得到一个空白屏幕。

我不能使用函数调用作为图像源吗?我知道我可以使用php文件作为源,例如< img src="render_image.php" />,但是后来我不能以面向对象的方式发送任何参数(我知道可以使用$ _GET来检索参数),但是我想使用一个很好的面向对象的方法来做书面代码。

那么,有什么方法可以将函数调用用作html标签的来源?

html php image render

4
推荐指数
1
解决办法
2万
查看次数