史上最简单的socket
2015-09-14 12:45
369 查看
1. client
2. Server
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)); } }
相关文章推荐
- 利用Instruments中的Leaks处理内存泄露
- 百度sitemap权限要全网开放了
- Android Studio的奇技淫巧
- protobuf简介
- qt5.2+ opencv2.4.10 + cmake 编译配置过程
- 使用commonfileUpload上传文件遇到后台中文乱码
- android动态加载jar包
- 这些年凋零的十大互联网公司
- 使用openURL实现程序间带参数跳转详解
- iOS UTC秒数时间戳和日期的互相转换[转载]
- hdu5422 Favorite Donut
- 基于Qt Phonon模块实现音乐播放器
- Oracle 11g R2 for Win7旗舰版(64位)的安装步骤
- 黑马程序员--#import和@class的区别
- mysql如何实现插入数据时如果不存在则插入如果存在则更新的操作
- JS高级程序设计-笔记
- Apache 和tomcat 的环境搭建 (修正版)
- C++ 拼接字符串的两种方法
- 四:springmvc RESTRUL CRUD
- 嵌入式linux基本开发环境搭建