PDA

View Full Version : How to pass data using Sockets



chaituchemico
09-08-2011, 12:17 AM
Guys can anyone help me to How to pass data between two different processes using sockets.

Here are the steps using sockets (correct me if I am wrong)

Process1(server):
1. Create a socket
2. Bind
3. listen
4. Accept
5. Close

Process2(Client):
1. Create a Socket
2. Connect
3. Close

In sockets, we have to use the same name for both the sockets. Can anyone please tell me how to give the same name.

While coming to pass the data between processes, If I wish to pass a struct of data using linked lists...Is it possible.

Can anyone please help me with this and kindly share any sample programs using Sockets if you have any. Thank you.

chaituchemico
09-08-2011, 10:39 AM
SERVER.C
int server (int client_socket)
{
while (1) {
int length;
char* text;
/* First, read the length of the text message from the socket.
read returns zero, the client closed the connection. */
if (read (client_socket, &length, sizeof (length)) == 0)
return 0;
/* Allocate a buffer to hold the text. */
text = (char*) malloc (length);
/* Read the text itself, and print it. */
read (client_socket, text, length);
printf (“%s\n”, text);
/* Free the buffer. */
free (text);
/* If the client sent the message “quit,” we’re all done.
if (!strcmp (text, “quit”))
return 1;
}
}

int main (int argc, char* const argv[])
{
const char* const socket_name = argv[1];
int socket_fd;
struct sockaddr_un name;
int client_sent_quit_message;
/* 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, &name, SUN_LEN (&name));
/* Listen for connections. */
listen (socket_fd, 5);
/* Repeatedly accept connections, spinning off one server() to deal
with each client. Continue until a client sends a “quit” message.
do {
struct sockaddr_un client_name;
socklen_t client_name_len;
int client_socket_fd;
*/
/* Accept a connection. */
client_socket_fd = accept (socket_fd, &client_name, &client_name_len);
/* Handle the connection. */
client_sent_quit_message = server (client_socket_fd);
/* Close our end of the connection. */
close (client_socket_fd);
}
while (!client_sent_quit_message);
/* Remove the socket file.
close (socket_fd);
unlink (socket_name);
*/
return 0;
}




CLIENT.C

/* Write TEXT to the socket given by file descriptor SOCKET_FD.
*/
void write_text (int socket_fd, const char* text)
{
/* Write the number of bytes in the string, including
NUL-termination. */
int length = strlen (text) + 1;
write (socket_fd, &length, sizeof (length));
/* Write the string. */
write (socket_fd, text, length);
}
int main (int argc, char* const argv[])
{
const char* const socket_name = argv[1];
const char* const message = argv[2];
int socket_fd;
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, &name, SUN_LEN (&name));
/* Write the text on the command line to the socket. */
write_text (socket_fd, message);
close (socket_fd);
return 0;
}


Here for this program we are giving the same name in command prompt for both server & client. What we have to do to give the same program name for the sockets in the program itself (can we use #define and assign a name for program)
How to send a struct of data for employees like name, address, email, etc using linked lists from client to server.
Can anyone please help me with this, I am not getting it. I sincerely appreciate your help. Thank you.

MK27
09-08-2011, 11:21 AM
Most people, including me, will not want to read your code unless you edit the post and INDENT PROPERLY.



Here for this program we are giving the same name in command prompt for both server & client.

Yeah, PF_LOCAL socket "addresses" are special files. Beware that if the file already exists and you try and bind() to it, you'll fail. I notice you have unlink() on the file at the end of the server code, that's good.



How to send a struct of data for employees like name, address, email, etc using linked lists from client to server.

You have to serialize it according to some protocol that you make up, and apply that.

http://sourceforge.net/apps/mediawiki/cpwiki/index.php?title=Serialization

You might find the part about linked lists (under "Example Uses") interesting.

IMO, a lot of IPC is better done with UDP (SOCK_DGRAM) sockets. They are easier to use/less prone to problems.

chaituchemico
09-08-2011, 12:01 PM
Thank you MK27.

const char* const socket_name = argv[1];
Here we are giving the same socket name for server & client using command prompt.
Is there any other way I can include same socket name for both server & client in the program itself.

MK27
09-08-2011, 12:16 PM
Here we are giving the same socket name for server & client using command prompt.
Is there any other way I can include same socket name for both server & client in the program itself.

Hardcode it:



const char* const socket_name = "/tmp/mysocket";

chaituchemico
09-08-2011, 01:44 PM
Thank you MK27.
I have gone through Serialization, it was a good document.

const char* const message = argv[2];
In the above program, I am sending data from the command prompt.
If I wish to send employee data (name, address, email). If I initialize a structure and array or linked list can I pass this type of data using sockets. Please help me in how to send data from client to sockets and how to read it in server.

tabstop
09-08-2011, 01:57 PM
Did you come up with a serialization scheme for your data yet? Once you do so, then that will basically tell you how to build your packet. You seem to have the syntax for write/read (based on skimming your code, since I also am not going to spend a lot of time working through unindented code).

chaituchemico
09-08-2011, 04:12 PM
I am trying to send details of a person from client to sever and display them by server.
I am errors while compiling this code. Please help me, whether is this the correct way or not.



Server.c
#include<stdlib.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";
int socket_fd;
int client_socket;
struct sockaddr_un name;
int client_sent_quit_message;
int length;
char* text;

/* 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, &client_name, &client_name_len);
read (client_socket, &length, sizeof (length));
text = (char*) malloc (length);
read (client_socket, &a, sizeof(length));
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);

/* Free the buffer. */
free (text);

/* Close our end of the connection. */
close (client_socket_fd);

/* Remove the socket file.*/
close (socket_fd);
unlink (socket_name);
return 0;
}

errors:
socket.c: In function ‘main’:
socket.c:44: warning: passing argument 2 of ‘accept’ from incompatible pointer type
/usr/include/sys/socket.h:214: note: expected ‘struct sockaddr * __restrict__’ but argument is of type ‘struct sockaddr_un *’




#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;
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, &name, SUN_LEN (&name));

/* 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);

int length = strlen (a) + 1;
write (socket_fd, &length, sizeof (length));
write (socket_fd, a, length);
close (socket_fd);
return 0;
}

errors:
socket1.c: In function ‘main’:
socket1.c:30: warning: passing argument 2 of ‘connect’ from incompatible pointer type
/usr/include/sys/socket.h:129: note: expected ‘const struct sockaddr *’ but argument is of type ‘struct sockaddr_un *’
socket1.c:41: error: incompatible type for argument 1 of ‘strlen’
/usr/include/string.h:397: note: expected ‘const char *’ but argument is of type ‘addressbook’
socket1.c:43: error: incompatible type for argument 2 of ‘write’
/usr/include/unistd.h:363: note: expected ‘const void *’ but argument is of type ‘addressbook’

tabstop
09-08-2011, 05:38 PM
You'll have to cast your pointers from sockaddr_un to sockaddr.

The rest indicate some level of bafflement. Read requires a pointer to a piece of memory where you intend to put the incoming information; so you will need to provide it with such a thing. strlen requires a string to find the length of; so you will need to provide it with such a thing. (Perhaps you meant "sizeof" instead.) Write requires a pointer to the information that is to be written; so you will need to provide it with such a thing.

MK27
09-08-2011, 05:43 PM
Please help me


Most people, including me, will not want to read your code unless you edit the post and INDENT PROPERLY.


I also am not going to spend a lot of time working through unindented code).

I am not trying to be mean, chaituchemico, but if you cannot put some effort in to help us help you, how much effort do you expect to see in return?

There are a number of accepted ways of indenting C source code; yours is not one of them.

Properly indented code:


int main(void) {
int i;
char str[666];

for (i = 0; i < 666; i++) {
str[i] = '*';
}


Unindented code which is AWKWARD TO READ:


int main(void) {
int i;
char str[666];
for (i = 0; i < 666; i++) {
str[i] = '*';
}


If you can understand the difference, please take some time to format your code correctly, and other people will be happy to look at it. If that is "a waste of time for you", do not expect anyone else to waste theirs.

Does that make sense?

Here's how to get rid of your first warning:
socket1.c:30: warning: passing argument 2 of ‘connect’ from incompatible pointer type



bind (socket_fd, (struct sockaddr*)&name, SUN_LEN (&name));


This is because bind() works with various kinds of sockets which use different structs to store address information -- eg, struct sockaddr_in and struct sockaddr_un -- but those structs share some features common to a base, struct sockaddr (this is a primitive form of object-oriented inheritance, if you are interested in that). So with bind(), you need to cast the second argument to a struct sockaddr.

If you indent your code I might be able to help with the rest of it.

chaituchemico
09-09-2011, 10:46 AM
I also am not going to spend a lot of time working through unindented code).
I am sorry, I did not do it by purpose. May be I just overlooked it. Sorry man.
Sorry for the late reply because we did not have power for last 11 hours.
I edited my post with indented code and and corrections for bind and read.

Thank you @tabstop & @Mk27

chaituchemico
09-09-2011, 12:25 PM
I am able to correct my errors guys with your suggestions. Thank you.


#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;
const char* const socket_name ="/tmp/socket";
int socket_fd;
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;
int client_socket;

/* Accept a connection. */
client_socket_fd = accept (socket_fd, (struct sockaddr*)&client_name, &client_name_len);

ret = read (client_socket_fd, &a, sizeof(a));

if(ret != 0) {
printf("error: %s",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);

/* Close our end of the connection. */
close (client_socket_fd);

/* Remove the socket file.*/
close (socket_fd);
unlink (socket_name);
return 0;
}



#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;
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));

/* 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));

/*close the socket */
close (socket_fd);
return 0;
}

chaituchemico
09-14-2011, 01:32 PM
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.



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;
}




error:
socket.c: In function ‘main’:
socket.c:58: error: expected ‘;’ before ‘{’ token



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;
}



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.

anduril462
09-14-2011, 03:41 PM
Your overall approach of sending the select option (or "command") to the server, then reading/writing data as appropriate is a fine approach. What you aren't doing well is checking the results of all your IO functions. You need to check the return code of socket, bind, listen, accept, connect, read, write, fopen, fread, fwrite, and maybe some others I missed.

Highlighting the lines that have errors is a big help to us. As for the "expected ';' before '{' token" errors, you used "elseif" insted of "else if" (you need a space). As for the "passing argument 2 of 'write'" error, you're calling it incorrectly. The write function isn't type-aware, it just wants a memory address to the start of the data it's going to send. You need to pass the address of the data it's sending, i.e. &select.

chaituchemico
09-14-2011, 04:00 PM
Thank you anduril462. I edited my code, now it is running with no errors.

chaituchemico
09-15-2011, 02:20 PM
This is my code with corrections. Now the code is compiling without any errors. The insertion part is doing good but there is a problem with display part. It is displaying only one person details where it has to display all the data. I think the problem is with read() and write(), but i couldn't correct it. Can anyone please help me. I have also included the code for checking the return value for socket, bind, connect, fread, fwrite. Can anyone please check if there is any wrong in read() and write() and how to make it to display all the data. I am highlighting the insert and display part in server.c and client.c to make it easy.



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>

#define FIRST_NAME_LENGTH 25
#define LAST_NAME_LENGTH 30
#define ADDRESS_LENGTH 25
#define NUMBER_LENGTH 15

typedef struct
{
int select;
char lastname[LAST_NAME_LENGTH];
char firstname[FIRST_NAME_LENGTH ];
char address[ADDRESS_LENGTH];
char phonenumber[NUMBER_LENGTH];
}addressbook;
typedef struct
{
char lastname[LAST_NAME_LENGTH];
char firstname[FIRST_NAME_LENGTH ];
}book;
book b= {0,0};
addressbook a={0,0};
FILE *fp;
char *filename = "addressbook.dat";
int main (int argc, char* const argv[])
{
int ret;
char answer = 'n';
const char* const socket_name ="/tmp/socket";
int socket_fd;
struct sockaddr_un name;

/* Create the socket. */
socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
if (socket_fd < 0){
perror("socket");
exit (EXIT_FAILURE);
}

/* Indicate that this is a server. */
name.sun_family = AF_LOCAL;
strcpy (name.sun_path, socket_name);
if(bind (socket_fd, (struct sockaddr*)&name, SUN_LEN (&name)) == -1){
perror("bind");
exit (EXIT_FAILURE);
}

/* 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);
do{
ret = read (client_socket_fd, &a, sizeof(addressbook));
printf("read return value:%d\n", ret);
if(ret < 0) {
perror("read");
exit (EXIT_FAILURE);
}
if(a.select == 1){
/* to insert data*/
if((fp = fopen(filename,"a+")) == NULL){
printf("Error opening %s for writing. Program terminated.\n", filename);
abort();
}
fwrite(&a, sizeof(addressbook), 1, fp); /* Write to the file */
fclose(fp); /* close file */
printf("New record added\n");
}else if (a.select == 2){
/* to display data*/
if((fp = fopen(filename, "r")) == NULL){
printf("Error opening %s for reading. Program terminated.", filename);
abort();
}
for(;;)
{
fread(&a, sizeof(addressbook), 1, fp); /*read from the file */
if(feof(fp)) /* If we read EOF */
break; /* done */
write (client_socket_fd, &a, sizeof(addressbook));
}
fclose(fp);
}else if (a.select == 3){
/*to delete data*/
FILE *newfp = NULL;
char *newfilename = NULL;
strcpy(b.lastname, a.lastname);
strcpy(b.firstname, a.firstname);
if((fp = fopen(filename, "r")) == NULL){
printf("Error opening %s for reading. Program terminated.", filename);
abort();
}
newfilename = tmpnam(NULL); // create temporary file name
if((newfp = fopen(newfilename, "w")) == NULL){
printf("Error opening %s for writing. Program terminated.", newfilename);
fclose(fp);
abort();
}
for(;;){
fread(&a, sizeof(addressbook), 1, fp); /* read the original file */
if(feof(fp)) /*If we read EOF */
break; /* done */
if((strcmp(b.firstname, a.firstname)==0) && (strcmp(b.lastname, a.lastname)==0)){
printf("record found\n");
printf("Do you really want to delete it(y or n)?");
scanf("%c", &answer);
if(tolower(answer) == 'y') /*if it's to be deleted*/
continue; /*skip copying*/
}
fwrite(&a, sizeof(addressbook), 1, newfp); /*copy current record*/
}
fclose(fp);
fclose(newfp);

if((newfp = fopen(newfilename, "r")) == NULL){ /* Open temporary file to read it */
printf("Error opening %s for reading. Program terminated.", newfilename);
abort();
}
if((fp = fopen(filename, "w"))==NULL){ /* Open original file to write it */
printf("Error opening %s for writing. Program terminated.", filename);
abort();
}
/* Copy contents of new temporary file back to old file */
/* This overwrites the original contents because the mode is "w" */
for(;;) {
fread(&a, sizeof(addressbook), 1, newfp); /* Read temporary file */
if(feof(newfp)) /* If we read EOF */
break; /* done */

fwrite(&a, sizeof(addressbook), 1, fp); /* Write record to original file */
write (client_socket_fd, &a, sizeof(addressbook));
}
fclose(fp); /* Close the original file */
fclose(newfp); /* Close the temporary file */
remove(newfilename); /* Delete the temporary file */
printf("Delete complete.");
}else if(a.select == 4){
/*to modify data*/

}else{
printf("wrong selection\n");
}

if((a.select > 4) || (a.select < 1))
break;
}while(1);

/* Close our end of the connection. */
close (client_socket_fd);

/* Remove the socket file.*/
close (socket_fd);
unlink (socket_name);
return 0;
}






Client.c
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>

#define FIRST_NAME_LENGTH 25
#define LAST_NAME_LENGTH 30
#define ADDRESS_LENGTH 25
#define NUMBER_LENGTH 15

typedef struct
{
int select;
char lastname[LAST_NAME_LENGTH];
char firstname[FIRST_NAME_LENGTH];
char address[ADDRESS_LENGTH];
char phonenumber[NUMBER_LENGTH];
}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 ret;
struct sockaddr_un name;

/* Create the socket. */
if((socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0)) == -1){
perror("socket");
exit (EXIT_FAILURE);
}

/* Store the server’s name in the socket address. */
name.sun_family = AF_LOCAL;
strcpy (name.sun_path, socket_name);
/* Connect the socket. */
if(connect (socket_fd, (struct sockaddr*)&name, SUN_LEN (&name)) == -1){
perror("connect");
exit (EXIT_FAILURE);
}

do {
printf("Address book: \n Press\n 1: Insert \n 2: Display \n3: Delete \n 4: Modify\n Enter:");
scanf("%d", &a.select);
if(a.select == 1){
/* 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, &a, sizeof(addressbook));
sleep (1);
}else if(a.select == 2){
write (socket_fd, &a, sizeof(addressbook));
sleep (1);
read (socket_fd, &a, sizeof(addressbook));
printf(" %s, %s, %s, %s\n", a.lastname, a.firstname, a.address, a.phonenumber);
}else if(a.select == 3){
printf("enter lastname of person to delete:\n");
scanf("%s", a.lastname);
printf("enter firstname of person to delete :\n");
scanf("%s", a.firstname);
write (socket_fd, &a, sizeof(addressbook));
sleep (1);
}else if(a.select == 4){
printf("enter lastname of person to modify:\n");
scanf("%s", a.lastname);
printf("enter firstname of person to modify :\n");
scanf("%s", a.firstname);
write (socket_fd, &a, sizeof(addressbook));
sleep (1);
}else{
printf("wrong selection\n");
}
if((a.select > 4) || (a.select < 1))
break;
}while(1);
/*close the socket */
close (socket_fd);
return 0;
}




Please suggest me if there is any wrong in my code. I am new to Linux programming, so I am trying to use sockets for reading and writing data from one process to another. Thank you.

anduril462
09-15-2011, 10:35 PM
You have a loop in the server code for writing the address book entries, but you only read one from the client side. You need to wrap that read()/printf() in a loop.

chaituchemico
09-16-2011, 01:49 PM
Thank you anduril462