62,612
社区成员
发帖
与我相关
我的任务
分享
private static String sendSynMsg(String ipAddr, byte[] datas) throws Exception{
//解析服务器地址和端口号
int dotPos = ipAddr.indexOf(':');
String ip = ipAddr.substring(0, dotPos).trim();
int port = Integer.parseInt(ipAddr.substring(dotPos+1).trim());
InetSocketAddress endpoint = new InetSocketAddress(ip , port);
System.out.println("ip:"+ip+"port:"+port);
Socket socket = null;
OutputStream out = null;
InputStream in = null;
try {
socket = new Socket();
//设置发送逗留时间2秒
socket.setSoLinger(true, 2);
//设置InputStream上调用 read()阻塞超时时间2秒
socket.setSoTimeout(2000);
//设置socket发包缓冲为32k;
socket.setSendBufferSize(32*1024);
//设置socket底层接收缓冲为32k
socket.setReceiveBufferSize(32*1024);
//关闭Nagle算法.立即发包
socket.setTcpNoDelay(true);
//连接服务器
socket.connect(endpoint);
//获取输出输入流
out = socket.getOutputStream();
in = socket.getInputStream();
//输出请求
out.write(datas);
out.flush();
//接收应答
BufferedReader br = new BufferedReader( new InputStreamReader(in) , 4096);
StringWriter received = new StringWriter(4096);
char[] charBuf = new char[4096];
int size = 0;
char lastChar = 0;
do {
size = br.read(charBuf , 0 , 4096);
lastChar = charBuf[size-1];
if(lastChar == 0){
received.write(charBuf, 0, size - 1);
}
//System.out.println(received.toString());
}while(lastChar != 0);
return received.toString();
} finally {
if (out != null) {
try {
out.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String[] args) {
byte[] charBuf = new byte[4096];
try {
sendSynMsg("132.97.101.22:7001",charBuf);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}