Client send data and raspberry pi(server) record data in txt file. You can connect any two devices by changing the server ip (raspi-raspi or raspi-pc)
if you have questions , you can ask me
client.c
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
int main(){
int socket_fd;
struct sockaddr_in caddr;
char *ip="10.42.0.249";
char msg[100];
//Socket
Address
caddr.sin_family=AF_INET;
caddr.sin_port=htons(7774); //you
can give port number as you want ,but can not special port or already used port
if(inet_aton(ip,&caddr.sin_addr)==0){
return (-1);
}
//Create
socket
socket_fd=socket(PF_INET,SOCK_STREAM,0);
if(socket_fd == -1){
printf("Error on socket creation [%s] \n",strerror(errno));
return (-1);
}
//Establish
connection
if(connect(socket_fd, (const struct sockaddr *)&caddr,sizeof(struct sockaddr))== -1){
printf("Error on socket connect [%s] \n",strerror(errno));
return (-1);
}
//Read
and write data
//client
sent word until the user input "quit" message
while(strncmp(msg,"quit",4)){
printf("Enter a word: ");
scanf("%s",msg);
if(write(socket_fd,msg,sizeof(msg))!=sizeof(msg)){
printf("Error writing [%s] \n",strerror(errno));
}
else{
printf("Sent data [%s] \n",msg);
}
}
//Close
the connection
close(socket_fd);
return 0;
}
server.c
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include <signal.h>
int main(){
int server_fd,client_fd;
struct sockaddr_in caddr,saddr;
char *ip="10.42.0.249"; //server ip
char msg[100];
FILE *file;
char* filename="kayit.txt";
file=fopen(filename,"w+");
//Server
Socket Address
saddr.sin_family=AF_INET;
saddr.sin_port=htons(7774); //create port
saddr.sin_addr.s_addr=htonl(INADDR_ANY);
//Create
server socket
server_fd=socket(PF_INET,SOCK_STREAM,0);
if(server_fd == -1){
printf("Error on socket creation [%s] \n",strerror(errno));
return (-1);
}
//Bind
the address to server socket
if(bind(server_fd,(struct sockaddr *)&saddr, sizeof(struct sockaddr))==-1){
printf("Error socket bind [%s] \n",strerror(errno));
return (-1);
}
//Listen
for a connection request
if(listen(server_fd,1)){
printf("Error socket listen [%s] \n",strerror(errno));
return (-1);
}
//Bind
the address to server socket
if(bind(server_fd,(struct sockaddr *)&saddr, sizeof(struct sockaddr))==$
printf("Error socket bind [%s] \n",strerror(errno));
return (-1);
}
//Accept
connection request and connect
socklen_t addrSize=sizeof(caddr);
if((client_fd=accept(server_fd,(struct
sockaddr*)&caddr,&addrSize))==-1){
printf("Error socket accept [%s] \n",strerror(errno));
close(server_fd);
return (-1);
}
printf("Server
new connecton has been established [%s/%d]\n",inet_ntoa(caddr.sin_addr),caddr.sin_port);
int size;
int client=0;
int ind=0;
while(!client){
//read and write maeesages
if((size=read(client_fd,msg,sizeof(msg)))==-1){
printf("Error reading [%s] \n",strerror(errno));
}
else{
msg[size]='\0';
if(fputs(msg,file)!=-1){
printf("Recorded data
[%s]\n",msg);
}
}
fflush(file);
if(!strncmp(msg,"quit",4)){ // if
client send quit word , client
connection and server are closed
printf("Record ending....\n");
close(client);
client=1;
}
}
//Close
connections
fclose(file);
close(server_fd);
return 0;
}