首页 \ 问答 \ 客户端/服务器JVM_Bind异常(Client/Server JVM_Bind exception)

客户端/服务器JVM_Bind异常(Client/Server JVM_Bind exception)

我正在尝试在java服务器和python客户端之间建立SSL连接

这是代码:

服务器端:

public class s implements Runnable {


List<SSLSocket> socketList= new ArrayList<SSLSocket>();
List<File> FileList= new ArrayList<File>();
List<Certificate> CertificateList = new ArrayList<Certificate>();

public static void main(String[] args) {
    s manager = new s();
    new Thread(manager).start();
    Scanner scanner = new Scanner(System.in);

    while(true){
        System.out.printf("Send> ");
        String message = scanner.nextLine();
        if(message.equals("") || message.equals("/n")){
            continue;
        }else{
            manager.send(message);
        }
    }
}

private static SSLServerSocket getServerSocket(int thePort)
    {
        SSLServerSocket s=null;
        try
        {
            String key="G:\\mySrvKeystore";
        char keyStorePass[]="123456".toCharArray();

        char keyPassword[]="123456".toCharArray();
        KeyStore ks= KeyStore.getInstance("JKS");

        ks.load(new FileInputStream(key),keyStorePass);


        KeyManagerFactory kmf= KeyManagerFactory.getInstance("SunX509");

        kmf.init(ks,keyPassword);

        SSLContext sslContext= SSLContext.getInstance("SSLv3");

        sslContext.init(kmf.getKeyManagers(),null,null);


        SSLServerSocketFactory factory=sslContext.getServerSocketFactory();

        s=(SSLServerSocket)factory.createServerSocket(thePort);

    }catch(Exception e)
    {
        System.out.println(e);
    }
    return(s);
}

public void run() {
    try {
        while (true) {
            SSLServerSocket sslserversocket = getServerSocket(**9991**);
            SSLSocket  client = (SSLSocket)sslserversocket.accept();
            socketList.add(client);
            new Thread(new SSocket(client,socketList,FileList)).start();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

客户端:

class timer(threading.Thread):
    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.ssl_sock = ssl.wrap_socket(self.sock, ssl_version=ssl.PROTOCOL_SSLv3)
        self.ssl_sock.connect(('localhost',9990))
        self.isrun = True
        threading.Thread.__init__(self);

def run(self): 
    while self.isrun:
        revice =  self.ssl_sock.recv(1024);
        print ("recv> " + revice); 
    self.sock.close();
    self.ssl_sock.close();

def send(self,str):
     self.ssl_sock.send(str + "\n")

def close(self):
    self.isrun=False

if __name__=='__main__':
     main()

我有:

在此处输入图像描述 我没有使用保留端口,我的操作系统是windows-7。 我首先运行服务器然后运行客户端。

我不明白为什么我不能运行该程序的多个实例? 是因为我使用的是同一个端口吗? 但是之前我只使用socket而不是SSLsocket就可以了。

谢谢你的帮助


I am trying to establish a SSL connection between java server and python client.

Here is the code:

Server side:

public class s implements Runnable {


List<SSLSocket> socketList= new ArrayList<SSLSocket>();
List<File> FileList= new ArrayList<File>();
List<Certificate> CertificateList = new ArrayList<Certificate>();

public static void main(String[] args) {
    s manager = new s();
    new Thread(manager).start();
    Scanner scanner = new Scanner(System.in);

    while(true){
        System.out.printf("Send> ");
        String message = scanner.nextLine();
        if(message.equals("") || message.equals("/n")){
            continue;
        }else{
            manager.send(message);
        }
    }
}

private static SSLServerSocket getServerSocket(int thePort)
    {
        SSLServerSocket s=null;
        try
        {
            String key="G:\\mySrvKeystore";
        char keyStorePass[]="123456".toCharArray();

        char keyPassword[]="123456".toCharArray();
        KeyStore ks= KeyStore.getInstance("JKS");

        ks.load(new FileInputStream(key),keyStorePass);


        KeyManagerFactory kmf= KeyManagerFactory.getInstance("SunX509");

        kmf.init(ks,keyPassword);

        SSLContext sslContext= SSLContext.getInstance("SSLv3");

        sslContext.init(kmf.getKeyManagers(),null,null);


        SSLServerSocketFactory factory=sslContext.getServerSocketFactory();

        s=(SSLServerSocket)factory.createServerSocket(thePort);

    }catch(Exception e)
    {
        System.out.println(e);
    }
    return(s);
}

public void run() {
    try {
        while (true) {
            SSLServerSocket sslserversocket = getServerSocket(**9991**);
            SSLSocket  client = (SSLSocket)sslserversocket.accept();
            socketList.add(client);
            new Thread(new SSocket(client,socketList,FileList)).start();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Client side:

class timer(threading.Thread):
    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.ssl_sock = ssl.wrap_socket(self.sock, ssl_version=ssl.PROTOCOL_SSLv3)
        self.ssl_sock.connect(('localhost',9990))
        self.isrun = True
        threading.Thread.__init__(self);

def run(self): 
    while self.isrun:
        revice =  self.ssl_sock.recv(1024);
        print ("recv> " + revice); 
    self.sock.close();
    self.ssl_sock.close();

def send(self,str):
     self.ssl_sock.send(str + "\n")

def close(self):
    self.isrun=False

if __name__=='__main__':
     main()

I got:

enter image description here I did not use reserve port and my OS is windows-7. And I run the server first then the client.

I don't understand why I cannot run multiple instance of the program? Is it because I am using the same port? But it was ok before when I just used socket instead of SSLsocket.

THanks for any help


原文:https://stackoverflow.com/questions/37370348
更新时间:2023-06-30 15:06

最满意答案

问题来自于你不能将有符号和无符号的arithemetics与整数混合在一起。 反正不是直接的。 你必须具体告诉你想要什么。 你提到的那一行给出零,因为你将一个uint16表示与(-1)相乘,默认情况下是从double重新转换为uint16 ,它截断为零( 0 )。 请改用int16

我使用一些更好的变量名称制作了更干净的代码版本(尽管有些人可能不同意)。 我发现使用数学上预期的“类型”作为变量名称(矩阵,向量等)的前缀很有用。 即使数据结构非常小,您也应该养成预分配数据的习惯。 我敢打赌,MATLAB编辑器可能会在右边缘发出恼人的警告,尖叫着你? :)

如果在vec_d的值中设置第16位,则final向量( int16向量)现在应该包含二进制补码(负)。 也不确定A2是什么,所以我删除了它。

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
vec_d = [
   63820
   63594
   63382
   63123
   62921
   62712
   62536
   62350
   62129
   61914
   61668
];
vec_d = uint16(vec_d);

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% extract 16th bit:
vec_a = bitget(vec_d, 16);

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% preallocate other datastructures:
vec_C1 = zeros(size(vec_a), 'uint16');
vec_A1 = zeros(size(vec_a), 'uint16');
vec_final = zeros(size(vec_a), 'int16');

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for num_k = 1:numel(vec_a)
    if (vec_a(num_k) == 1)  
        vec_C1(num_k) = bitcmp(uint16(vec_d(num_k)));
        vec_A1(num_k) = vec_C1(num_k) + 1;
        vec_final(num_k) = (-1) * int16(vec_A1(num_k));
    else  
        vec_final(num_k) = vec_d(num_k);
    end
end
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

希望能帮助到你。


The problem comes from the fact that you cannot mix signed and unsigned arithemetics with integers. Not directly anyway. You have to tell specifically what you want. The line you mention gives zero because you multiply a uint16 representation with (-1) which is by default recast from double to uint16, which truncates to zero (0). Use int16 instead.

I have made a more clean version of your code using some better variables names (though some might disagree). I find it useful to use the mathematically intended "type" as a prefix on variables names (matrix, vector, etc.). You should also get in the habit of preallocating your data, even if the datastructures are quite small. I bet the MATLAB editor is likely screaming at you with annoying warnings in the right margin? :)

The final vector (an int16 vector) should now contain the two's complement (negative) values if the 16th bit is set in the values of vec_d. Also not sure what A2 was for, so I removed it.

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
vec_d = [
   63820
   63594
   63382
   63123
   62921
   62712
   62536
   62350
   62129
   61914
   61668
];
vec_d = uint16(vec_d);

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% extract 16th bit:
vec_a = bitget(vec_d, 16);

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% preallocate other datastructures:
vec_C1 = zeros(size(vec_a), 'uint16');
vec_A1 = zeros(size(vec_a), 'uint16');
vec_final = zeros(size(vec_a), 'int16');

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for num_k = 1:numel(vec_a)
    if (vec_a(num_k) == 1)  
        vec_C1(num_k) = bitcmp(uint16(vec_d(num_k)));
        vec_A1(num_k) = vec_C1(num_k) + 1;
        vec_final(num_k) = (-1) * int16(vec_A1(num_k));
    else  
        vec_final(num_k) = vec_d(num_k);
    end
end
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Hope it helps.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取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的基本操作命令。。。