62,614
社区成员
发帖
与我相关
我的任务
分享
public class Treadtest1 {
static BufferedReader br;
static Socket socket;
static InputStream is;
static OutputStream os;
/**服务器IP*/
final static String HOST = "XXX.xx.xxx.xxx";
/**服务器端端口*/
final static int PORT = xxxx;
public static void main(String[] args) throws IOException
{
init(); //初始化
send ("Connect".getBytes());
byte[] data1 = receive();
String s1= new String (data1, "GB2312");//byte转为String
System.out.println(s1);
send ("01234567".getBytes()); //数字字符要逐位发送!!!
byte[] data2 = receive();
String s2= new String (data2, "GB2312");//byte转为String
System.out.println(s2+" success");
//问题出在下面,我想按照之前接收os中的方式来接收,结果无法接收后续数据了
byte[] data3 = receive();
System.out.println(data3);
}
/**
* 初始化
*/
private static void init (){
try {
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
socket = new Socket(HOST,PORT);
is = socket.getInputStream();
os = socket.getOutputStream();
//DataInputStream dis = new DataInputStream(is);//DataInputStream用法??
} catch (Exception e) {}
}
/**
* 向服务器端发送数据
* @param data 数据内容
*/
private static void send(byte[] data){
try{
os.write(data);
}catch(Exception e){}
}
/**
* 接收服务器端反馈
* @return 反馈数据
*将流中的数据转化成字节型,可参考
*/
private static byte[] receive(){
byte[] b = new byte[1024];
try {
int n = is.read(b);
byte[] data = new byte[n];
//复制有效数据
System.arraycopy(b, 0, data, 0, n);
return data;
} catch (Exception e){}
return null;
}
}
跟服务端开发人员一起去连调,先得确定是你这边的问题,然后再具体来查找。
那就一直循环读呗。