先看一张图,画的很挫,将就看。
TCP 客户端与服务端通信时,是服务端会拿到客户端的socket进行通信。
TCP就相当于以前的座机,有一个听筒和一个话筒,A用话筒说话,B用听筒听。
下面讲讲java中TCP的使用以及步骤。
TCP客户端步骤:
1.建立Socket服务,并确定IP和Port
2.通过socket服务获取输入流或输出流。
3.通输入或输出流操作数据。
TCP服务端的步骤:
1.用ServerSocket来监听Port
2.用accept获取客户端的Socket
3.通客户端的Socket来获取输入和输出流。
4.通过数据流传输。
用一个简单实例演示:
package com.core.net; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; class TcpClient{ public static void main(String[] args) throws Exception { Socket s = new Socket("localhost", 10004); OutputStream out = s.getOutputStream(); out.write("tcp come ...".getBytes()); s.close(); } } class TcpServer{ public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10004); Socket s = ss.accept(); String ip = s.getInetAddress().getHostAddress(); System.out.println(ip+".....connect"); InputStream in = s.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); System.out.println(new String(buf,0,len)); s.close(); ss.close(); } } public class TcpDemo { public static void main(String[] args) { } }
这个实例只是客服端发送数据,服务器接收数据。
下面是一个例子是客服端发送数据,服务器发送数据。
package com.core.net; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; class Client2{ public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost", 10004); OutputStream out = socket.getOutputStream(); out.write("服务端,你好".getBytes()); InputStream in = socket.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); System.out.println(new String(buf,0,len)); socket.close(); } } class Server2{ public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10004); Socket s = ss.accept(); InputStream in = s.getInputStream(); byte[] buf = new byte[1024]; int len = in.read(buf); System.out.println(new String(buf,0,len)); OutputStream out = s.getOutputStream(); Thread.sleep(10000); out.write("客服端,你好".getBytes()); s.close(); ss.close(); } } public class TcpDemo2 { public static void main(String[] args) { } }
最后一个例子是服务端为大写转换器,客服端输入数据,服务端将输入的数据转换大写。
package com.core.net; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; class TransClient{ public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost", 10005); //键盘输入 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //socket输入流 BufferedReader bufin = new BufferedReader(new InputStreamReader(socket.getInputStream())); //socket输出流 BufferedWriter bufout = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String line = null; while((line = reader.readLine())!=null){ if("over".equals(line)) break; bufout.write(line); bufout.newLine();//要注意 "\r\n" bufout.flush(); String str = bufin.readLine(); System.out.println("server:"+str); } reader.close(); socket.close(); } } class TransServer{ public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10005); Socket s = ss.accept(); String ip = s.getInetAddress().getHostAddress(); System.out.println(ip+"......connection"); BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line = null; while((line = reader.readLine())!=null){ System.out.println(line); out.write(line.toUpperCase()); out.newLine(); out.flush(); } s.close(); ss.close(); } } public class TransDemo { public static void main(String[] args) { } }
时间: 2024-10-10 05:24:12