java中TCP传输协议


class TcpClient { public static void main(String[] args) throws Exception { //创建client的socket服务,指定目的主机和port 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服务,并监听一个port ServerSocket ss = new ServerSocket( 10003); //通过accept方法获取连接过来的client对象 Socket s = ss.accept(); String ip = s.getInetAddress().getHostAddress(); //获取client发送过来的数据。使用client对象的读取流来读取数据 InputStream in = s.getInputStream(); byte[] buf = new byte[ 1024]; int len = in.read(buf); System. out.println( new String(buf, 0,len)); //关闭client s.close(); } } class TcpClient2 { public static void main(String[] args) throws Exception { //建立socket服务。指定连接的主机和port 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(); } } /*需求:建立一个文本转换服务器 client给服务端发送文本,服务端会将文本转成大写再返还给client*/ 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输出流。并发送给client 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(); } }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM