网络编程示例:基于套接字的简单服务器和客户端
#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 8888
#define BUFFER_SIZE 1024
void error(const char *msg) {
perror(msg);
exit(1);
}
int main() {
int sockfd, newsockfd;
socklen_t clilen;
char buffer[BUFFER_SIZE];
struct sockaddr_in serv_addr, cli_addr;
int n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
error("ERROR opening socket");
}
memset((char *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
error("ERROR on binding");
}
listen(sockfd, 5);
printf("Server listening on port %d...\n", PORT);
clilen = sizeof(cli_addr);
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
error("ERROR on accept");
}
memset(buffer, 0, BUFFER_SIZE);
n = read(newsockfd, buffer, BUFFER_SIZE - 1);
if (n < 0) {
error("ERROR reading from socket");
}
printf("Received message from client: %s\n", buffer);
n = write(newsockfd, "Server got your message", 23);
if (n < 0) {
error("ERROR writing to socket");
}
close(newsockfd);
}
close(sockfd);
return 0;
}
创建TCP服务器
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main() {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
perror("socket");
return 1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(12345);
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
close(server_fd);
return 1;
}
if (listen(server_fd, 5) == -1) {
perror("listen");
close(server_fd);
return 1;
}
client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
if (client_fd == -1) {
perror("accept");
close(server_fd);
return 1;
}
char buffer[128];
ssize_t bytes_read = read(client_fd, buffer, sizeof(buffer));
if (bytes_read == -1) {
perror("read");
} else {
printf("Received from client: %s\n", buffer);
}
close(client_fd);
close(server_fd);
return 0;
}
创建TCP客户端
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main() {
int sock_fd;
struct sockaddr_in server_addr;
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1) {
perror("socket");
return 1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(12345);
if (inet_pton(AF_INET, "192.168.1.1", &server_addr.sin_addr) <= 0) {
perror("inet_pton");
close(sock_fd);
return 1;
}
if (connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
close(sock_fd);
return 1;
}
const char *message = "Hello, Server!";
ssize_t bytes_sent = write(sock_fd, message, strlen(message));
if (bytes_sent == -1) {
perror("write");
close(sock_fd);
return 1;
}
char buffer[128];
ssize_t bytes_received = read(sock_fd, buffer, sizeof(buffer));
if (bytes_received == -1) {
perror("read");
} else {
printf("Received from server: %s\n", buffer);
}
close(sock_fd);
return 0;
}