java中TCP传输协议
2015-08-13 13:15
399 查看
class TcpClient { public static void main(String[] args) throws Exception { //创建客户端的socket服务,指定目的主机和端口 Socket s = new Socket("192.168.1.10",10003); //为了发送数据,获取socket流中的输出流 java.io.OutputStream out = s.getOutputStream(); out.write("hello tcp".getBytes()); s.close(); } } class TcpServer { public static void main(String[] args) throws Exception { //建立服务端socket服务,并监听一个端口 ServerSocket ss = new ServerSocket(10003); //通过accept方法获取连接过来的客户端对象 Socket s = ss.accept(); String ip = s.getInetAddress().getHostAddress(); //获取客户端发送过来的数据,使用客户端对象的读取流来读取数据 InputStream in = s.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); System.out.println(new String(buf,0,len)); //关闭客户端 s.close(); } } class TcpClient2 { public static void main(String[] args) throws Exception { //建立socket服务,指定连接的主机和端口 Socket s = new Socket("192.168.1.10",10004); //获取socket流中的输出流。将数据写入该流,通过网络传送给服务端 OutputStream out = (OutputStream) s.getOutputStream(); out.write("hello Tcp".getBytes()); //获取socket流中的输入流,将服务端反馈的数据获取到,并打印 InputStream in = s.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); System.out.println(new String(buf,0,len)); s.close(); } } class TcpServer2 { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10004); Socket s = ss.accept(); //服务端收信息 InputStream in = s.getInputStream(); String ip = s.getInetAddress().getHostAddress(); byte[] buf = new byte[1024]; int len = in.read(buf); System.out.println(new String(buf,0,len)); //服务端发信息 OutputStream out = (OutputStream) s.getOutputStream(); out.write("have receive".getBytes()); s.close(); ss.close(); } } /*需求:建立一个文本转换服务器 客户端给服务端发送文本,服务端会将文本转成大写再返还给客户端*/ class TcpClient3 { public static void main(String[] args) throws Exception { Socket s = new Socket("192.168.1.4",10005); //定义读取键盘数据的流对象,读取键盘录入文本 BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); //获取socket流中的输出流,通过网络传送到服务端 BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); //定义一个socket读取流,读取服务端返回的大写信息 BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = null; while((line=bufr.readLine())!=null) { if("over".equals(line)) break; bufOut.write(line); bufOut.newLine();//结束标记 bufOut.flush(); String str = bufIn.readLine(); System.out.println("server:"+str); } bufr.close(); s.close(); } } class TcpServer3 { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10005); Socket s = ss.accept(); //读取socket读取流中的数据 BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream())); //socket输出流,将大写文本写入到socket输出流,并发送给客户端 BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line = null; while((line=bufIn.readLine())!=null) { bufOut.write(line.toUpperCase()); bufOut.newLine();//结束标记 bufOut.flush(); } s.close(); ss.close(); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统