Sponsored Content
Homework and Emergencies Homework & Coursework Questions Client/server atm with sockets Post 302371307 by tamanas on Saturday 14th of November 2009 05:56:03 AM
Old 11-14-2009
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
 

10 More Discussions You Might Find Interesting

1. Programming

Sockets donot send any thing to client

I have written a socket program in C under RedHat Linux 6.2. This program is using SOCK_STREAM (TCP) to connect to any ip address at port # 23. The program is hosted on the linux machine and works well with Cisco routers. But when I am trying to connect to the same linux machine, it does not return... (4 Replies)
Discussion started by: biswa
4 Replies

2. UNIX for Advanced & Expert Users

FTP over ATM issue

We just recently upgraded our T1 wan link to a 10Mbit ATM link. Windows pc's seem fine and get great speed, however we have 2 Sco unix box's that seem really slow at FTPing now. both Sco box's are SCO OpenServer Enterprise System (ver 5.0.5m) compaq ML530's I'm not really sure what to... (4 Replies)
Discussion started by: whegra
4 Replies

3. UNIX for Dummies Questions & Answers

client/server

Hi, yes i belong to that duummies group of people so here is the question that i need someone to explain it to me and posiblly to answere it to me in a plain english. This is the question: Describe the concept of “client-server” software. Discuss what each side of the equation... (2 Replies)
Discussion started by: bole
2 Replies

4. Programming

How to acknowledge from client to server in sockets

Hi all, i am having a doubt regarding how to get the acknowledgement(reply) from client to server & how to recieve at the server....imagine there r 8 frames...we have to send first 4frames at a time and after tat we have to receive and ack and only after receiving an ack we have to send... (1 Reply)
Discussion started by: Rohil
1 Replies

5. Shell Programming and Scripting

Client-server

Hi, I have installed the vmware server on my debian os and has several clients connected to it. Is there any script that enable the server to restart the client automatically?? Can anyone help. Thanks in advance (3 Replies)
Discussion started by: kanexxx
3 Replies

6. Programming

Client/Server Socket Application - Preventing Client from quitting on server crash

Problem - Linux Client/Server Socket Application: Preventing Client from quitting on server crash Hi, I am writing a Linux socket Server and Client using TCP protocol on Ubuntu 9.04 x64. I am having problem trying to implement a scenario where the client should keep running even when the... (2 Replies)
Discussion started by: varun.nagpaal
2 Replies

7. Windows & DOS: Issues & Discussions

Office server => laptop =>client server ...a lengthy and laborious ftp procedure

Hi All, I need your expertise in finding a way to solve my problem.Please excuse if this is not the right forum to ask this question and guide me to the correct forum,if possible. I am a DBA and on a daily basis i have to ftp huge dump files from my company server to my laptop and then... (3 Replies)
Discussion started by: kunwar
3 Replies

8. Programming

Client Server C

Hello, Please I would create a client and a server in C that communicate frequently. The client sends "hello message" to the server, the server waits a few minutes and sends a "hello message" to the client, the client sends again "hello server ".. etc up to 15 minutes Can you guide me... (3 Replies)
Discussion started by: chercheur857
3 Replies

9. UNIX for Dummies Questions & Answers

Client server C

Hello, Please, is there on unix.com the source code of a client C and server C: as shown in following figure: File:InternetSocketBasicDiagram zhtw.png - Wikipedia, the free encyclopedia Thank you so much for help (1 Reply)
Discussion started by: chercheur857
1 Replies

10. Shell Programming and Scripting

Sftp script for dev server to client server

hi, i am new to unix, cuold u send some sftp acripts to send files to dev server to clint server, (1 Reply)
Discussion started by: Koti.annam
1 Replies
SENDMMSG(2)						     Linux Programmer's Manual						       SENDMMSG(2)

NAME
sendmmsg - send multiple messages on a socket SYNOPSIS
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <sys/socket.h> int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags); DESCRIPTION
The sendmmsg() system call is an extension of sendmsg(2) that allows the caller to transmit multiple messages on a socket using a single system call. (This has performance benefits for some applications.) The sockfd argument is the file descriptor of the socket on which data is to be transmitted. The msgvec argument is a pointer to an array of mmsghdr structures. The size of this array is specified in vlen. The mmsghdr structure is defined in <sys/socket.h> as: struct mmsghdr { struct msghdr msg_hdr; /* Message header */ unsigned int msg_len; /* Number of bytes transmitted */ }; The msg_hdr field is a msghdr structure, as described in sendmsg(2). The msg_len field is used to return the number of bytes sent from the message in msg_hdr (i.e., the same as the return value from a single sendmsg(2) call). The flags argument contains flags ORed together. The flags are the same as for sendmsg(2). A blocking sendmmsg() call blocks until vlen messages have been sent. A nonblocking call sends as many messages as possible (up to the limit specified by vlen) and returns immediately. On return from sendmmsg(), the msg_len fields of successive elements of msgvec are updated to contain the number of bytes transmitted from the corresponding msg_hdr. The return value of the call indicates the number of elements of msgvec that have been updated. RETURN VALUE
On success, sendmmsg() returns the number of messages sent from msgvec; if this is less than vlen, the caller can retry with a further sendmmsg() call to send the remaining messages. On error, -1 is returned, and errno is set to indicate the error. ERRORS
Errors are as for sendmsg(2). An error is returned only if no datagrams could be sent. VERSIONS
The sendmmsg() system call was added in Linux 3.0. Support in glibc was added in version 2.14. CONFORMING TO
sendmmsg() is Linux-specific. NOTES
The value specified in vlen is capped to UIO_MAXIOV(1024). EXAMPLE
The example below uses sendmmsg() to send onetwo and three in two distinct UDP datagrams using one system call. The contents of the first datagram originates from a pair of buffers. #define _GNU_SOURCE #include <netinet/ip.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> int main(void) { int sockfd; struct sockaddr_in sa; struct mmsghdr msg[2]; struct iovec msg1[2], msg2; int retval; sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd == -1) { perror("socket()"); exit(EXIT_FAILURE); } sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); sa.sin_port = htons(1234); if (connect(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == -1) { perror("connect()"); exit(EXIT_FAILURE); } memset(msg1, 0, sizeof(msg1)); msg1[0].iov_base = "one"; msg1[0].iov_len = 3; msg1[1].iov_base = "two"; msg1[1].iov_len = 3; memset(&msg2, 0, sizeof(msg2)); msg2.iov_base = "three"; msg2.iov_len = 5; memset(msg, 0, sizeof(msg)); msg[0].msg_hdr.msg_iov = msg1; msg[0].msg_hdr.msg_iovlen = 2; msg[1].msg_hdr.msg_iov = &msg2; msg[1].msg_hdr.msg_iovlen = 1; retval = sendmmsg(sockfd, msg, 2, 0); if (retval == -1) perror("sendmmsg()"); else printf("%d messages sent ", retval); exit(0); } SEE ALSO
recvmmsg(2), sendmsg(2), socket(2), socket(7) Linux 2014-07-08 SENDMMSG(2)
All times are GMT -4. The time now is 10:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy