The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers > Homework & Coursework Questions
.
grep unix.com with google



Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-14-2009
Registered User
 

Join Date: Nov 2009
Posts: 1
Client/server atm with sockets

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.







3.


Code:
#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);
}
:



4. 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).

Last edited by bakunin; 11-15-2009 at 10:08 PM.. Reason: added code-tags. Please provide them yourself henceforth
Sponsored Links
Reply

Bookmarks

Tags
atm, client, server, sockets

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Client/Server Socket Application - Preventing Client from quitting on server crash varun.nagpaal High Level Programming 2 08-27-2009 11:57 PM
Client-server kanexxx Shell Programming and Scripting 3 11-25-2008 01:05 AM
How to acknowledge from client to server in sockets Rohil High Level Programming 1 11-07-2008 09:15 AM
ntp server and ntp client bubba112557 SUN Solaris 1 05-10-2005 11:37 AM
Sockets donot send any thing to client biswa High Level Programming 4 07-31-2001 10:24 AM



All times are GMT -4. The time now is 01:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0