Hello everyone!I would appreciate any help you could give me,I have to make a program of an atm machine using client server sockets and semaphores.I know how to construct an ATM in c++ but don't know anything about unix c.The problem is that Don't know what to do and how to link the two programs together.
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
void error(char *);
void main(int argc, char *argv[])
{
int sockfd, servlen,n;
struct sockaddr_un serv_addr;
char buffer[82];
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, argv[1]);
servlen = strlen(serv_addr.sun_path) +
sizeof(serv_addr.sun_family);
if ((sockfd = socket(AF_UNIX, SOCK_STREAM,0)) < 0)
error("Creating socket");
if (connect(sockfd, (struct sockaddr *)
&serv_addr, servlen) < 0)
error("Connecting");
printf("Please enter your message: ");
bzero(buffer,82);
fgets(buffer,80,stdin);
write(sockfd,buffer,strlen(buffer));
n=read(sockfd,buffer,80);
printf("The return message was\n");
write(1,buffer,n);
}
void error(char *msg)
{
perror(msg);
exit(0);
}
/* a server in the unix domain. The pathname of
the socket address is passed as an argument */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
void error(char *);
int main(int argc, char *argv[])
{
int sockfd, newsockfd, servlen, clilen, n;
struct sockaddr_un cli_addr, serv_addr;
char buf[80];
if ((sockfd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
error("creating socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, argv[1]);
servlen=strlen(serv_addr.sun_path) +
sizeof(serv_addr.sun_family);
if(bind(sockfd,(struct sockaddr *)&serv_addr,servlen)<0)
error("binding socket");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(
sockfd,(struct sockaddr *)&cli_addr,&clilen);
if (newsockfd < 0)
error("accepting");
n=read(newsockfd,buf,80);
printf("A connection has been established\n");
write(1,buf,n);
write(newsockfd,"I got your message\n",19);
}
void error(char *msg)
{
perror(msg);
exit(0);
}
:
- Computer engineering informatics Department (University), Patras , Greece, Triantafylou, 330(operating systems):
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).