Java网络编程实例---------------------------------TCP和UDP发送数据统计
TCPClient 用来发送TCP数据,服务器返回发送的数据
package org.bq.tcp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.net.Socket; /** *@author 白强 *@version 1.0 */ public class TCPClient { public TCPClient(String serverName) throws Exception { byte seq=0; byte[] buffer = new byte[1]; Socket socket = new Socket(); socket.setSoTimeout(1000); long begin=0, end=0, time=0; int count=0,times=0; //发送1000次 while(count < 1000) { seq++; buffer[0] = seq; System.out.println("seq is :"+seq); socket = new Socket(serverName,8888); BufferedInputStream istream = new BufferedInputStream(socket.getInputStream()); BufferedOutputStream ostream = new BufferedOutputStream(socket.getOutputStream()); //记录发送前的时间 begin=System.currentTimeMillis(); ostream.write(buffer, 0, buffer.length); ostream.flush(); int num_bytes = istream.read(buffer); System.out.println(num_bytes); socket.close(); //记录发送完你的时间 end=System.currentTimeMillis(); //累计时间 time+=(end-begin)*1.0; //累计发送次数 times++; //发送成功,下次循环 if (buffer[0] == seq) {++count;} } System.out.println("发送次数: "+times); System.out.println("平均时间: "+time*1.0/1000); } public static void main(String[] args) throws Exception { new TCPClient("localhost"); } }
package org.bq.tcp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.net.ServerSocket; import java.net.Socket; /** * @author 白强 * @version 1.0 * */ public class TCPServer { public static void main(String args[])throws Exception{ System.out.println("TCP Server starting ... "); byte[] buffer = new byte[1]; Socket socket=new Socket(); ServerSocket serverSocket = new ServerSocket(8888); //无限循环 while(true) { try{ //接受请求 socket = serverSocket.accept(); //输入输出流 BufferedInputStream istream = new BufferedInputStream(socket.getInputStream()); BufferedOutputStream ostream = new BufferedOutputStream(socket.getOutputStream()); //读取数据 int num_bytes = istream.read(buffer); System.out.println(num_bytes); //发送数据 ostream.write(buffer, 0, num_bytes); ostream.flush(); socket.close(); }catch(Exception e){ System.out.println(e.getMessage()); } } } }
UDPClient发送UDP数据,服务器返回发送的数据
package org.bq.udp; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketTimeoutException; /** *@author 白强 *@version 1.0 */ public class DatagramClient { public DatagramClient(String serverName) throws IOException { byte[] sendbuffer = new byte[1]; byte[] recvbuffer = new byte[1]; long begin = 0, end = 0, time = 0; int times = 0, dup = 0; DatagramSocket clientSocket = new DatagramSocket(8001); InetAddress IPAddress = InetAddress.getByName(serverName); System.out.println(IPAddress); clientSocket.setSoTimeout(20); for (int i = 0; i < 128; i++) { try { //记录起始时间 begin = System.currentTimeMillis(); sendbuffer[0] = (byte) i; //发送数据 DatagramPacket sendPacket = new DatagramPacket(sendbuffer, sendbuffer.length, IPAddress, 8000); clientSocket.send(sendPacket); //接收数据 DatagramPacket recvPacket = new DatagramPacket(recvbuffer, recvbuffer.length); clientSocket.receive(recvPacket); //记录结束时间 end = System.currentTimeMillis(); //累计时间 time += (end - begin) * 1.0; //返回的数据包 byte modified[] = recvPacket.getData(); times++; //发送的与接受的不同则重发 if (sendbuffer[0] != modified[0]) { i -= 1; dup += 1; } } catch (SocketTimeoutException timeoutExcep) { //发送超时 System.out.println(timeoutExcep.getMessage()); i -= 1; } } System.out.println("发送次数: " + times); System.out.println("平均时间: " + time * 1.0 / 1000); System.out.println("包重复: " + dup); } public static void main(String args[]) throws Exception { new DatagramClient("localhost"); } }
package org.bq.udp; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * @author 白强 * @version 1.0 */ public class DatagramServer { public DatagramServer() throws Exception { System.out.println("UDP Server starting ..."); DatagramSocket serverSocket = new DatagramSocket(8000); // 定义接收包和发送包,两者必须相同以比较 byte[] receiveData = new byte[1]; byte[] sendData = receiveData; // 服务器无限循环 while (true) { // 接收数据 DatagramPacket recvPacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(recvPacket); String data = new String(recvPacket.getData()); System.out.println("Server geting -------"+data); sendData = data.getBytes(); // 得到发送者的IP地址和端口 InetAddress IPAddress = recvPacket.getAddress(); int port = recvPacket.getPort(); // 将发送的数据直接返回 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } } public static void main(String args[]) throws Exception { new DatagramServer(); } }