tcp客户端与服务器通信 c++ 代码实现
时间: 2023-05-26 14:03:35 浏览: 114
C++实现Tcp客户端和服务器端的通信
以下是一个基本的TCP客户端与服务器通信的C代码实现。
服务器端:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
int main()
{
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
char buffer[1024];
int bytes_received, bytes_sent;
socklen_t client_addr_len;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8888);
server_addr.sin_addr.s_addr = INADDR_ANY;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("socket");
exit(1);
}
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
exit(1);
}
if (listen(server_socket, 5) < 0) {
perror("listen");
exit(1);
}
printf("Server started...\n");
while (1) {
client_addr_len = sizeof(client_addr);
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_socket < 0) {
perror("accept");
exit(1);
}
printf("Client connected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
while (1) {
memset(buffer, 0, sizeof(buffer));
bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received < 0) {
perror("recv");
exit(1);
} else if (bytes_received == 0) {
printf("Client disconnected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
break;
}
printf("Received from client: %s", buffer);
bytes_sent = send(client_socket, buffer, bytes_received, 0);
if (bytes_sent < 0) {
perror("send");
exit(1);
}
}
close(client_socket);
}
close(server_socket);
return 0;
}
```
客户端:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
int main()
{
int client_socket;
struct sockaddr_in server_addr;
struct hostent *server;
char buffer[1024];
int bytes_received, bytes_sent;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8888);
server = gethostbyname("localhost");
if (server == NULL) {
fprintf(stderr, "Error: no such host\n");
exit(1);
}
memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("socket");
exit(1);
}
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(1);
}
printf("Connected to server: %s:%d\n", inet_ntoa(server_addr.sin_addr), ntohs(server_addr.sin_port));
while (1) {
memset(buffer, 0, sizeof(buffer));
printf("Enter message: ");
fgets(buffer, sizeof(buffer), stdin);
bytes_sent = send(client_socket, buffer, strlen(buffer), 0);
if (bytes_sent < 0) {
perror("send");
exit(1);
}
bytes_received = recv(client_socket, buffer, sizeof(buffer), 0);
if (bytes_received < 0) {
perror("recv");
exit(1);
} else if (bytes_received == 0) {
printf("Server disconnected\n");
break;
}
printf("Received from server: %s", buffer);
}
close(client_socket);
return 0;
}
```
阅读全文