上位机与下位机进行tcp通讯案例
时间: 2023-06-27 14:07:58 浏览: 66
下面是一个上位机与下位机进行TCP通讯的案例:
下位机代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8888
int main()
{
int sockfd, connfd;
struct sockaddr_in serveraddr, clientaddr;
char buffer[1024];
int n;
// 创建套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("Failed to create socket.\n");
exit(1);
}
// 初始化服务器地址
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(PORT);
// 绑定套接字
if (bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) == -1) {
printf("Failed to bind.\n");
exit(1);
}
// 监听连接请求
if (listen(sockfd, 5) == -1) {
printf("Failed to listen.\n");
exit(1);
}
while (1) {
printf("Waiting for connection...\n");
// 接收连接请求
socklen_t clientaddrlen = sizeof(clientaddr);
connfd = accept(sockfd, (struct sockaddr*)&clientaddr, &clientaddrlen);
if (connfd == -1) {
printf("Failed to accept.\n");
continue;
}
printf("Accept new connection from %s:%d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
// 接收客户端发送的数据
memset(buffer, 0, sizeof(buffer));
n = read(connfd, buffer, sizeof(buffer)-1);
if (n == -1) {
printf("Failed to read data.\n");
close(connfd);
continue;
}
printf("Received data from client: %s\n", buffer);
// 发送响应数据到客户端
const char* response = "Hello, I'm server!";
n = write(connfd, response, strlen(response));
if (n == -1) {
printf("Failed to send response data.\n");
close(connfd);
continue;
}
// 关闭连接
close(connfd);
}
close(sockfd);
return 0;
}
```
上位机代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVER_IP "127.0.0.1"
#define PORT 8888
int main()
{
int sockfd;
struct sockaddr_in serveraddr;
char buffer[1024];
int n;
// 创建套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("Failed to create socket.\n");
exit(1);
}
// 初始化服务器地址
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(SERVER_IP);
serveraddr.sin_port = htons(PORT);
// 连接服务器
if (connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) == -1) {
printf("Failed to connect to server.\n");
exit(1);
}
// 发送数据到服务器
const char* data = "Hello, I'm client!";
n = write(sockfd, data, strlen(data));
if (n == -1) {
printf("Failed to send data.\n");
close(sockfd);
exit(1);
}
// 接收服务器响应数据
memset(buffer, 0, sizeof(buffer));
n = read(sockfd, buffer, sizeof(buffer)-1);
if (n == -1) {
printf("Failed to receive response data.\n");
close(sockfd);
exit(1);
}
printf("Received data from server: %s\n", buffer);
close(sockfd);
return 0;
}
```
这个案例中,下位机作为服务器,等待上位机的连接请求,一旦收到请求,就接收数据并发送响应数据。上位机作为客户端,连接下位机服务器后发送数据,接收服务器响应数据。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)