史上最简单的socket
2015-09-14 12:45
246 查看
原文链接:https://www.geek-share.com/detail/2653205900.html
1. client
import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class TcpClient2 { /** * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Socket s = new Socket("192.168.1.120",9998); OutputStream os = s.getOutputStream(); os.write("i will go".getBytes()); os.close(); s.close(); } }
2. Server
import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class TcpServer2 { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub ServerSocket ss = new ServerSocket(9998); System.out.println(ss.getInetAddress()); Socket s = ss.accept(); InputStream is= s.getInputStream(); byte[] buf = new byte[1024]; int len = is.read(buf); System.out.println(new String(buf,0,len)); } }
转载于:https://www.cnblogs.com/childhooding/p/4806874.html
相关文章推荐
- 简单理解SOCKET
- python 使用socket 实现一发一收的简单聊天
- 简单socket【C/S,服务器收文件】
- nodejs socket实现的服务端和客户端简单通信
- Android Socket(简单的字符串)
- C# Socket简单例子(服务器与客户端通信)
- 简单RPC之Socket实现
- Socket编程之简单介绍
- 一个简单的基于socket的通讯处理程序
- 基于C#的Socket简单通讯
- C#基于Socket的简单聊天室实践
- 使用Beetle简单构建高性能Socket tcp应用
- python socket简单网络编程
- C#通信之Socket通信的简单例子
- Socket 由浅入深系列--------- 简单实现编程(三)
- 简单聊天室socket.io实现
- MFC CSocket简单C/S通信
- 面向连接的Socket Server的简单实现(简明易懂)
- unity3D-----------socket客户端、服务器(简单)
- Socket通信:简单传输到增加多线程进行多数据传输