level 4
岛国射手
楼主
RT。现在我的客户端连接成功服务端后,客户端要一直保持连接,并且在服务端发送消息成功后,客户端要把服务端的消息显示出来。
下面上代码,帮忙看看那里有问题:、
package com.Demos;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class ServerApp {
public static void main(String[] args) {
Socket JavaSocket = null;
DataOutputStream os = null;
DataInputStream is =null;
try {
JavaSocket= new Socket("192.168.0.250",6379);
os = new DataOutputStream(JavaSocket.getOutputStream());
is = new DataInputStream(JavaSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to");
}
if (JavaSocket != null && os != null && is != null){
try {
String jcStr="subscribe All\r\n";
byte[] jcBytes =jcStr.getBytes();
os.write(jcBytes);//使用byte[]发送数据包
os.flush();
byte[] cbuf=new byte[8096];
is.read(cbuf);
while(true){String responseLine=new String(cbuf);
System.out.println("读取到的String是:"+responseLine);
}
//os.close();
// is.close();
// JavaSocket.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) {
System.err.println("IOException: " + e);
} }
}
}
I
2013年09月16日 06点09分
1
下面上代码,帮忙看看那里有问题:、
package com.Demos;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class ServerApp {
public static void main(String[] args) {
Socket JavaSocket = null;
DataOutputStream os = null;
DataInputStream is =null;
try {
JavaSocket= new Socket("192.168.0.250",6379);
os = new DataOutputStream(JavaSocket.getOutputStream());
is = new DataInputStream(JavaSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to");
}
if (JavaSocket != null && os != null && is != null){
try {
String jcStr="subscribe All\r\n";
byte[] jcBytes =jcStr.getBytes();
os.write(jcBytes);//使用byte[]发送数据包
os.flush();
byte[] cbuf=new byte[8096];
is.read(cbuf);
while(true){String responseLine=new String(cbuf);
System.out.println("读取到的String是:"+responseLine);
}
//os.close();
// is.close();
// JavaSocket.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) {
System.err.println("IOException: " + e);
} }
}
}
I