Skip to main content
ark2
Associate II
November 7, 2022
Question

I want to make Raspberry Pie a client and connect it to the server

  • November 7, 2022
  • 0 replies
  • 13278 views

I want to make Stm32 a server and send and receive messages through Raspberry Pi and WiFi

Raspberry Pie made a code in C language

if ((server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
 
 printf("socket() error!");
 
 }
 
 memset((char*)&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((unsigned short)server_port);
 
 
 //bind
 
 setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuseflag, sizeof(reuseflag));
 
 while (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1)
 
 {
 
 printf("blind() eroor");
 
 }
 //listen
 
 if (listen(server_socket, SOMAXCONN) < 0) {
 
 printf("listen() eroor");
 
 }
 
 printf("Server Ready!!\n");
 
 while (1)
 
 {
 
 printf("Waiting Client...\n");
 
 //accept 
 len = sizeof(client_addr);
 
 client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &len);
 
 if (client_socket == 0)
 
 {
 
 printf("accept()");
 
 close(server_socket);
 
 return 0;
 
 }
 
 
 printf("client connected\n");
 
 get_client_ip(client_socket);
 
 int sockState = 0;
 
 while (1) {
 
 if (connectIotClient == NOTCONNECTED) {
 
 printf("Iot\n");
 
 if (init_client(IotIpAddress, IotPort, &IOTClient_socket) != 0)
 
 {
 
 connectIotClient = NOTCONNECTED;
 
 }
 
 else
 
 {
 
 connectIotClient = CONNECTED;
 
 }
 
 }
 
 printf("\n");
 
 
 if (connectFiberClient == NOTCONNECTED)
 {
 printf("fiber\n");
 
 
 if (init_client(fiberIpAddress, fiberPort, &fiberClient_socket) != 0) {
 
 connectFiberClient = NOTCONNECTED;
 
 }
 
 else
 
 {
 connectFiberClient = CONNECTED;
 
 }
 
 }
 
 printf("\n");
 
 
 
int init_client(char* ipAddr, int port, int* _clientSocket)
 
{
 
 struct sockaddr_in serveraddr;
 
 //WSADATA wsaData;
 
 int ret = 0;
 
 //char buf[BUFSIZE] = { 0, };
 
 int sock;
 
 
 //socket()
 
 sock = socket(AF_INET, SOCK_STREAM, 0); //TCP/IP¿ë ¼ÒÄ�? »ý¼º
 
 
 if (sock == 0) printf("socket()");
 
 //connect()
 
 memset(&serveraddr, 0, sizeof(serveraddr));
 
 serveraddr.sin_family = AF_INET;
 
 serveraddr.sin_addr.s_addr = inet_addr(ipAddr);
 
 serveraddr.sin_port = htons(port);
 
 if (connect(sock, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) != 0)
 {
 
 printf("connect ERROR!!\n");
 ret = -1;
 }
 
 else
 {
 
 printf("connect....\n");
 
 *_clientSocket = sock;
 
 ret = 0;
 
 }
 
 return ret;
 
}

I used the code to operate as a server, but I need to change it to a client, so how should I write it?

Stm32 is working as a server and connecting it with hercules (tcp client), so it seems like it's working well with the server, so there's no problem with implementing the server

(Stm32 and raspberry have very different code types, so they can't be exchanged.)

If you test it simply using the code below, you can send and receive it, but I don't know why it doesn't work on the top

I changed AF_INET to PF_INET

Iot, you can't comment on where you're connecting, or you can change the location

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
#define IotIpAddress "192.168.0.***"
#define IotPort 5001
unsigned char test_msg[6] = {0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF};
 
 
void ErrorHandling(char* message);
 
int main(int argc, char* argv[])
{
	int hSocket;
	struct sockaddr_in servAddr;
 
	char message[30];
	int strLen;
 
	if (argc != 3)
	{
		printf("Usage : %s \n", argv[0]);
		exit(1);
	}
 
	hSocket = socket(PF_INET, SOCK_STREAM, 0);
	if (hSocket == 0)
		ErrorHandling("socket() error");
 
	memset(&servAddr, 0, sizeof(servAddr));
	servAddr.sin_family = AF_INET;
	servAddr.sin_addr.s_addr = inet_addr(argv[1]);
	servAddr.sin_port = htons(atoi(argv[2]));
	if (connect(hSocket, (struct sockaddr*)&servAddr, sizeof(servAddr)) != 0)
		ErrorHandling("connect() error!");
	
 send(hSocket, test_msg, 6, 0);
 
 
	strLen = recv(hSocket, message, sizeof(message) - 1, 0);
	if (strLen == -1)
		ErrorHandling("read() error!");
	for(int j = 0; j < strLen; j++){
		printf("%02X ", message[j]);
		}
		printf("\n");
	return 0;
}
 
void ErrorHandling(char* message)
{
	fputs(message, stderr);
	fputc('\n', stderr);
	exit(1);
}

This topic has been closed for replies.