#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
int socketInit(struct InputCommander *socketMsg,char *ipAdress,char *port)
{
int s_fd;
struct sockaddr_in s_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
//socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1)
{
perror("socket");
exit(-1);
}
//bind
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(socketMsg->port));
inet_aton(socketMsg->socketAdress,&s_addr.sin_addr);
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//listen
listen(s_fd,10);
socketMsg->sfd = s_fd;
return s_fd;
}
int socketGetCommand(struct InputCommander *socketMsg)
{
int c_fd;
int n_read;
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
int c_len = sizeof(struct sockaddr_in);
c_fd = accept(socketMsg->sfd,(struct sockaddr *)&c_addr,&c_len);
n_read = read(c_fd,socketMsg->command,sizeof(socketMsg->command));
if(n_read == -1)
{
perror("read");
}if(n_read>0)
{
printf("\nget:%d\n",n_read);
}
}
struct InputCommander socketContrl = {
.commandName = "socket",
.command = {'\0'},
.port = "8088",
.socketAdress = "192.168.1.176",
.Init = socketInit,
.getCommand = socketGetCommand,
.log = {'\0'},
.next = NULL
};
struct InputCommander *addsocketContrlToCommandLink(struct InputCommander *phead)
{
if(phead == NULL)
{
return &socketContrl;
}else
{
socketContrl.next = phead;
phead = &socketContrl;
return phead;
}
}
树莓派开发实战项目 智能家居--简单工厂模式(初步实现socket服务器“有部分问题”)
最新推荐文章于 2025-02-01 18:10:29 发布