Hi guys, With your help I am able to send data between two processes using sockets. Thank you.
I tried to extend my program. This is to insert and display the details of a person.
there are two processes- server.c and client.c.
for insertion - client process takes the input data and sends to server. Server must read the data and write to a file.
for display - client must retrieve the data from the sever(which is stored in file) and display it.
Can anyone help me with my code. First I am sending the select option for insertion and display then sending the data from client to server. Is this the correct or is there any good approach for this kind of situation.
I also had one error, can anyone please help me.
Code:
server.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<unistd.h>
#include<errno.h>
typedef struct
{
char lastname[25];
char firstname[25];
char address[25];
char phonenumber[25];
}addressbook;
addressbook a={0,0};
int main (int argc, char* const argv[])
{
int ret;
FILE *fp;
const char* const socket_name ="/tmp/socket";
int socket_fd;
int select;
struct sockaddr_un name;
/* Create the socket. */
socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
/* Indicate that this is a server. */
name.sun_family = AF_LOCAL;
strcpy (name.sun_path, socket_name);
bind (socket_fd, (struct sockaddr*)&name, SUN_LEN (&name));
/* Listen for connections. */
listen (socket_fd, 5);
struct sockaddr_un client_name;
socklen_t client_name_len;
int client_socket_fd;
/* Accept a connection. */
client_socket_fd = accept (socket_fd, (struct sockaddr*)&client_name, &client_name_len);
read (client_socket_fd, &select, sizeof(select));
if(select == 1){
ret = read (client_socket_fd, &a, sizeof(a));
if(ret != 0) {
printf("error: %s\n",strerror(errno));
}
printf("lastname of person : %s\n", a.lastname);
printf("firstname of person : %s\n", a.firstname);
printf("address of person : %s\n", a.address);
printf("phone number of person : %s\n", a.phonenumber);
fp = fopen("addressbook.dat","a+");
fwrite(&a, sizeof(addressbook), 1, fp);
fclose(fp);
} elseif (select == 2){
fp = fopen("addressbook.dat", "r");
fread(&a, sizeof(addressbook), 1, fp);
write (client_socket_fd, (struct sockaddr*)&a, sizeof(a));
}else{
printf("wrong selection");
}
/* Close our end of the connection. */
close (client_socket_fd);
/* Remove the socket file.*/
close (socket_fd);
unlink (socket_name);
return 0;
}
Code:
error:
socket.c: In function ‘main’:
socket.c:58: error: expected ‘;’ before ‘{’ token
Code:
client.c
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<unistd.h>
typedef struct
{
char lastname[25];
char firstname[25];
char address[25];
char phonenumber[25];
}addressbook;
addressbook a={0,0};
int main (int argc, char* const argv[])
{
const char* const socket_name = "/tmp/socket";
const char* const message;
int socket_fd;
int select;
struct sockaddr_un name;
/* Create the socket. */
socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
/* Store the server’s name in the socket address. */
name.sun_family = AF_LOCAL;
strcpy (name.sun_path, socket_name);
/* Connect the socket. */
connect (socket_fd, (struct sockaddr*)&name, SUN_LEN (&name));
do {
printf("Address book: \n Press\n 1: Insert \n 2: Display \n Enter:");
scanf("%d", &select);
if(select == 1){
write (socket_fd, select, sizeof(select));
/* Write the text on the command line to the socket.*/
printf("enter details\n");
printf("enter lastname of person :\n");
scanf("%s", a.lastname);
printf("enter firstname of person :\n");
scanf("%s", a.firstname);
printf("enter address of person :\n");
scanf("%s", a.address);
printf("enter phone number of person :\n");
scanf("%s", a.phonenumber);
write (socket_fd, (struct sockaddr*)&a, sizeof(a));
}elseif(select == 2){
write (socket_fd, select, sizeof(select));
read (socket_fd, &a, sizeof(a));
printf("lastname of person : %s\n", a.lastname);
printf("firstname of person : %s\n", a.firstname);
printf("address of person : %s\n", a.address);
printf("phone number of person : %s\n", a.phonenumber);
}else{
printf("wrong selection");
}
if((select > 4) || (select < 1))
break;
}while(1);
/*close the socket */
close (socket_fd);
return 0;
}
Code:
error:
socket1.c: In function ‘main’:
socket1.c:39: warning: passing argument 2 of ‘write’ makes pointer from integer without a cast
/usr/include/unistd.h:363: note: expected ‘const void *’ but argument is of type ‘int’
socket1.c:51: error: expected ‘;’ before ‘{’ token
Can anyone please help me with this. Thank you.