public class Atx {
Txlog tlog = new Txlog();
public static int NO_CON_TIMEOUT;
public static int SOTIME;
public void tx(String msg, String ipadress ) throws IOException{
// int no_con_timeout =10000;
int port = 25902;
try {
InetAddress IPAddress =InetAddress.getByName(ipadress);
byte[] byteBuffer =msg.getBytes();
try (Socket client = new Socket()) {
client.connect(new InetSocketAddress(IPAddress, port), getnocon());
client.setSoTimeout(getsotime());
client.setTcpNoDelay(true);
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream();
out.write(byteBuffer); // Send the encoded string to the server
// Receive the same string back from the server
int totalBytesRcvd = 0; // Total bytes received so far
int bytesRcvd; // Bytes received in last read
while (totalBytesRcvd < byteBuffer.length) {
if ((bytesRcvd = in.read(byteBuffer, totalBytesRcvd,byteBuffer.length - totalBytesRcvd)) == -1)
throw new SocketException("Connection closed prematurely");
totalBytesRcvd +=bytesRcvd;}
//System.out.println("Received: " + new String(byteBuffer)+ " From: " + ipadress) ;
if(msg.charAt(0)=='A')
{tlog.tlog(ipadress , new String(byteBuffer)+ " Accepted by other"); }
else
if(msg.charAt(0)=='D')
{tlog.tlog(ipadress , new String(byteBuffer)+ " WIFI OOR delay Cancelled"); }
client.close();
}
}catch(IOException e)
{
// System.out.println(e + " to " + ipadress );
if(msg.charAt(0)=='A')
{tlog.tlog(ipadress , e.getMessage()+ " Accepted by other Not Reached") ;}
else
if(msg.charAt(0)=='D')
{tlog.tlog(ipadress , e.getMessage()+ " WIFI OOR delay Cancelled Not Reached"); }
}
}
public void setnocn(int nocontime) {
Atx.NO_CON_TIMEOUT=nocontime;
}
public void setsotime(int sotime) {
Atx.SOTIME=sotime;
}
public int getnocon() {
return Atx.NO_CON_TIMEOUT;
}
public int getsotime() {
return Atx.SOTIME;
}
}
如果Frist Thread调用tx(String msg,String ipaddress),它仍处理如果第二个线程也调用tx(String msg,String ipaddress)会发生什么。此方法只是在TCP端口上向服务器发送消息,并接收回送并关闭连接。