LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2010, 10:35 AM   #1
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Rep: Reputation: 9
network programming related help required client server program


Hi,
I am reading and understanding code from a book.
I am copy pasting the exact code which I have.
Following program is for client.c
Code:
#include<sys/types.h>
#include<sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>>
#include <stdlib.h>
int main() {
int sockfd;
int len;
struct sockaddr_un address;
int result;
char ch = 'A';
        sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
        address.sun_family = AF_UNIX;
        strcpy(address.sun_path, "server_socket");
I was not able to understand the use of server_socket in above strcpy function.
Where does this come to picture

Code:
        len = sizeof(address);
        result = connect(sockfd, (struct sockaddr *)&address, len);
        if(result == -1) {
        perror("oops: client1");
        exit(1);
        }

write(sockfd, &ch, 1);
read(sockfd, &ch, 1);
printf("char from server = %c\n", ch);
close(sockfd);
exit(0);
}
When I run the code as
Code:
gcc client.c -o client.o
then on terminal I see each time letter B why is that happening.


and following program is for server.c
Code:
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include<stdlib.h>
int main()
{
        int server_sockfd, client_sockfd;
        int server_len, client_len;
        struct sockaddr_un server_address;
        struct sockaddr_un client_address;
        unlink("server_socket");
        server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
        server_address.sun_family = AF_UNIX;
        strcpy(server_address.sun_path, "server_socket");
I am not clear with use of server_address.sun_path
I understand that server_address is a struct of type sockaddr_un
but what is sun_path I had seen the file linux/un.h
but could not understand.

Code:
        server_len = sizeof(server_address);
        bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
        listen(server_sockfd, 5);
        while (1) {
                char ch;
                printf("server waiting\n");
                client_len = sizeof(client_address);
                client_sockfd = accept(server_sockfd,
                                       (struct sockaddr *)&client_address,
                                       &client_len);
                read(client_sockfd, &ch, 1);
                ch++;
                write(client_sockfd, &ch, 1);
                close(client_sockfd);
        }
}
 
Old 10-18-2010, 12:22 PM   #2
zak100
Member
 
Registered: Jul 2009
Posts: 262

Rep: Reputation: 2
Hi,
I can answer one of your queries. Clinet is sending 'A' to server while server is doing:
Code:
ch++;
and sending it back, so client gets 'B'.

Zulfi.
 
1 members found this post helpful.
Old 10-18-2010, 01:28 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
And, of course, for the other query look here:
Quote:
Sockets can use many different transports, not just TCP/IP ("AF_INET").

In this case, "sockaddr_un" holds an address for "Unix sockets" ("AF_UNIX"):

http://en.wikipedia.org/wiki/Unix_domain_socket

'Hope that helps!
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Interactive Client / Server Programming CyberEd Programming 8 08-31-2007 11:47 AM
OpenGL programming using server/client jmeza Programming 1 10-02-2006 05:01 PM
required iptable script for ftp and udp related program satish Linux - Networking 6 01-11-2006 09:44 AM
Refresh client user list at autentication client/server network. robertoneto123 Linux - Networking 0 11-11-2003 10:38 AM
Client-Server Programming Mohsen Programming 4 07-15-2003 01:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration