带有ssl连接的Java线程

时间:2014-09-30 08:46:48

标签: java

我有一个代码,这是一个网络代码,我想用这些代码连接这个网页代码。但是当我开始使用我的代码时,Eclipse给了我这条消息:

线程中的异常"线程-17" java.lang.OutOfMemoryError:Java堆空间

问题可能是我的源代码,就是这样:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class SimulatorWorker {
    public static void main(String[] args) {
        Thread th = new Thread();
        th.start();

        try {
            for (int i = 1; i <= 10; i++) {
                System.out.println("This is the " + i + " connection:");
                sslConnection();

                th.sleep(2000);
            }
        } catch (InterruptedException e) {
            System.out.println("Thread interrupted!");
            e.printStackTrace();
        } catch (Exception e) {
            // TODO catch block
            e.printStackTrace();
        }
    }

    public static void sslConnection() throws Exception {

        Socket socket = null;
        try {

            socket = new Socket("127.0.0.1", 9003);

            PrintWriter outWriter = new PrintWriter(socket.getOutputStream(),
                    true);

            outWriter.println("Hello Server!");

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (socket != null)
                    socket.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }
}

买路,对不起我的语言技巧! :/

0 个答案:

没有答案