Simple TCP example

 

Server:
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
	public static void main(String[] args) throws Exception {
		ServerSocket serversocket = new ServerSocket(8000);
		Socket socket = null;
		InputStream ips = null;
		OutputStream ops = null;

		byte[] recvbuffer = new byte[1024];
		byte[] sendbuffer = new byte[1024];
		byte[] content = "Hello, This is server!".getBytes();
		System.arraycopy(content, 0, sendbuffer, 0, content.length);
		long index = 0, length = 0;

		for (;;) {
			try {
				socket = serversocket.accept();
				ips = socket.getInputStream();
				ops = socket.getOutputStream();

				System.out.println("Connected From: " + socket.getInetAddress()
						+ ":" + socket.getPort());

				index = 0;
				length = 0;
				long current = System.currentTimeMillis();

				for (;;) {
					// Thread.sleep(1);

					int available = ips.available();
					if (available >= recvbuffer.length) {
						ips.read(recvbuffer);
						length += recvbuffer.length;
						index++;
						current = System.currentTimeMillis();
						System.out.print("\rReceive: " + index + ", Length: "
								+ length);
						if (!new String(recvbuffer).trim().equalsIgnoreCase(
								"Hello, This is client!")) {
							System.err.println("Content Error!");
						}
					} else {
						if (System.currentTimeMillis() - current > 3000) {
							ops.write(sendbuffer);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				System.out.println("Total Length: "
						+ (length + ips.available()));

				if (ops != null) {
					ops.flush();
					ops.close();
				}
				if (ips != null) {
					ips.close();
				}
				if (socket != null) {
					socket.close();
				}
			}
		}
	}
}
Client:
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class TcpClient {
	public static void main(String[] args) throws Exception {
		if (args.length < 3) {
			System.out
					.println("Usage:java TcpClient ServerIP ServerPort SendCount");
			return;
		}
		Socket socket = new Socket(InetAddress.getByName(args[0]),
				Integer.parseInt(args[1]));
		InputStream ips = socket.getInputStream();
		OutputStream ops = socket.getOutputStream();

		byte[] recvbuffer = new byte[1024];
		byte[] sendbuffer = new byte[1024];
		byte[] content = "Hello, This is client!".getBytes();
		System.arraycopy(content, 0, sendbuffer, 0, content.length);

		long index = 0, length = 0;
		System.out.println("Send start...");

		while (true) {
//			Thread.sleep(1);

			ops.write(sendbuffer);

			index++;
			length += sendbuffer.length;
			System.out.print("\rSend: " + index + ", "
					+ (index * 100 / Integer.parseInt(args[2])) + "%"
					+ ", Length: " + length);

			if (ips.available() >= recvbuffer.length) {
				ips.read(recvbuffer, 0, recvbuffer.length);

				if (!new String(recvbuffer).trim().equalsIgnoreCase(
						"Hello, This is server!")) {
					System.err.println("Content Error!");
				}
			}

			if (index >= Integer.parseInt(args[2])) {
				break;
			}
		}

		ops.flush();
		ops.close();
		ips.close();
		socket.close();
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值