用TCP socket 发送数据
package com.hsled.net;import java.io.DataOutputStream;
import java.net.UnknownHostException;
import java.net.Socket;
import java.io.IOException;
import java.net.*;/**
* 发送
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class MySocket { Socket socket = null;
DataOutputStream out = null; public MySocket() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
} } public boolean sendMethod2(String ip, int port, byte[] sendData_byte) {
boolean isSuccess = true;
try {
socket = new Socket(ip, port);
}
catch (UnknownHostException ex1) {
System.out.println("UnknownHostException ex1 : " + ex1.getMessage());
ex1.getStackTrace();
isSuccess = false;
return isSuccess;
}
catch (IOException ex2) {
System.out.println("IOException ex2 : " + ex2.getMessage());
ex2.getStackTrace();
isSuccess = false;
return isSuccess;
} //end try catch try {
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException ex3) {
System.out.println("IOException ex3" + ex3.getMessage());
ex3.getStackTrace();
isSuccess = false;
} //end try catch
System.out.println("sendData_byte:" + sendData_byte.length); try { out.write(sendData_byte); //输出数据 }
catch (IOException ex4) {
System.out.println("IOException ex4" + ex4.getMessage());
ex4.getStackTrace();
isSuccess = false;
} //end try catch
try {
out.flush();//刷新缓冲区
}
catch (IOException ex8) {
System.out.println("IOException ex4" + ex8.getMessage());
ex8.getStackTrace();
isSuccess = false; }
System.out.println("size++++++++++" + out.size());
try {
Thread.sleep(10000); //延时
}
catch (InterruptedException ex7) {
}
try {
socket.setSoTimeout(200); //设置接受返回信息的超时时间
}
catch (SocketException ex) {
System.out.println("IOException ex4" + ex.getMessage());
ex.getStackTrace();
isSuccess = false;
}
System.out.println("size++++++++++" + out.size());
try { out.close();
socket.close();
}
catch (IOException ex6) {
System.out.println("IOException ex4" + ex6.getMessage());
ex6.getStackTrace();
isSuccess = false;
}
try {
Thread.sleep(50);
}
catch (InterruptedException ex5) {
System.out.println(this.getClass().getName() + "出错了!");
ex5.printStackTrace();
isSuccess = false;
} //end try catch
return isSuccess;
} //end sendMethod private void jbInit() throws Exception {
}
}
//----------------------------------------------------------//在输出完毕后需要有个延时时间 但是 这个时间在数据量大的时候需要延长
//数据量小的时候没有必要的该那么长时间 怎么实时监控数据流的输出情况在其
//可以准确在out.write(byte[])输出完毕以后, 调用out.close()
//设置一个需要经常变化的延时时间让线程等待?
package com.hsled.net;import java.io.DataOutputStream;
import java.net.UnknownHostException;
import java.net.Socket;
import java.io.IOException;
import java.net.*;/**
* 发送
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class MySocket { Socket socket = null;
DataOutputStream out = null; public MySocket() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
} } public boolean sendMethod2(String ip, int port, byte[] sendData_byte) {
boolean isSuccess = true;
try {
socket = new Socket(ip, port);
}
catch (UnknownHostException ex1) {
System.out.println("UnknownHostException ex1 : " + ex1.getMessage());
ex1.getStackTrace();
isSuccess = false;
return isSuccess;
}
catch (IOException ex2) {
System.out.println("IOException ex2 : " + ex2.getMessage());
ex2.getStackTrace();
isSuccess = false;
return isSuccess;
} //end try catch try {
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException ex3) {
System.out.println("IOException ex3" + ex3.getMessage());
ex3.getStackTrace();
isSuccess = false;
} //end try catch
System.out.println("sendData_byte:" + sendData_byte.length); try { out.write(sendData_byte); //输出数据 }
catch (IOException ex4) {
System.out.println("IOException ex4" + ex4.getMessage());
ex4.getStackTrace();
isSuccess = false;
} //end try catch
try {
out.flush();//刷新缓冲区
}
catch (IOException ex8) {
System.out.println("IOException ex4" + ex8.getMessage());
ex8.getStackTrace();
isSuccess = false; }
System.out.println("size++++++++++" + out.size());
try {
Thread.sleep(10000); //延时
}
catch (InterruptedException ex7) {
}
try {
socket.setSoTimeout(200); //设置接受返回信息的超时时间
}
catch (SocketException ex) {
System.out.println("IOException ex4" + ex.getMessage());
ex.getStackTrace();
isSuccess = false;
}
System.out.println("size++++++++++" + out.size());
try { out.close();
socket.close();
}
catch (IOException ex6) {
System.out.println("IOException ex4" + ex6.getMessage());
ex6.getStackTrace();
isSuccess = false;
}
try {
Thread.sleep(50);
}
catch (InterruptedException ex5) {
System.out.println(this.getClass().getName() + "出错了!");
ex5.printStackTrace();
isSuccess = false;
} //end try catch
return isSuccess;
} //end sendMethod private void jbInit() throws Exception {
}
}
//----------------------------------------------------------//在输出完毕后需要有个延时时间 但是 这个时间在数据量大的时候需要延长
//数据量小的时候没有必要的该那么长时间 怎么实时监控数据流的输出情况在其
//可以准确在out.write(byte[])输出完毕以后, 调用out.close()
//设置一个需要经常变化的延时时间让线程等待?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货