⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpclient.java

📁 SSD8练习3 自己做的 拿出来交流交流
💻 JAVA
字号:
/**
 * This class models a client using TCP to communite with the server.
 * 
 * @author tyrant
 * @version 1.0.0
 */

import java.net.*;
import java.io.*;

public class TCPClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			
			long totalTime = 0;
			long startTime = 0;
			long endTime = 0;
			int counterOfPacket = 1;
			
			/* byte arrays to store one byte to receive and to send. */
			byte[] sendByte = new byte[1];
			byte[] receiveByte = new byte[1];
			
			sendByte[0] = 'a';

			// get the start time before the while circle.
			startTime = System.currentTimeMillis();

			while (counterOfPacket <= 1000) {
				
				// connect to the server.
				Socket clientSocket = new Socket("localhost", 6000);
				
				// iostreams to communicate with the server.
				DataOutputStream outToServer = new DataOutputStream(
						clientSocket.getOutputStream());				
				DataInputStream inFromServer = new DataInputStream(
						clientSocket.getInputStream());
				
				// exchange one byte with the server.
				outToServer.write(sendByte, 0, sendByte.length);								
				inFromServer.read(receiveByte, 0, receiveByte.length);
				
				counterOfPacket++;
				
				clientSocket.close();
			}

			// get the end time after the while circle.
			endTime = System.currentTimeMillis();
			
			totalTime = endTime - startTime;
						
			System.out.println("Total time: " + totalTime + "ms");
			System.out.println("RTT: " + totalTime / 1000.0 + "ms");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -