TCP通信的客户端代码实现

两端通信时步骤:
1.服务端程序,需要事先启动,等待客户端的连接。
⒉.客户端主动连接服务器端,连接成功才能通信。服务端不可以主动连接客户端。在Java中,提供了两个类用于实现TCP通信程序:
1.客户端︰`java.net.Socket类表示。创建 Socket对象,向服务端发出连接请求,服务端响应请求,两者建立连接开始通信。
⒉.服务端︰java.netLServerSocket类表示。创建 ServerSocket对象,相当于开启一个服务,并等待客户端的连接。

TCP通信的客户端:向服务器发送连接请求,给服务器发送数据,读取服务器回写的数据表示客户端的类:
java.net.Socket:此类实现客户端套接字(也可以就叫"套接字”)。套接字是两台机器间通信的端点。套接字:包含了IP地址和端口号的网络单位
构造方法:
Socket(String host, int port)创建一个流套接字并将其连接到指定主机上的指定端口号。参数:
String host:服务器主机的名称/服务器的IP地址int port:服务器的端口号
成员方法:
outputStream getoutputstream())返回此套接字的输出流。InputStream getInputstream()返回此套接字的输入流。void close()关闭此套接字。

package A_Lian_one.Demo21;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket; public class TCPClient {
public static void main(String[] args) throws IOException {
//1.创建一个客户端对象Socket,构造方法绑定服务器的IP地址和端口号
Socket socket = new Socket("127.0.0.1",8888);
//2.使用Socket对象中的方法getOutputStream()获取网络字节输出流OutputStream对象
OutputStream os = socket.getOutputStream();
//3.使用网络字节输出流OutputStream对象中的方法write,给服务器发送数据
os.write("你好服务器".getBytes()); //4.使用Socket对象中的方法getInputStream()获取网络字节输入流InputStream对象
InputStream is = socket.getInputStream(); //5.使用网络字节输入流InputStream对象中的方法read,读取服务器回写的数据
byte[] bytes = new byte[1024];
int len = is.read(bytes);
System.out.println(new String(bytes,0,len)); //6.释放资源(Socket)
socket.close(); } }

TCP通信的服务器端代码实现

服务器的实现步骤:
1.创建服务器serversocket对象和系统要指定的端口号
2.使用ServerSocket对象中的方法accept,获取到请求的客户端对象Socket
3.使用socket对象中的方法getInputStream()获取网络字节输入流Inputstream对象4.使用网络字节输入流Inputstream对象中的方法read,读取客户端发送的数据
5.使用socket对象中的方法getoutputstream ()获取网络字节输出流outputstream对象6.使用网络字节输出流outputstream对象中的方法write,给客户端回写数据
7.释放资源(Socket, Serversocket)

package A_Lian_one.Demo21;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket; public class TCPServer {
public static void main(String[] args) throws IOException {
//1.创建服务器ServerSocket对象和系统要指定的端口号
ServerSocket server = new ServerSocket(8888);
//2.使用ServerSocket对象中的方法accept,获取到请求的客户端对象Socket
Socket socket = server.accept();
//3.使用Socket对象中的方法getInputStream()获取网络字节输入流InputStream对象
InputStream is = socket.getInputStream();
//4.使用网络字节输入流InputStream对象中的方法read,读取客户端发送的数据
byte[] bytes = new byte[1024];
int len = is.read(bytes);
System.out.println(new String(bytes,0,len));
//5.使用Socket对象中的方法getOutputStream()获取网络字节输出流OutputStream对象
OutputStream os = socket.getOutputStream();
//6.使用网络字节输出流OutputStream对象中的方法write,给客户端回写数据
os.write("收到谢谢".getBytes());
//7.释放资源(Socket,ServerSocket)
socket.close();
server.close();
}
}

最新文章

  1. 实例:对2个Makefile的备注
  2. [WPF系列]基础Combox
  3. codevs 2021 中庸之道
  4. eclipse连接外部tomcat进行debug
  5. centos下安装nginx
  6. iOS圆形图片裁剪,以及原型图片外面加一个圆环
  7. 层叠上下文(The stacking context)
  8. MVP+RXJAVA+RecyclerView实现sd卡根目录下的所有文件中的照片加载并显示
  9. 一些需要注意的C知识点
  10. 对于观察者模式,Reactor模式,Proactor模式的一点理解
  11. 如何在eclipse使用StaggeredGridView
  12. notification.setLatestEventInfo(context, title, message, pendingIntent); undefined
  13. <正见>摘抄
  14. convertView
  15. python中的异常
  16. linux中sogou输入法崩溃重启
  17. 1.8 Double-Opening and Virtual Machine
  18. String hashcode的兴趣试玩
  19. MacBook Home End
  20. 给统计人讲python(1)模拟城市_数据分析

热门文章

  1. 对于python中“FileNotFoundError: [Errno 2] No such file or directory”的解决办法
  2. 本地文件上传Gitee
  3. mindxdl--common--validators.go
  4. Web浏览器Linux Shell(shellinabox解决通用区服务器Linux Shell访问很麻烦的问题)
  5. Go语言核心36讲10
  6. dd格式化硬盘
  7. Atcoder补题计划
  8. layui table表格使用table.resize()方法 重置表格尺寸
  9. 【Java SE】课程目录
  10. Linux通过脚本实现多台主机统一部署