Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-14-2012, 01:40 AM   #1
LQ Newbie
Registered: Oct 2012
Posts: 1

Rep: Reputation: Disabled
Socket Thread Programing

I am trying to synchronize a server for more than two different client using mutex..... but I am not getting the exact Idea.... Here is the server code...

Plz guide me regarding dat....

int server_sockfd, client_sockfd;
pthread_mutex_t mutex;

void *wwrite(void *ch)
write(client_sockfd, &ch, sizeof(ch));

int main()
int server_len, client_len;
struct sockaddr_un server_address;
struct sockaddr_un client_address;

pthread_t thread1;
/* remove old socket and create an unnamed socket */

server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);

/* Name the Socket */

server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, "server_socket");
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

/* create a connection queue and wait for clients */

listen(server_sockfd, 5);
printf("server waiting\n");

/* Accept connection */
int ch;
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);

/* read and write to client on client_sockfd */

read(client_sockfd, &ch, sizeof(ch));
printf("you entered %d\n",ch);
pthread_create(&thread1,NULL,wwrite,(void *)ch);

Old 10-14-2012, 03:43 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,089

Rep: Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986
I do not really understand what do you want to implement. But first, please try to use [code][/code] to keep formatting of your code. There is a problem with { and/or }.
Using pthread_create together with pthread_join means you create a thread and wait until it finished, therefore this while cycle will never be able to run two or more threads parallel.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] problem with server-socket programing chandinisree Linux - Networking 10 08-23-2010 04:49 AM
socket programing question golden_boy615 Programming 1 02-28-2010 12:29 AM
socket programing - using message boundaries libinmeledam Linux - Networking 4 06-03-2008 07:48 PM
I need some socket programing in C help tomazN Programming 6 12-08-2005 12:35 PM
Raw Socket Programing djgerbavore Programming 2 08-18-2004 01:58 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:48 PM.

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