该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
目前服务端是用C做的。JAVA客户端订阅一个消息后,就等待服务器发送消息。客户端木有问题,现在客户端不知道哦哪错了。连上就断,连上就断。
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);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); } } } }