socket编程示例 C++
时间: 2023-09-07 13:04:57 浏览: 879
### 回答1:
以下是一个简单的socket编程示例代码,使用C语言实现客户端与服务器之间的通信:
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sock;
struct sockaddr_in server;
char message[1000], server_reply[2000];
// 创建socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
printf("Could not create socket");
}
server.sin_addr.s_addr = inet_addr("服务器IP地址");
server.sin_family = AF_INET;
server.sin_port = htons(端口号);
// 连接到服务器
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}
printf("Connected\n");
// 发送数据
printf("Enter message: ");
scanf("%s", message);
if (send(sock, message, strlen(message), 0) < 0)
{
printf("Send failed");
return 1;
}
// 接收服务器响应
if (recv(sock, server_reply, 2000, 0) < 0)
{
printf("recv failed");
return 1;
}
printf("Server reply: %s\n", server_reply);
close(sock);
return 0;
}
```
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int socket_desc, client_sock, c, read_size;
struct sockaddr_in server, client;
char client_message[2000];
// 创建socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
// 准备socket结构体
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(端口号);
// 绑定socket
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{
perror("bind failed. Error");
return 1;
}
printf("bind done\n");
// 监听socket
listen(socket_desc, 3);
// 等待客户端连接
printf("Waiting for incoming connections...\n");
c = sizeof(struct sockaddr_in);
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
printf("Connection accepted\n");
// 接收客户端数据
if ((read_size = recv(client_sock, client_message, 2000, 0)) > 0)
{
printf("Client message: %s\n", client_message);
}
else if (read_size == 0)
{
printf("Client disconnected");
fflush(stdout);
}
else
{
perror("recv failed");
}
// 发送响应给客户端
if (send(client_sock, "Hello client", strlen("Hello client"), 0) <
### 回答2:
以下是一个简单的socket编程示例,使用C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
char *hello = "Hello from server";
// 创建socket文件描述符
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置socket选项
int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// 绑定地址和端口到socket
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听连接
if (listen(server_fd, 3) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
// 接受客户端连接
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
// 读取客户端请求
valread = read(new_socket, buffer, BUFFER_SIZE);
printf("%s\n", buffer);
// 向客户端发送响应
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
return 0;
}
```
此示例实现了一个简单的服务器端,侦听8080端口上的连接。它首先通过`socket()`函数创建一个socket文件描述符,然后使用`bind()`函数将其绑定到指定的端口。之后,通过`listen()`函数开始监听连接请求。
一旦有客户端连接,使用`accept()`函数接受连接,返回一个新的socket文件描述符来与客户端通信。服务器通过`read()`函数读取来自客户端的请求,并通过`send()`函数向客户端发送响应。
其中,`setsockopt()`函数用于设置socket选项,`perror()`函数用于打印错误信息。这只是一个非常简单的示例,只是展示了socket编程的基本用法,实际应用中可能需要更多的错误处理和逻辑来处理不同情况。
### 回答3:
以下是一个简单的socket编程示例,使用C语言实现了一个简单的客户端和服务器端通信:
```c
// 客户端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 8888
#define BUFFER_SIZE 1024
int main() {
int client_socket;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
// 创建客户端socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Failed to create socket");
exit(EXIT_FAILURE);
}
// 设置服务器地址信息
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr) <= 0) {
perror("Invalid address/Address not supported");
exit(EXIT_FAILURE);
}
// 连接服务器
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Connection failed");
exit(EXIT_FAILURE);
}
// 从服务器接收数据
memset(buffer, 0, sizeof(buffer));
if (read(client_socket, buffer, sizeof(buffer)) < 0) {
perror("Failed to receive data");
exit(EXIT_FAILURE);
}
printf("Received data from server: %s\n", buffer);
// 关闭客户端socket
close(client_socket);
return 0;
}
```
```c
// 服务器端代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVER_PORT 8888
#define BUFFER_SIZE 1024
#define MESSAGE "Hello, client!"
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
char buffer[BUFFER_SIZE];
// 创建服务器端socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Failed to create socket");
exit(EXIT_FAILURE);
}
// 设置服务器地址信息
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
// 绑定socket到指定地址和端口
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
exit(EXIT_FAILURE);
}
// 监听连接请求
if (listen(server_socket, 5) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
// 等待客户端连接
printf("Waiting for client connection...\n");
socklen_t client_addr_len = sizeof(client_addr);
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_socket < 0) {
perror("Accept failed");
exit(EXIT_FAILURE);
}
// 向客户端发送数据
if (write(client_socket, MESSAGE, sizeof(MESSAGE)) < 0) {
perror("Failed to send data");
exit(EXIT_FAILURE);
}
// 关闭服务器端socket和客户端socket
close(client_socket);
close(server_socket);
return 0;
}
```
这个示例演示了一个简单的客户端和服务器端通信。客户端首先创建一个socket,并与指定的服务器进行连接。客户端连接成功后,从服务器接收数据并打印。服务器端创建一个socket,并绑定到指定的地址和端口。然后,服务器端监听连接请求,并等待客户端连接。当客户端连接成功后,服务器端向客户端发送一条消息。完成通信后,服务器端和客户端关闭各自的socket连接。
阅读全文