public static void main(String a[]) throws Exception { boolean bool = false; try { InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); do { Socket clientSocket = new Socket("localhost", 6789); TCPClient client = new TCPClient(clientSocket); client.talkOnSocket(); System.out.println("Do you want tranfer another file : Y/N"); String choise = br.readLine().trim(); if (choise.equalsIgnoreCase("y")) { bool = true; } else if (choise.equalsIgnoreCase("n")) { bool = false; } else { bool = false; System.out.println("Invalid entry. Clsoing."); } } while (bool); in.close(); br.close(); } catch (Exception e) { System.out.println("Server might not be up and running...."); System.exit(0); } }
// -------------------------------------- public void setup() { // Make a new Client with an XML file. client = new TCPClient(this, "mpe" + ID + ".xml"); // The size is determined by the client's local width and height size(client.getLWidth(), client.getLHeight(), P3D); client.enable3D(true); resetEvent(client); // IMPORTANT, YOU MUST START THE CLIENT! client.start(); }
// -------------------------------------- public void frameEvent(TCPClient c) { // clear the screen background(255); for (int x = 0; x < client.getMWidth(); x += 100) { pushMatrix(); translate(x, client.getMHeight() / 2); stroke(0); noFill(); rotate(angle); box(75); popMatrix(); } angle += 0.02f; }
public void closeConnection() { stopStreaming(); if (tcpClient != null) { tcpClient.closeConnection(); tcpClient = null; } }
private void sendMessageUsingTcp(String messageToSend) { if (tcpClient == null) { // we create a TCPClient object tcpClient = TCPClient.getTCPClientAndConnect( tcpIpAddr, tcpIpPort, new TCPClient.OnMessageReceived() { @Override // here the messageReceived method is implemented public void messageReceived(String message) { dealWithMessage( message); // TODO: make sure that runs on original thread. (avoid multithread // unsafe access). // publishProgress(message);//this method calls the onProgressUpdate } }); } // new connectTask().execute(messageToSend); tcpClient.sendMessage(messageToSend); }
@Override public void run() { CLog.i(tag, "SocketOutputThread begin work"); while (isStart) { CLog.i(tag, "sendMsgList.size()=" + sendMsgList.size()); // 锁发送list synchronized (sendMsgList) { // 发送消息 for (MsgEntity msg : sendMsgList) { String message = new String(msg.getBytes()); Date currTime = new Date(); CLog.i(tag, "####"); CLog.i(tag, "currTime=" + df.format(currTime)); CLog.i(tag, "msg=" + message); CLog.i(tag, "msg.getSendTime()=" + df.format(msg.getSendTime())); if (msg.getTryTimes() > 0 && msg.isComplete()) { sendMsgList.remove(msg); continue; } if (msg.getSendTime().getTime() <= currTime.getTime()) { msg.trySend(); TCPClient.instance(mLogin) .sendMsg(Const.str2EncryptedByte(msg.toString(), "NAN", mLogin.getName())); msg.setSendDelay(Const.RETRY_SEND_SECOND); // Try send 3 times if (msg.getTryTimes() >= Const.RETRY_SEND_TIMES) { Const.broadCastToActivity( "WARN", null, "Send order Fail:" + message, Const.whichActivity.MAIN); msg.setComplete(); } if (msg.isComplete()) sendMsgList.remove(msg); } } } synchronized (this) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); Const.broadCastToActivity( "ERROR", null, "TCPOutputThread.wait():InterruptedException", Const.whichActivity.MAIN); } // After sending message Thread in waiting } } CLog.i(tag, "SocketOutputThread is over!"); }
public void testTCPClient() { String result; String comando; TCPClient tcpclient = new TCPClient(); try { /** * Este teste pega uma palavra válida de comando, faz o cálculo do checksum e envia para o CLP * acionando a classe TCPClient. * * <p>Estes comandos... comando = ccsum.calcCheckSum("00SA00000C101010101010",12874); comando * = ccsum.calcCheckSum("00SA00000C010101010101",12874); comando = * ccsum.calcCheckSum("00RA00000C",12874); comando = ccsum.calcCheckSum("00RC",12874); * * <p>Após o cálculo do checksum (ccsum), assumem esses valores: * tcpclient.sendCLP("/00SA00000C1010101010106D"); * tcpclient.sendCLP("/00SA00000C0101010101016D"); tcpclient.sendCLP("/00RA00000C26"); * tcpclient.sendCLP("/00RCF5"); */ CalcCheckSum ccsum = new CalcCheckSum(); comando = ccsum.calcCheckSum("00SA00000C101010101010", 12874); result = tcpclient.sendCLP(comando); System.out.println("Resultado retornado pela classe TCPClient: " + result); checkSend = StringUtils.substring(comando, 8, 12); checkReturn = StringUtils.substring(result, 2, 6); assertEquals(checkReturn, checkSend); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); assertEquals(true, false); } }
/** @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Logging.start(Logging.LEVEL_DEBUG); TimeSync ts = TimeSync.initializeLocal(50); try { ts.waitForStartup(); TCPClient com = new TCPClient( 3334, null, new NIOServer() { public void onAccept(NIOConnection connection) { onConnect(connection); } public void onConnect(NIOConnection connection) { System.out.println("connected to " + connection.getEndpoint()); connection.read(BufferPool.allocate(1024)); connection.setContext(new AtomicBoolean(false)); } public void onRead(NIOConnection connection, ReusableBuffer buffer) { System.out.println("read from " + connection); buffer.flip(); byte[] data = new byte[buffer.remaining()]; buffer.get(data); String contents = new String(data); BufferPool.free(buffer); connection.read(BufferPool.allocate(1024)); System.out.println(">> " + contents); } public void onClose(NIOConnection connection) { System.out.println("connection from " + connection.getEndpoint() + " closed "); } public void onWriteFailed(IOException exception, Object context) { System.out.println("could not write, context: " + context); } public void onConnectFailed( InetSocketAddress endpoint, IOException exception, Object context) { System.out.println("could not connect to: " + endpoint + ", context: " + context); } }); com.start(); com.waitForStartup(); ReusableBuffer data = ReusableBuffer.wrap("Hello world!\n".getBytes()); com.write(new InetSocketAddress("localhost", 3333), data, "Yagg"); Thread.sleep(100); data = ReusableBuffer.wrap("Hello world!\n".getBytes()); com.write(new InetSocketAddress("localhost", 3333), data, "Yagga"); Thread.sleep(30000); data = ReusableBuffer.wrap("YaggaYagga!\n".getBytes()); com.write(new InetSocketAddress("localhost", 3333), data, null); Thread.sleep(2000); com.shutdown(); ts.close(); } catch (Exception ex) { ex.printStackTrace(); System.exit(1); } }