日期:2014-05-20 浏览次数:20835 次
package com.ewanshang.zhongyintong.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class HJClient {
public static void main(String[] args) throws IOException {
Socket socket = null;
InputStream is = null;
OutputStream os = null;
String ip = "192.168.2.193";
try {
socket = new Socket(ip, 8888);
// 发送信息
os = socket.getOutputStream();
os.write("测试测试".getBytes());
os.flush();
is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
byte[] b = new byte[1024];
int flag = 0;
while ((flag = is.read(b)) != -1) {//一直不返回-1
System.out.println(new String(b));
}
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
if (null != socket) {
socket.close();
}
}
}