一个简单的socket客户端和服务端的例子

本文介绍了Socket在网络编程中的基本概念和作用,它位于应用层和传输层之间,提供TCP/IP通信接口。文章详细讲解了socket的创建、绑定、监听、接受和连接等关键函数,并给出了一个简单的服务端和客户端示例,帮助读者理解和实践Socket编程。
摘要由CSDN通过智能技术生成

网络编程中最基础的就是socket的操作,这里记录一下socket的基础操作有哪些,分别是什么作用,最后以一个简单的客户端和服务端例子收尾。


socket是什么?

socket起源于Unix,秉承着一切皆文件的思想,socket也是   打开 -读写- 关闭 这样的模式的一个实现。socket用于不同主机间进程的通信,而每个进程由 所使用的协议,Ip,端口号,三者决定,有兴趣的可以百度一下 多路分解和多路复用。


让我们看一下socket在TCP/IP中所处的位置


可以看到,socket位于应用层与传输层之间,作为一层抽象层把tcp/IP的复杂操作抽象成了一些简单的接口供应用层来调用。


socket的基本操作

主要介绍如下几个函数

int socket(int domain , int type, int protocol)

该函数创建一个socket套接字,返回一个socket描述符,相当于对该套接字的引用,在接下来的函数中将会用到它。

我们来看看各个参数,这里只介绍一些楼主使用过的

domain : 协议族,常用的就是AF_INET,AF_INET6,AF_LOCAL等几种(还有不少),它最关键的作用是指定了地址格式,比如说AF_INET(事实上楼主至今也只用了它..),它决定了该socket套接字的地址必须使用 32位的ipv4地址和16位的port号

type :类型&#x

好的,以下是一个简单的Java Socket客户端服务端例子服务端代码: ```java import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(8080); // 监听8080端口 } catch (IOException e) { System.err.println("Could not listen on port: 8080."); System.exit(1); } Socket clientSocket = null; try { System.out.println("Waiting for connection..."); clientSocket = serverSocket.accept(); // 等待客户端连接 System.out.println("Connected to client: " + clientSocket.getInetAddress().getHostName()); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Received from client: " + inputLine); out.println("Server received: " + inputLine); // 发送回复给客户端 } out.close(); in.close(); clientSocket.close(); serverSocket.close(); } } ``` 客户端代码: ```java import java.io.*; import java.net.*; public class Client { public static void main(String[] args) throws IOException { Socket socket = null; PrintWriter out = null; BufferedReader in = null; try { socket = new Socket("localhost", 8080); // 连接到本地8080端口 out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: localhost."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: localhost."); System.exit(1); } BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); // 发送消息给服务器 System.out.println("Received from server: " + in.readLine()); // 接收服务器的回复 } out.close(); in.close(); stdIn.close(); socket.close(); } } ``` 这个例子中,服务端监听8080端口,等待客户端连接。一旦客户端连接上来,服务端会接收客户端发送的消息,并将收到的消息回复给客户端客户端连接到本地的8080端口,向服务端发送消息,并接收服务端的回复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值