java中tcp小样例

Posted yutingliuyl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中tcp小样例相关的知识,希望对你有一定的参考价值。

服务端:

ServerSocket service = new ServerSocket(7777);
		Socket socket = service.accept();
		InputStream input = socket.getInputStream();
		OutputStream out = socket.getOutputStream();
		BufferedInputStream bufinput = new BufferedInputStream(input);
		BufferedOutputStream bufout = new BufferedOutputStream(out);
		byte [] buf = new byte[1024];
		int len = 0;
		
		StringBuffer sb = new StringBuffer();
        String message = new String(buf,0,bufinput.read(buf));
        System.out.println(message);
        
        out.write("服务端:\n小客你好啊!".getBytes());

client:

Socket client = new Socket("127.0.0.1",7777);
        OutputStream out = client.getOutputStream();
        InputStream input = client.getInputStream();
        BufferedOutputStream bufout = new BufferedOutputStream(out);
        BufferedInputStream bufinput = new BufferedInputStream(input);
        bufout.write("客户端:\n你好".getBytes());
        bufout.flush();
        byte [] buf = new byte [1024];
        String message = new String(buf,0,bufinput.read(buf));
        System.out.println(message);





以上是关于java中tcp小样例的主要内容,如果未能解决你的问题,请参考以下文章

Maven上手使用小样例

python中的TCP编程学习

简单的ADO.NET连接数据小样例

线性回归 python小样例

D3D 线列 小样例

静态函数调用非静态函数的小样例