77import java .io .IOException ;
88import java .io .OutputStream ;
99import java .net .Socket ;
10- import java .util .Arrays ;
1110import java .util .List ;
1211
1312/**
@@ -37,6 +36,7 @@ public class Crawl extends Thread {
3736 * @throws IOException
3837 */
3938 public boolean init () throws IOException {
39+ System .out .println ("获取登录信息" );
4040 String roomId = Utils .getRoomId ();
4141 String time = String .valueOf (System .currentTimeMillis ());
4242
@@ -103,7 +103,7 @@ public boolean init() throws IOException {
103103 */
104104 public void login () throws IOException {
105105 socket = new Socket (serverIp ,port );
106- System .out .println ("连接弹幕服务器 :" + serverIp + ":" + port );
106+ System .out .println ("登录弹幕服务器 :" + serverIp + ":" + port );
107107 ByteArrayOutputStream byteArray = new ByteArrayOutputStream ();
108108 byte [] b = new byte []{0x00 , 0x06 , 0x00 , 0x02 , 0x00 , 0x61 };
109109 byteArray .write (b );
@@ -124,12 +124,16 @@ public void login() throws IOException {
124124
125125 @ Override
126126 public void run () {
127- MessageHandler messageHandler ;
127+ MessageHandler messageHandler = null ;
128128 OutputStream outputStream ;
129129
130130 try {
131- init ();
131+ //初始化失败
132+ if (!init ()) {
133+ return ;
134+ }
132135 login ();
136+ System .out .println ("----------------------------" );
133137
134138 messageHandler = new MessageHandler (socket );
135139 outputStream = socket .getOutputStream ();
@@ -160,6 +164,12 @@ public void run() {
160164 e .printStackTrace ();
161165 } catch (InterruptedException e ) {
162166 e .printStackTrace ();
167+ } finally {
168+ try {
169+ messageHandler .close ();
170+ } catch (IOException e ) {
171+ e .printStackTrace ();
172+ }
163173 }
164174 }
165175}
0 commit comments