Socket通信的原理大致分为以下几个步骤:
客户端的步骤如下:
下面是一个简单的TCP Socket通信的Python代码示例:
python
# 服务器端代码
import socket
# 创建一个socket对象
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 绑定IP地址和端口号
server_address = ('localhost', 8000)
server_socket.bind(server_address)
# 开始监听连接请求
server_socket.listen(1)
print('服务器已启动,等待客户端连接...')
# 等待客户端连接
client_socket, client_address = server_socket.accept()
print(f'客户端{client_address}已连接')
# 接收客户端发送的数据
data = client_socket.recv(1024)
print(f'收到客户端发送的数据:{data}')
# 向客户端发送响应数据
response = '你好,客户端!'
client_socket.send(response.encode())
# 关闭连接
client_socket.close()
server_socket.close()
客户端代码:
python
import socket
# 创建一个socket对象
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 连接服务器,指定IP地址和端口号
server_address = ('localhost', 8000)
client_socket.connect(server_address)
# 向服务器发送数据
message = '你好,服务器!'
client_socket.send(message.encode())
# 接收服务器的响应数据
response = client_socket.recv(1024)
print(f'收到服务器的响应:{response}')
# 关闭连接
client_socket.close()
这个示例中,服务器端首先创建一个Socket对象,并绑定到指定的IP地址和端口号上。然后,它开始监听来自客户端的连接请求。当有客户端请求连接时,服务器接受连接请求,并与客户端进行通信。在通信过程中,客户端向服务器发送数据,服务器接收数据并返回响应。最后,双方都关闭连接。
以下是一个简单的C语言Socket通信代码示例:
服务器端代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8000
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
char buffer[1024];
int bytes_read;
// 创建socket对象
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("socket error");
exit(1);
}
// 绑定IP地址和端口号
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind error");
exit(1);
}
// 开始监听连接请求
if (listen(server_socket, 1) == -1) {
perror("listen error");
exit(1);
}
// 等待客户端连接
socklen_t client_addr_len = sizeof(client_addr);
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_socket == -1) {
perror("accept error");
exit(1);
}
// 接收客户端发送的数据并输出到控制台
bytes_read = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_read > 0) {
buffer[bytes_read] = '\0';
printf("收到客户端发送的数据:%s\n", buffer);
} else {
perror("recv error");
exit(1);
}
// 向客户端发送响应数据并关闭连接
const char *response = "你好,客户端!";
send(client_socket, response, strlen(response), 0);
close(client_socket);
close(server_socket);
return 0;
}
客户端代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8000
#define SERVER "localhost"
int main() {
int client_socket;
struct sockaddr_in server_addr;
char buffer[1024];
int bytes_read;
// 创建socket对象
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("socket error");
exit(1);
}
// 连接服务器,指定IP地址和端口号
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER); // 将主机名转换为IP地址
server_addr.sin_port = htons(PORT); // 将端口号转换为网络字节序
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("connect error");
exit(1);
}
// 向服务器发送数据并等待响应
const char *message = "你好,服务器!";
send(client_socket, message, strlen(message), 0);
bytes_read = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_read > 0) {
buffer[bytes_read] = '\0';
printf("收到服务器的响应:%s\n", buffer);
} else {
perror("recv error");
exit(1);
}
// 关闭连接
close(client_socket);
return 0;
}